如何使用ggh4x包自定义嵌套X轴的分组排序?
自定义ggh4x嵌套箱线图的分组排序
问题描述
我使用ggh4x包结合以下数据集与代码创建带有两个分类变量嵌套关系的箱线图,当前X轴的嵌套关系显示符合预期,但分组按字母顺序排列,希望自定义排序,将「Not Exposed」组置于「Exposed」组之前。尝试使用weave_factors替代interaction后,嵌套关系无法正确显示,求可行的自定义分组排序方法。
使用的数据集
set1 <- structure(list(Tx = c("Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Exposed", "Exposed", "Exposed", "Exposed", "Exposed", "Exposed", "Exposed", "Exposed", "Exposed", "Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Exposed", "Exposed", "Exposed", "Exposed", "Exposed", "Exposed", "Exposed", "Exposed", "Exposed", "Exposed"), Species = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), levels = c("Species1", "Species2"), class = "factor"), Size = c(88.5, 83.3, 59.5, 78, 50.3, 57, 78.2, 59, 85, 59.5, 13.1, 50.1, 55, 60.1, 13.8, 27, 57.1, 53.1, 42, 16, 88.8, 26.2, 62, 108.5, 92.3, 74.4, 77.3, 96, 88.7, 77.8, 50.7, 61.9, 65.1, 63.5, 64, 88.6, 53.8, 82.1, 78.8, 75.6)), row.names = c(NA, -40L), class = c("tbl_df", "tbl", "data.frame"))
原绘图代码
library(ggplot2) library(ggh4x) ggplot(set1, aes(x=interaction(Tx, Species), y=Size)) + stat_boxplot(geom="errorbar", width = 0.15) + geom_boxplot(show.legend=FALSE, outlier.shape = NA, aes(fill = interaction(Tx, Species))) + geom_jitter(width = 0.1, shape=21, colour="black", fill="grey95", stroke=0.5, size=1) + guides(x="axis_nested") + theme_classic() + theme(axis.title = element_text(face="bold"), text = element_text(family = "serif", size = 12.5))
解决方法
方法一:修改Tx变量的因子水平顺序(最直接)
默认分组排序由因子水平的字母顺序决定,只需重新指定Tx的因子水平,将「Not Exposed」放在首位,后续interaction生成的分组会自动遵循这个顺序,嵌套轴也能正常识别层级:
# 重新指定Tx的因子水平顺序 set1$Tx <- factor(set1$Tx, levels = c("Not Exposed", "Exposed")) # 运行原绘图代码即可得到预期排序的嵌套箱线图 library(ggplot2) library(ggh4x) ggplot(set1, aes(x=interaction(Tx, Species), y=Size)) + stat_boxplot(geom="errorbar", width = 0.15) + geom_boxplot(show.legend=FALSE, outlier.shape = NA, aes(fill = interaction(Tx, Species))) + geom_jitter(width = 0.1, shape=21, colour="black", fill="grey95", stroke=0.5, size=1) + guides(x="axis_nested") + theme_classic() + theme(axis.title = element_text(face="bold"), text = element_text(family = "serif", size = 12.5))
方法二:使用weave_factors配合手动指定顺序(无需修改原数据集)
如果不想修改原数据集的因子结构,可以用weave_factors生成分组,再通过forcats::fct_relevel手动指定排序,同时保留嵌套轴的显示效果:
library(ggplot2) library(ggh4x) library(forcats) ggplot(set1, aes(x=fct_relevel(weave_factors(Tx, Species), # 按需求指定分组顺序 c("Not Exposed:Species1", "Not Exposed:Species2", "Exposed:Species1", "Exposed:Species2")), y=Size)) + stat_boxplot(geom="errorbar", width = 0.15) + geom_boxplot(show.legend=FALSE, outlier.shape = NA, aes(fill = x)) + geom_jitter(width = 0.1, shape=21, colour="black", fill="grey95", stroke=0.5, size=1) + guides(x="axis_nested") + theme_classic() + theme(axis.title = element_text(face="bold"), text = element_text(family = "serif", size = 12.5)) + labs(x = NULL) # 可选:重置X轴标题,避免显示过长的因子名称
内容的提问来源于stack exchange,提问作者arnaudm
相关产品推荐
相关产品推荐

