I would like to produce histograms with density lines for all my numeric columns, and facet them by another column.
Using the Iris data set as an example, I would like to produce histograms for Sepal.Length
, etc., with facets for each of Species
.
This is what I have tried:
for (i in colnames(subset(iris, select = -`Species`))) {
plot= ggplot(iris, aes(x= i))+
geom_histogram()+
geom_density(colour = "blue", size = 1) +
facet_wrap(~ Species, scales = "free")
print(plot)
}
I also tried
for (i in colnames(subset(iris, select = -`Species`))) {
plot= ggplot(subset(iris, select = -`Species`), aes(x= i))+
geom_histogram()+
geom_density(colour = "blue", size = 1) +
facet_wrap(~ iris$Species, scales = "free")
print(plot)
}
The error I get is
Error in
f()
: StatBin requires a continuous x variable: the x variable is discrete. Perhaps you want stat="count"?
Do I need to put something in the geom_histogram()
command?