1

I have this df,

    df <- structure(list(Gender = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("", "Female", "Male", 
"Q6 - OBS: Sex of Respondent"), class = "factor"), Incident = c("Death", 
"Detention", "Extortion", "Kidnapping", "Physical_abuse", "Robbery", 
"Sexual_assault", "Death", "Detention", "Extortion", "Kidnapping", 
"Physical_abuse", "Robbery", "Sexual_assault"), Victim = structure(c(5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("", 
"No", "Q54 - Did you   witness any migrant deaths   during your journey?", 
"Refused", "Yes", "Q69 - Did you   experience any physical abuse or harassment (of a non-sexual nature) during your journey?", 
"Q62 - Did you   witness or experience any sexual assault or harassment during your journey?", 
"Q75 - Have   you been kidnapped or otherwise held against your will during your journey?", 
"Q96 - Have you been detained by the police, military, militia or immigration officials during your journey?", 
"Q84 - Have   you ever been robbed during   your journey?", "Q90 - Did you   have to give government officials gifts, services or   bribes during your journey?"
), class = "factor"), n = c(253L, 300L, 1978L, 73L, 740L, 646L, 
553L, 436L, 816L, 4052L, 194L, 1196L, 1059L, 259L), Percent = c(8, 
10, 65, 2, 24, 21, 18, 6, 12, 59, 3, 17, 15, 4)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -14L), groups = structure(list(
    Gender = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L), .Label = c("", "Female", "Male", "Q6 - OBS: Sex of Respondent"
    ), class = "factor"), Incident = c("Death", "Detention", 
    "Extortion", "Kidnapping", "Physical_abuse", "Robbery", "Sexual_assault", 
    "Death", "Detention", "Extortion", "Kidnapping", "Physical_abuse", 
    "Robbery", "Sexual_assault"), .rows = list(1L, 2L, 3L, 4L, 
        5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L)), row.names = c(NA, 
-14L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE))

which I plotted like this:

df %>% 
 ggplot(aes(x=Incident, y=Percent, fill=Gender))+
 geom_col(position = "dodge", width=0.72)

Now I need to sort Incident from higher total percentage to lower total percentage, so that Extortion comes first on the left, followed by Physical abuse, etc. I have tried:

df %>% 
  mutate(Incident=reorder(Incident, -Percent)) %>%
  ggplot(aes(x=Incident, y=Percent, fill=Gender))+
  geom_col(position = "dodge", width=0.72)

But I get the error:

Error: Column `Incident` can't be modified because it's a grouping variable

I have then tried ungroup, or fct_rev, but I cannot make it work! The only thing that works is to export the df as csv, to then import it again, and then it works. But of course that is not very efficient... Anybody please help!

johnjohn
  • 716
  • 2
  • 9
  • 17
  • 3
    Where did you call `ungroup`? It's unclear why that wouldn't have worked – camille Feb 10 '20 at 20:32
  • 3
    Using `df %>% ungroup() %>% mutate(Incident=reorder(Incident, -Percent)) %>% ggplot(aes(x=Incident, y=Percent, fill=Gender))+ geom_col(position = "dodge", width=0.72)` works for me. If it does not for you , maybe you have a conflict with an another package using a function called `ungroup`. Maybe you can restart your r session – dc37 Feb 10 '20 at 20:36
  • camille and dc37, indeed I had called ungroup at the wrong place!! By curiosity, is ungroup the only way to sort variables that have been grouped? many thanks – johnjohn Feb 10 '20 at 20:48

0 Answers0