У меня есть этот 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))
, который я построил следующим образом:
df %>%
ggplot(aes(x=Incident, y=Percent, fill=Gender))+
geom_col(position = "dodge", width=0.72)
Теперь мне нужно отсортировать Инцидент от более высокого общего процента до более низкого общего процента, чтобы вымогательство сначала идет слева, затем следует физическое насилие и др. c. Я попытался:
df %>%
mutate(Incident=reorder(Incident, -Percent)) %>%
ggplot(aes(x=Incident, y=Percent, fill=Gender))+
geom_col(position = "dodge", width=0.72)
Но я получаю ошибку:
Error: Column `Incident` can't be modified because it's a grouping variable
Затем я попытался ungroup
или fct_rev
, но я не могу заставить его работать! Единственное, что работает, это экспортирует df как csv, затем импортирует его снова, и тогда он работает. Но, конечно, это не очень эффективно ... Кто-нибудь, пожалуйста, помогите!