how create histogram from data frame in R
34,580
Solution 1
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)
Solution 2
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))
Solution 3
If you want a histogram of all the data, you can use
hist(c(df$col1,df$col2))
Author by
Deividas Kiznis
Updated on July 19, 2022Comments
-
Deividas Kiznis almost 2 years
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)