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)
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With