I want to make histograms for all the quantitative variables in a data frame.
the data can be found here
the code is here:
library(ggplot2)
cereal <- read.csv('Cereals.csv')
quantitative <- c("calories", "protein", "fat", "sodium", "fiber", "carbo", "sugars", "potass", "vitamins", "weight", "cups")
cereal[,quantitative] <- as.numeric(as.character(unlist(cereal[,quantitative])))
for (variable in quantitative){
plot <- ggplot(cereal, aes(variable))
+geom_histogram(binwidth = 0.5)
print(plot)
}
but I always get the error: "StatBin requires a continuous x variable: the x variable is discrete. Perhaps you want stat="count"?"
I have check some solutions such as change geom_histogram to geom_bar or add binwidth = 0.;5 but none of these help.
Does anyone know how to solve this question? Thank you!