0

I have a following dataset:

 country_iso2  fraud_flag    value
1  US            0            136
2  US            1            15
3  BR            0            130
4  BR            1            30
5  MX            0            96             
6  MX            1            54

Based on it I have created a plot using:

p1<-ggplot(data=d,aes(x=country_iso2,y=value,fill=fraud_flag))+
  geom_bar(position='dodge',stat='identity')

The plot looks like this:

enter image description here

How can I adjust my code so that bar groups are going in descending order based on orange bar. So in my case I want the order to be US,BR,MX.

Little L
  • 17
  • 6
  • Also covered in https://stackoverflow.com/q/63222313/5325862, https://stackoverflow.com/q/31955729/5325862, https://stackoverflow.com/q/12774210/5325862, https://stackoverflow.com/q/3253641/5325862, plus follow links in those posts to find many more – camille Oct 28 '21 at 00:16
  • The one from the first post puts my bars in the desired order but gives me a stacked plot instead of dodged plot for some reason, although I use position='dodge'. – Little L Oct 28 '21 at 00:54
  • Without seeing code, I don't know why that would be the case, but there are similar answers in the other 4 posts I linked to. The basic idea is just that you need a factor in order to sort it however you want – camille Oct 28 '21 at 01:01
  • I managed to get it correct in the end based on that link. Thanks for sharing! – Little L Oct 28 '21 at 10:50

1 Answers1

0

We arrange first and then convert the 'country_iso2' to factor based on the order of rows before plotting

library(dplyr)
library(ggplot2)
d %>% 
   arrange(desc(value), country_iso2) %>% 
   mutate(country_iso2 = factor(country_iso2, levels = unique(country_iso2)),
     fraud_flag = factor(fraud_flag)) %>% 
   ggplot(aes(x=country_iso2,y=value,fill=fraud_flag))+
     geom_bar(position='dodge',stat='identity')

-output

enter image description here

data

d <- structure(list(country_iso2 = c("US", "US", "BR", "BR", "MX", 
"MX"), fraud_flag = c(0L, 1L, 0L, 1L, 0L, 1L), value = c(136L, 
15L, 130L, 30L, 96L, 54L)), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6"))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Hi Akrun, Thank you for your response. I am only able to get the desired output if I run your second chunk (the one you labeled 'data') first and then run the first chunk. If I run only the first chunk I get the same output as before. My data actually has 20 countries so I don't know how I can get the correct structure for it. – Little L Oct 28 '21 at 00:52