在ggplot2中按组排序Odds Ratio图的技术求助
解决Odds Ratio图按组排序问题
问题分析
你当前的代码中变量始终按字母排序的核心原因是:facet_grid(labels~.)是基于labels变量的顺序来排列分面的,仅将groups转为因子只会影响图例和着色,无法改变分面的排列顺序。
解决方案
要实现按组排序,需调整labels变量的因子水平顺序,具体操作如下:
- 指定你期望的组排列优先级(例如
Household→Demographics→Behaviour) - 将
groups转换为带指定顺序的因子,确保分组排序时遵循该优先级 - 按
groups分组排序数据,再把labels设置为因子,其水平顺序即为排序后的顺序
修改后的完整代码
library(ggplot2) # 数据定义 pfhist_df <- data.frame(labels = c("Travel time to hospital > 10h", "Travel time to hospital > 18h", "Travel time to hospital > 28h", "Ethnicity: Dusun", "Ethnicity: Rungus", "Ethnicity: Sungoi", "Wealth: quartile 2", "Wealth: quartile 3", "Wealth: quartile 4", "Occupation: student", "Previously diagnosed with malaria", "Elevation: 13 - 30m", "Elevation > 213m", "Travel time to clinic >7h", "Travel time to clinic >10h", "Head of household educated", "Spend more than 3h outside daily", "Travel to forest"), groups = c("Household", "Household", "Household", "Demographics", "Demographics", "Demographics", "Demographics", "Demographics", "Demographics", "Demographics", "Behaviour", "Household", "Household", "Household", "Household", "Demographics", "Behaviour", "Behaviour"), boxOdds = c(1.34, 1.58, 2.33, 1.89, 1.57, 1.46, 0.8, 0.69, 0.48, 0.43, 1.6, 1.26, 0.54, 1.22, 1.32, 0.79, 1.57, 1.4), boxCILow = c(1.09, 1.27, 1.83, 1.43, 1.21, 1.06, 0.67, 0.56, 0.38, 0.28, 1.36, 1.03, 0.4, 1.01, 1.08, 0.72, 1.11, 1.12), boxCIHigh = c(1.64, 1.96, 2.95, 2.49, 2.05, 2.29, 0.99, 0.85, 0.6, 0.65, 1.88, 1.54, 0.72, 1.48, 1.63, 0.87, 2.23, 1.75)) # 关键:调整因子顺序实现按组排序 # 1. 指定组的期望排列顺序 group_order <- c("Household", "Demographics", "Behaviour") pfhist_df$groups <- factor(pfhist_df$groups, levels = group_order) # 2. 按groups分组,保持组内原顺序(若要按OR值排序,可替换为order(pfhist_df$groups, pfhist_df$boxOdds)) pfhist_df <- pfhist_df[order(pfhist_df$groups), ] # 3. 将labels设置为因子,水平顺序为排序后的顺序 pfhist_df$labels <- factor(pfhist_df$labels, levels = pfhist_df$labels) # 绘图代码(与原代码一致,无需修改) ggplot(pfhist_df, aes(x = boxOdds, y = groups)) + geom_errorbarh(aes(xmin = boxCILow, xmax = boxCIHigh), size = .5, height = .2, color = "gray50") + geom_point(aes(colour = groups, shape = groups), size = 3.5 ) + geom_vline(aes(xintercept = 1), linetype = 2) + scale_shape_manual(values = rep(15, 5)) + scale_fill_manual(values = rep(c("#ffffff00", "#f0f0f090"), 9)[-1], guide = "none") + scale_x_log10() + #coord_cartesian(xlim = c(0.01, 100)) + facet_grid(labels~., switch = "y") + theme_bw() + theme(panel.spacing.y = unit(0, "points"), panel.border = element_blank(), axis.text.y = element_blank(), plot.title = element_text(hjust = 0.5), axis.ticks.length.y = unit(0, "points"), strip.text.y.left = element_text(angle = 0), strip.background.y = element_blank(), strip.placement = "outside", axis.line = element_line() ) + ylab("") + xlab("Odds Ratio") + ggtitle("Recent Exposure to Plasmodium falciparum")
额外调整说明
如果需要组内按Odds Ratio值排序,只需将排序代码替换为:
# 组内按OR值从小到大排序 pfhist_df <- pfhist_df[order(pfhist_df$groups, pfhist_df$boxOdds), ] # 若要从大到小排序,使用负号 pfhist_df <- pfhist_df[order(pfhist_df$groups, -pfhist_df$boxOdds), ]
内容的提问来源于stack exchange,提问作者bellbyrne
相关产品推荐
相关产品推荐

