英文:
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?
答案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")
英文:
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")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论