Manual ordering of categorical variables in ggplot has been changed.

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

Manual ordering of categorical variables in ggplot is changed

问题

I manually re-ordered my categories as:

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

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

bite_anatomy_other_variables %>%
  filter(!(species %in% c("猴子", "家畜(牛/山羊/绵羊/猪/马)"))) %>%
  add_count(species) %>%
  mutate(species = factor(species, levels = species_order)) %>%
  mutate(species = paste0(species, "\n", "(n = ", n, ")")) %>%
  ggplot(mapping = aes(y = species, fill = body_part_collapsed)) +
  geom_bar(colour = "black", position = "fill") +
  scale_x_continuous(labels = c("0", "25%", "50%", "75%", "100%")) +
  labs(x = "回应比例", y = "动物种类") +
  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:

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

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

bite_anatomy_other_variables %>%
  filter(!(species %in% c("Monkey", "Livestock (cattle/goat/sheep/pig/horse)"))) %>%
  add_count(species) %>%
  mutate(species = factor(species, levels = species_order)) %>%
  mutate(species = paste0(species, "\n", "(n = ", n, ")")) %>% # THIS CODE CREATES ISSUES
  ggplot(mapping = aes(y = species, fill = body_part_collapsed)) +
  geom_bar(colour = "black", position = "fill") +
  scale_x_continuous(labels = c("0", "25%", "50%", "75%", "100%")) +
  labs(x = "Proportion of responses", y = "Animal species") +
  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:

bite_anatomy_other_variables %>%
  filter(!(species %in% c("Monkey", "Livestock (cattle/goat/sheep/pig/horse)"))) %>%
  add_count(species) %>%
  mutate(y_species = 
    reorder(
      factor(paste0(species, "\n", "(n = ", n, ")")),
      as.numeric(species)
  )) %>%
  ggplot(mapping = aes(y = y_species, fill = body_part_collapsed)) +
  geom_bar(colour = "black", position = "fill") +
  scale_x_continuous(labels = c("0", "25%", "50%", "75%", "100%")) +
  labs(x = "Proportion of responses", y = "Animal species") +
  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:

bite_anatomy_other_variables %>%
  filter(!(species %in% c("Monkey", "Livestock (cattle/goat/sheep/pig/horse)"))) %>%
  add_count(species) %>%
  mutate(y_species = 
    reorder(
      factor(paste0(species, "\n", "(n = ", n, ")")),
      as.numeric(species)
  )) %>% 
  ggplot(mapping = aes(y = y_species, fill = body_part_collapsed)) +
  geom_bar(colour = "black", position = "fill") +
  scale_x_continuous(labels = c("0", "25%", "50%", "75%", "100%")) +
  labs(x = "Proportion of responses", y = "Animal species") +
  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:

确定