如何在ggplot2中保持变量顺序与输入一致?
问题
尝试使用以下R代码绘制模型回归系数的森林图:
library(ggplot2) library(tidyverse) # Create a data.frame forestPlot <- tribble( ~factor, ~PR, ~Low, ~High, #------/------/------/------/ "Breastfeeding: <17 weeks vs 26-51", 0.75,0.52,1.10, "17-25 weeks vs 26-51", 0.81, 0.50, 1.31, "≥52 weeks vs 26-51", 1.20, 0.88, 1.63, "Mother's age", 0.99, 0.96, 1.02, "Mother's education: High school vs tertiary", 1.64, 1.14, 2.36, "Vocational vs tertiary",1.31, 0.92, 1.86, "Mother's country of birth: UK/Ireland vs AU/NZ", 0.53, 0.17, 1.63, "India vs AU/NZ",1.66, 1.05, 2.61, "Asia-except India vs AU/NZ",1.86,1.28,2.69, "Other vs AU/NZ",0.83, 0.43, 1.58, "IRSAD: 1 vs 5", 1.48, 0.77, 2.83, "2 vs 5", 1.47, 0.79, 2.78, "3 vs 5", 1.09, 0.56, 2.14, "4 vs 5", 1.36, 0.72, 2.56, ) forestPlot # Factoring variables of the model forestPlot$factor <- factor(forestPlot$factor, levels = c( "Breastfeeding: <17 weeks vs 26-51", "17-25 weeks vs 26-51", "≥52 weeks vs 26-51", "Mother's age", "Mother's education: High school vs tertiary", "Vocational vs tertiary", "Mother's country of birth: UK/Ireland vs AU/NZ", "India vs AU/NZ", "Asia-except India vs AU/NZ", "Other vs AU/NZ", "IRSAD: 1 vs 5", "2 vs 5", "3 vs 5", "4 vs 5")) forestPlot # Make a forest plot library(forcats) Model1A <- forestPlot %>% ggplot(aes(x = fct_inorder(factor), y = PR, ymin = Low, ymax = High)) + geom_errorbar(width=.2, size = 1.0, show.legend = F) + geom_point(size= 3.5, shape=21, fill="white", show.legend = F)+ geom_hline(yintercept = 1, linetype = 2, col = "red", size = 0.8) + coord_flip() + labs(title = "A. Model for detal caries prevalance", subtitle = "Breastfeeding as main exposure", x = "", y = "PR (95% CI)") + theme( text = element_text(family = "Georgia"), plot.title = element_text(size = 14, color = "grey10", face = "bold"), plot.subtitle = element_text(face = "italic", color = "gray20", size = 12), plot.caption = element_text(face = "italic", size = 12, color = "gray40"), axis.title.x = element_text(face = "bold", size = 12, color = "grey20"), axis.title.y = element_blank(), panel.grid.major = element_line(color = "gray90"), panel.grid.minor.y = element_blank(), panel.grid.minor.x = element_blank(), panel.background = element_rect(fill = "#fcfbfd")) Model1A
但绘图中预测变量的顺序从底部开始显示,希望保持与输入变量相同的顺序(从顶部开始),使用fct_inorder(factor)函数并未生效,求解决方法。
解决方案
问题根源在于coord_flip()会翻转坐标轴的显示顺序:你已经手动指定了factor的levels为输入的从上到下顺序,但翻转后这个顺序会被反转,导致第一个变量出现在图的底部。
只需对factor变量反转因子顺序即可修正,有两种简单方法:
方法1:在aes中使用fct_rev()
将ggplot代码中的x = fct_inorder(factor)替换为x = fct_rev(factor),利用你已经提前设置好的levels反转顺序,配合coord_flip()就能让输入的第一个变量显示在图的顶部:
Model1A <- forestPlot %>% ggplot(aes(x = fct_rev(factor), y = PR, ymin = Low, ymax = High)) + geom_errorbar(width=.2, size = 1.0, show.legend = F) + geom_point(size= 3.5, shape=21, fill="white", show.legend = F)+ geom_hline(yintercept = 1, linetype = 2, col = "red", size = 0.8) + coord_flip() + labs(title = "A. Model for detal caries prevalance", subtitle = "Breastfeeding as main exposure", x = "", y = "PR (95% CI)") + theme( text = element_text(family = "Georgia"), plot.title = element_text(size = 14, color = "grey10", face = "bold"), plot.subtitle = element_text(face = "italic", color = "gray20", size = 12), plot.caption = element_text(face = "italic", size = 12, color = "gray40"), axis.title.x = element_text(face = "bold", size = 12, color = "grey20"), axis.title.y = element_blank(), panel.grid.major = element_line(color = "gray90"), panel.grid.minor.y = element_blank(), panel.grid.minor.x = element_blank(), panel.background = element_rect(fill = "#fcfbfd")) Model1A
方法2:设置坐标轴limits
不修改aes,而是添加scale_x_discrete(limits = rev(levels(forestPlot$factor)))来指定x轴的显示顺序,同样能达到效果:
Model1A <- forestPlot %>% ggplot(aes(x = factor, y = PR, ymin = Low, ymax = High)) + geom_errorbar(width=.2, size = 1.0, show.legend = F) + geom_point(size= 3.5, shape=21, fill="white", show.legend = F)+ geom_hline(yintercept = 1, linetype = 2, col = "red", size = 0.8) + coord_flip() + scale_x_discrete(limits = rev(levels(forestPlot$factor))) + # 添加这一行 labs(title = "A. Model for detal caries prevalance", subtitle = "Breastfeeding as main exposure", x = "", y = "PR (95% CI)") + theme( text = element_text(family = "Georgia"), plot.title = element_text(size = 14, color = "grey10", face = "bold"), plot.subtitle = element_text(face = "italic", color = "gray20", size = 12), plot.caption = element_text(face = "italic", size = 12, color = "gray40"), axis.title.x = element_text(face = "bold", size = 12, color = "grey20"), axis.title.y = element_blank(), panel.grid.major = element_line(color = "gray90"), panel.grid.minor.y = element_blank(), panel.grid.minor.x = element_blank(), panel.background = element_rect(fill = "#fcfbfd")) Model1A
两种方法都能让变量按照你输入的顺序从图的顶部开始显示,无需额外调整factor的levels设置。
内容的提问来源于stack exchange,提问作者user332276
相关产品推荐
相关产品推荐

