Plotting multiple histograms quickly in R

21,562

Solution 1

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)

Solution 2

With ggplot2 you can use facet_wrap to create a grid based on other variables.

For example:

library(ggplot2)

data(mtcars)

ggplot(data = mtcars) +
    geom_histogram(aes(x = mpg), bins = 4, colour = "black", fill = "white") +
    facet_wrap(~ gear)

example histogram with facets

And you can use the bins parameter to easily set how many breaks you want.

Share:
21,562

Related videos on Youtube

Gooze
Author by

Gooze

Data learner &amp; R user. Economist by trade, I use maps and quantitative analysis to get to the bottom of urban based issues and provide some evaluation on the societal effects of policies and transitions.

Updated on July 05, 2022

Comments

  • Gooze
    Gooze almost 2 years

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

    data(mtcars)    
    hist(mtcars[,c(1,2,3,4)])
    

    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 ggplot2?

    • amatsuo_net
      amatsuo_net almost 7 years
      hist(mtcars[c(1,2,3,4)]) doesn't work. what do you see on your screen?
    • abichat
      abichat almost 7 years
      After loading mtcars, your code hist(mtcars[c(1,2,3,4),1]) produces an error: Error in hist.default(mtcars[c(1, 2, 3, 4)]): 'x' must be numeric
    • Gooze
      Gooze over 6 years
      Sorry for the late reply, i had computer issues and then forgot about this. The code runs fine for me in RStudio, however perhaps its best to put a comma before the column specification: hist(mtcars[,c(1,2,3,4)])
  • Gregor Thomas
    Gregor Thomas almost 7 years
    This isn't quite what OP wants--OP is asking for how to plot histograms of each column, not of one column by another.
  • Paolo
    Paolo almost 7 years
    @Gregor I realize that now after seeing the other answer (which works and seems to answer the question)
  • Gooze
    Gooze over 6 years
    Thanks, i think i can work with this by changing 1:ncol(mtcars) to 1:4. When i run lapply(mtcars[1:4], FUN=hist), however, i do not get the desired result above. Do you know why that is?