I am trying to create a bar plot with two layers, one ordered by counts, and the other one a running total of the counts. This is where I am at the moment:
ggplot(data = df %>% group_by(variable_int) %>% summarise(n = n()) ,
aes(x = as.factor(variable_int), y = n)) +
geom_col() +
geom_col(aes(y = cumsum(n)), alpha = 0.3)
The variable is categorical but the levels are integers (0-20), hence why I've used as.factor().
What I'd like to do is order the bar plot in an ascending fashion by counts for the first layer (geom_col()
), with a transparent running total (second) layer appearing in the background of the plot. What I've got at the moment though is the running total layer in ascending order and the other layer ordered in terms of its categorical level (i.e. 0-20), not in terms of its counts, so it appears unordered.
I've tried using x = reorder(as.factor(variable_int),n)
but this doesn't seem to work.
Any help would be much appreciated! Thanks!