0

I have a stacked bar chart providing the proportion of visa case status (certified or denied) by world region. Here is the summary table that is used for the plot, as well as the code for the plot itself:

#Summarize grouped dataframe by World Region and Case Status Coded (by number 
of records and proportion)

summ_worldreg <- df_nona_worldreg %>% group_by(World.Region, 
CaseStatusCodedFactor) %>% summarise(n=n()) %>% mutate(proportion = n/sum(n))

Output of summ_worlreg: 

   World.Region              CaseStatusCodedFactor     n proportion
   <fct>                     <fct>                 <int>      <dbl>
 1 Australia and New Zealand Denied                  125     0.0628
 2 Australia and New Zealand Certified              1865     0.937 
 3 Carribbean                Denied                  337     0.187 
 4 Carribbean                Certified              1467     0.813 
 5 Central America           Denied                 2423     0.238 
 6 Central America           Certified              7762     0.762 
 7 Central Asia              Denied                   31     0.0861
 8 Central Asia              Certified               329     0.914 


#Plot 100% Stacked Bar Graph showing distribution of Case Status by World 
Region

worldreg_case_bar <- ggplot(summ_worldreg, aes(x = summ_worldreg$World.Region, 
y = summ_worldreg$proportion, fill = summ_worldreg$CaseStatusCodedFactor)) + 
geom_bar(stat = 'identity') 

Image of Plot: 100% Stacked Bar Chart - Case Status by World Region

I am wanting to order the factors (World Regions) in this bar chart so that the proportion of 'denied' records increases from left to right. In other words, I want the world region with the lowest proportion of 'denied' records to be on the far left and the world region with the highest proportion of 'denied' records to be on the far right.

Any insight on how to order this bar chart would be much appreciated. Thanks!

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
J. Staak
  • 11
  • 2
  • Use `reorder` to make the levels of World Region go in order you want. – Gregor Thomas Mar 02 '18 at 18:35
  • See, e.g., any of the first five questions [when you search the ggplot2 tag for order](https://stackoverflow.com/search?q=%5Bggplot2%5D+order). – Gregor Thomas Mar 02 '18 at 18:37
  • 1
    Also, never use `$` inside `aes()`. It is bad practice and will cause problems with complicated plots (with facets, summary stats, etc.). Just do `ggplot(summ_worldreg, aes(x = World.Region, y = proportion, fill = CaseStatusCodedFactor))` – Gregor Thomas Mar 02 '18 at 18:46
  • You can also use `forcats::fct_reorder` to do the reordering based on a function of other variables, which makes it easy to do this without needing to type the order manually. – Calum You Mar 02 '18 at 20:10
  • Possible duplicate of [Reorder bars in geom\_bar ggplot2](https://stackoverflow.com/questions/25664007/reorder-bars-in-geom-bar-ggplot2) – Michael Harper Mar 02 '18 at 22:15
  • Thanks, all - much appreciated. How would I use reorder within ggplot2 to reorder by 'proportion' only with respect to the 'Certified' rows? Right now I have: firm_case_bar <- ggplot(summ_firm2, aes(x = reorder(Firm.Size, proportion), y = proportion, fill = CaseStatusCodedFactor)) + geom_bar(stat = 'identity'). I know that the 'proportion' piece of the reorder function needs to be updated to only pull 'proportion' from 'Certified' rows. Any advice? – J. Staak Mar 03 '18 at 23:37
  • Keep your code simple and do the reordering before the ggplot. In this case, I would probably just refactor rather than using reorder: `my_order = filter(summ_worldreg, CaseStatusCodedFactor == "Certified") %>% arrange(proportion) %>% pull(World.Region)`, then `summ_worldreg = mutate(summ_worldreg, World.Region = factor(World.Region, levels = my_order)`. – Gregor Thomas Mar 05 '18 at 02:32

0 Answers0