Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how create histogram from data frame in R

Tags:

r

histogram

I want create histogram from data frame, but every time code is used I get error 'x' must be numeric.

    df <- data.frame(col1 = c(10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120),
    col2 = c(10, 21, 31, 41, 51, 61, 72, 82, 92, 104, 114, 134))

    hist(df)
like image 683
Deividas Kiznis Avatar asked Apr 23 '17 18:04

Deividas Kiznis


2 Answers

you can do

hist(df$col1)

or

with(df, hist(col2))

If you want all the columns each in their own histograms you could perhaps do something like

par(mfrow=c(2,1))
histout=apply(df,2,hist)
like image 85
Glen_b Avatar answered Sep 21 '22 06:09

Glen_b


Please consider other visualizations for your example, as a histogram may not be the best for comparing the very similar data in col1 and col2. In your case, it would be useful to transform your df first into a tidy format

library(ggplot2)
library(tidyr)

df_tidy <- gather(df, cols, value) 

and then use one of the following charts that highlight the small differences in the data:

as density chart:

ggplot(df_tidy, aes(x = value)) + 
  geom_density(aes(color=cols))

or scatter plot:

ggplot(df_tidy, aes(x = value, y=cols)) + 
  geom_point(aes(color=cols), size=3) +
  scale_x_continuous(breaks = c(0,25,50,75,100,125))

or boxplot:

ggplot(df_tidy, aes(x = cols, y=value)) + 
  geom_boxplot(aes(fill=cols))
like image 43
Agile Bean Avatar answered Sep 18 '22 06:09

Agile Bean