how create histogram from data frame in R

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)

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))

Tags:

R

Histogram