【问题标题】:Plotting every variable in a dataframe like facets绘制数据框中的每个变量,如构面
【发布时间】:2016-09-23 14:38:24
【问题描述】:

我有一个数据框 df,其中包含 12 个变量的 17 个观察值。每个观察值代表一个变量在一年中假设的值,我的年份是从 1995 年到 2011 年。

我想在 x 轴上绘制一些变量,在 x 轴上始终绘制年份,在 y 轴上绘制变量的值。我希望它像方面一样,在一个图表中。

我该怎么做?我使用 ggplot2 来绘制其中一个变量。

我写的代码:

ggplot(c4ItaGerFinale, aes(Anno,EXPtot)) +
  geom_point(color = "steelblue", size=2) +
  labs(x= "Anno", y= "Milioni di dollari", title = "Settore Tessile, wiod c4") +
  coord_cartesian(ylim=c(0,6000))

EXPtot 是一个变量,Anno 是我希望始终位于 x 轴上的变量,c4ItaGerFinale 是我的 data.frame。

【问题讨论】:

  • 使用 tidyr::gather 将所有 y 变量放在一列中,然后按变量分面
  • 欢迎来到 Stack Overflow!请阅读有关how to ask a good question 的信息以及如何提供reproducible example。这将使其他人更容易帮助您。

标签: r plot ggplot2


【解决方案1】:

正如用户@Richard Telford 所建议的,我使用gather 将所有变量放在一个列中,然后按变量分面。

dfgraph <- gather(df, Variable, Name, 2:12)

然后使用ggplot。

【讨论】:

    猜你喜欢
    • 2019-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-18
    • 2020-10-05
    • 2020-01-09
    相关资源
    最近更新 更多