Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot every column in a data frame as a histogram on one page using ggplot

Tags:

r

ggplot2

I would like to plot each column of a data.frame using a histogram on one page. Here is an example using the sample "diamonds" data set which comes with R:

p = list()
for (i in 1:ncol(diamonds)) p[[i]] <- qplot(diamonds[,i], xlab=names(diamonds)[[i]])
do.call(grid.arrange, p)

enter image description here

This does plot all the columns, but the data looks the same in each one. So, something is clearly wrong.

Is this the right approach for this task? I'm sure I have some silly syntax somewhere that is assigning the same column data set to each element in the list, but I'm not sure what it is.

Thank you

like image 886
oneself Avatar asked Oct 23 '12 17:10

oneself


People also ask

How do I make a histogram of all columns in R?

To create histogram of all columns in an R data frame, we can use hist. data. frame function of Hmisc package. For example, if we have a data frame df that contains five columns then the histogram for all the columns can be created by using a single line code as hist.

How do you plot multiple histograms in the same plot in R?

Plot two histograms Using plot() will simply plot the histogram as if you'd typed hist() from the start. However, you can now use add = TRUE as a parameter, which allows a second histogram to be plotted on the same chart/axis.

Can you build a histogram using ggplot2?

You can also make histograms by using ggplot2 , “a plotting system for R, based on the grammar of graphics” that was created by Hadley Wickham. This post will focus on making a Histogram With ggplot2.

Which method is used to create a histogram using ggplot2?

Basic histogram with geom_histogram It is relatively straightforward to build a histogram with ggplot2 thanks to the geom_histogram() function. Only one numeric variable is needed in the input.


1 Answers

Here you go:

library(reshape2)
library(ggplot2)
d <- melt(diamonds[,-c(2:4)])
ggplot(d,aes(x = value)) + 
    facet_wrap(~variable,scales = "free_x") + 
    geom_histogram()

enter image description here

melting allows us to use the resulting grouping variables (called variable) to split the data into groups and plot a histogram for each one. Note the use of scales = "free_x" because each of the variables has a markedly different range and scale.

like image 87
joran Avatar answered Oct 24 '22 11:10

joran