Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: plot histogram of all columns in a data.frame

I'm a new user in R and I've just started to work with it to see the distribution of my data but I got stuck on this error. I have a data frame and I would like to plot histograms of it's numeric columns. So what I did is as bellow :

num_data <-my_data[, sapply(my_data, is.numeric)] 
for (i in 1:length(names(num_data))){
  print(i)
  hist( num_data[i], main='hist', breaks=20, prob=TRUE)
}

But I get the error 'Error in hist.default(num_data[i], main = "hist", breaks = 20, prob = TRUE) : 'x' must be numeric ' I checked the type of num_data[i] and it is a list of numeric values. SO I have no idea of what is the problem. Can any one please give me hint?

like image 425
Alex Avatar asked May 01 '16 20:05

Alex


2 Answers

A side by side ggplot solution.

library(ggplot2)
library(tidyr)
ggplot(gather(num_data, cols, value), aes(x = value)) + 
       geom_histogram(binwidth = 20) + facet_grid(.~cols)
like image 168
Psidom Avatar answered Sep 21 '22 02:09

Psidom


More reliable than hist() is the histogram function from the Hmisc package:

library(Hmisc)
hist.data.frame(num_data)

This should print histograms for all columns in your dataframe.

like image 40
Agile Bean Avatar answered Sep 19 '22 02:09

Agile Bean