Gooze Gooze - 24 days ago 9
R Question

Plotting multiple histograms quickly in R

For exploratory analysis, its often useful to quickly plot multiple variables in one grid. An easy way to do this is to:


enter image description here

However, it becomes difficult to adjust breaks and axes to maintain consistency i.e.

hist(mtcars[,c(1,2,3,4)], breaks = 10)

does not effect the histograms. Is there an easy work around this or an easy way to do this in

Answer Source

This is how to do it with hist() :

lapply(mtcars[1:4], FUN=hist)

However I prefer to store plots in R objects with ggplot2 and display plot lists with cowplot::plotgrid() :

list <-lapply(1:ncol(mtcars),
              function(col) ggplot2::qplot(mtcars[[col]],
                                           geom = "histogram",
                                           binwidth = 1))

cowplot::plot_grid(plotlist = list)