简体   繁体   中英

Arranging plots using grid.arrange R

I have six plots obtained with ggplot2 for normality analysis: 2 histograms, 2 qqplots and 2 boxplots.

I want to display them together ordered by type of plot: so the histograms in the first row, the qqplots in the second row and the boxplots in the third row. For this I use the grid.arrange function from gridExtra package as follows:

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

But this error message pops up:

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

any idea how to solve this?

As people said in the comments the error was the aes() of one of the plots. The confussion came as R allows you to create an object even when it´s not operational, I guess this is because it can be modified later. This is the code for the plot:

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()

As you can see, the mistake is the first aes() argument, as I wrote sample= instead of x=. Already solved.

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM