0

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!

Sean
  • 47
  • 1
  • 6
  • 1
    Please add minimal working example. https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. I am unable to reproduce your error with the information provided – George Jul 26 '18 at 02:36
  • Possible duplicate of [Change the order of a discrete x scale](https://stackoverflow.com/questions/3253641/change-the-order-of-a-discrete-x-scale) – andrew_reece Jul 26 '18 at 03:31
  • Hi Andrew, thank you for the suggestion! I did try the answers given in that page but without any success I'm afraid (I am quite new to R though so might have missed something!). I don't know if it is due to all those particular solutions using geom_bar whereas I am using geom_col that caused me some trouble? I found the below answer by @TC-Zhang to do the trick though :) – Sean Jul 26 '18 at 14:18

1 Answers1

1

try this, the trick is to set the levels of variable_int factor.

library(tidyverse)
df <- data.frame(variable_int = sample(1:20, 30,replace = T))

gg.data <- 
  df %>% 
  group_by(variable_int) %>% 
  summarise(n = n()) %>%
  arrange(n) %>%
  ungroup() %>%
  mutate(variable_int=factor(variable_int, levels = variable_int))

ggplot(data = gg.data , 
   aes(x = variable_int, y = n)) + 
   geom_col() +
   geom_col(aes(y = cumsum(n)), alpha = 0.3)

Created on 2018-07-26 by the reprex package (v0.2.0.9000).

TC Zhang
  • 2,757
  • 1
  • 13
  • 19
  • Thank you very much TC, this did the trick! I only made a slight change by using `arrange(desc(n))` instead of `arrange(n)` so that it was shown in decreasing order instead of increasing. Ended having a really nice example of the Pareto principle for my data which I could see really clearly with this plot so thanks again! – Sean Jul 26 '18 at 14:11