I don't get how to make the bars of an histogram to appears in descending order with ggplot.
Heres my code with a dataframe that everyone can use :
library(ggplot2)
library(scales)
chol <- read.table(url("http://assets.datacamp.com/blog_assets/chol.txt"),
header = TRUE)
ggplot(chol) +
geom_histogram(aes(x = AGE, y = ..ncount.., fill = ..ncount..),
breaks=seq(20, 50, by = 2),
col="red",
alpha = .2) +
scale_fill_gradient("Percentage", low = "green", high = "red") +
scale_y_continuous(labels = percent_format()) +
labs(title="Histogram for Age") +
labs(x="Age", y="Percentage")
The resulting histogram that i want in descending order :
I tried to order the column AGE before plotting :
## set the levels in order we want
Chol <- within(Chol,
AGE <- factor(AGE,
levels=names(sort(table(AGE),
decreasing=TRUE)
I get an error when i plot the order AGE with ggplot and geom_histogram.