Manual ordering of categorical variables in ggplot has been changed.

huangapple go评论91阅读模式
英文:

Manual ordering of categorical variables in ggplot is changed

问题

I manually re-ordered my categories as:

  1. bite_anatomy_other_variables <- bite_anatomy_other_variables %>%
  2. mutate(species = fct_relevel(species,
  3. "宠物狗" , "无主狗" , "狗(未知状态)",
  4. "宠物猫" , "无主猫" , "猫(未知状态)",
  5. "鼠类/啮齿动物" , "猴子" , "家畜(牛/山羊/绵羊/猪/马)"))

Then when I make a plot with the code below, the order changes

  1. bite_anatomy_other_variables %>%
  2. filter(!(species %in% c("猴子", "家畜(牛/山羊/绵羊/猪/马)"))) %>%
  3. add_count(species) %>%
  4. mutate(species = factor(species, levels = species_order)) %>%
  5. mutate(species = paste0(species, "\n", "(n = ", n, ")")) %>%
  6. ggplot(mapping = aes(y = species, fill = body_part_collapsed)) +
  7. geom_bar(colour = "black", position = "fill") +
  8. scale_x_continuous(labels = c("0", "25%", "50%", "75%", "100%")) +
  9. labs(x = "回应比例", y = "动物种类") +
  10. ggtitle("暴露的身体部位")

The manual order stayed the intended way when I did not use the "mutate(species = paste0(species, "\n", "(n = ", n, ")")) %>%"

Now when I use the above code, the order changes, and I can't seem to return it back. Can you help me with showing the graph in my desired category order?

英文:

I manually re-ordered my categories as:

  1. bite_anatomy_other_variables <- bite_anatomy_other_variables %>%
  2. mutate(species = fct_relevel(species,
  3. "Pet dog" , "Unowned dog" , "Dog (unknown status)",
  4. "Pet cat" , "Unowned cat" , "Cat (unknown status)",
  5. "Rat/rodent" , "Monkey" , "Livestock (cattle/goat/sheep/pig/horse)"))

Then when I make a plot with the code below, the order changes

  1. bite_anatomy_other_variables %>%
  2. filter(!(species %in% c("Monkey", "Livestock (cattle/goat/sheep/pig/horse)"))) %>%
  3. add_count(species) %>%
  4. mutate(species = factor(species, levels = species_order)) %>%
  5. mutate(species = paste0(species, "\n", "(n = ", n, ")")) %>% # THIS CODE CREATES ISSUES
  6. ggplot(mapping = aes(y = species, fill = body_part_collapsed)) +
  7. geom_bar(colour = "black", position = "fill") +
  8. scale_x_continuous(labels = c("0", "25%", "50%", "75%", "100%")) +
  9. labs(x = "Proportion of responses", y = "Animal species") +
  10. ggtitle("Body part exposed")

The manual order stayed the intended way when I did not use the "mutate(species = paste0(species, "\n", "(n = ", n, ")")) %>%"

Now when I use the above code, the order changes, and I can't seem to return it back. Can you help me with showing the graph in my desired category order?

Undesired order

Desired order but with the "(n = xxx)" in the y-axis

答案1

得分: 0

以下是代码的翻译部分:

"A labeler (as I suggested in comments) won't actually work so easily since it requires input from another column. Instead, I create a new factor with the annotations and use reorder to match the manually ordered version:

  1. bite_anatomy_other_variables %>%
  2. filter(!(species %in% c("Monkey", "Livestock (cattle/goat/sheep/pig/horse)"))) %>%
  3. add_count(species) %>%
  4. mutate(y_species =
  5. reorder(
  6. factor(paste0(species, "\n", "(n = ", n, ")")),
  7. as.numeric(species)
  8. )) %>%
  9. ggplot(mapping = aes(y = y_species, fill = body_part_collapsed)) +
  10. geom_bar(colour = "black", position = "fill") +
  11. scale_x_continuous(labels = c("0", "25%", "50%", "75%", "100%")) +
  12. labs(x = "Proportion of responses", y = "Animal species") +
  13. ggtitle("Body part exposed")

Manual ordering of categorical variables in ggplot has been changed."

英文:

A labeler (as I suggested in comments) won't actually work so easily since it requires input from another column. Instead, I create a new factor with the annotations and use reorder to match the manually ordered version:

  1. bite_anatomy_other_variables %>%
  2. filter(!(species %in% c("Monkey", "Livestock (cattle/goat/sheep/pig/horse)"))) %>%
  3. add_count(species) %>%
  4. mutate(y_species =
  5. reorder(
  6. factor(paste0(species, "\n", "(n = ", n, ")")),
  7. as.numeric(species)
  8. )) %>%
  9. ggplot(mapping = aes(y = y_species, fill = body_part_collapsed)) +
  10. geom_bar(colour = "black", position = "fill") +
  11. scale_x_continuous(labels = c("0", "25%", "50%", "75%", "100%")) +
  12. labs(x = "Proportion of responses", y = "Animal species") +
  13. ggtitle("Body part exposed")

Manual ordering of categorical variables in ggplot has been changed.

huangapple
  • 本文由 发表于 2023年5月22日 23:00:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76307485.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定