I have the following data frame and want to plot a stacked area plot:
library(ggplot2)
set.seed(11)
df <- data.frame(a = rlnorm(30), b = as.factor(1:10), c = rep(LETTERS[1:3], each = 10))
ggplot(df, aes(x = as.numeric(b), y = a, fill = c)) +
geom_area(position = 'stack') +
theme_grey() +
scale_x_discrete(labels = levels(as.factor(df$b))) +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
The resulting plot on my system looks like this:
Unfortunately, the x-axis doesn't seem to show up. I want to plot the values of df$b
rotated so that they don't overlap, and ultimately I would like to sort them in a specific way (haven't gotten that far yet, but I will take any suggestions).
Also, according to ?factor()
using as.numeric()
with a factor is not the best way to do it. When I call ggplot
but leave out the as.numeric()
for aes(x=...
the plot comes up empty.
Is there a better way to do this?