【问题标题】:Arranging plots using grid.arrange R使用 grid.arrange R 排列图
【发布时间】:2020-06-16 21:58:18
【问题描述】:

我用 ggplot2 获得了六个用于正态性分析的图:2 个直方图、2 个 qqplots 和 2 个箱线图。

我想按绘图类型将它们一起显示:所以第一行是直方图,第二行是 qqplots,第三行是箱线图。为此,我使用 gridExtra 包中的 grid.arrange 函数,如下所示:

grid.arrange(grobs= list(plot1, plot2, qqplot1, qqplot2, boxplot1, boxplot2),
             ncol=2, nrow=3,
             top = ("Histograms + Quantile Graphics + Boxplots"))

但是弹出这个错误信息:

Error: stat_bin() requires an x or y aesthetic.

知道如何解决这个问题吗?

【问题讨论】:

  • 它不是来自 gridextra,你的一个图不起作用,在合并之前测试每个图。
  • 您的其中一个情节似乎存在问题。该错误消息表明您错误地设置了直方图的美学。 geom_histogram 只需要一个变量。可以分享一下代码吗?建议使用do.call("grid.arrange", c(plot_list, ncol = 2, nrow = 3))plot_list 作为你的地块列表,因为grid.arrange 需要grobs、gtables、ggplot 或trellis 对象,而不是这些对象的列表。

标签: r plot gridextra


【解决方案1】:

正如人们在 cmets 中所说,错误是其中一个图的 aes()。混乱来了,因为 R 允许你创建一个对象,即使它不是可操作的,我想这是因为它可以在以后修改。这是情节的代码:

ggplot(data = mtcars, aes(sample=mtcars$mpg)) +
  geom_histogram(aes(y = ..density.., fill = ..count..), binwidth = 1) +
  geom_density(alpha=.2) +
  scale_fill_gradient(low = "#6ACE78", high = "#0D851D") +
  stat_function(fun = dnorm, colour = "firebrick",
                args = list(mean = mean(mtcars$mpg),
                            sd = sd(mtcars$mpg))) +
  labs(x = "Tiempo de seguimiento", y = "")+
  theme_bw()

如您所见,错误在于第一个 aes() 参数,因为我写的是 sample= 而不是 x=。已经解决了。

谢谢

【讨论】:

    猜你喜欢
    • 2012-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-14
    相关资源
    最近更新 更多