如何调整ggplot2中多因子X轴的Pretreatment类别顺序
调整ggplot2中Pretreatment因子的X轴顺序
我用R语言的ggplot2绘制了不同因子水平均值的可视化图表,当前分类因子Pretreatment的X轴是默认字母顺序Discs-Mortar-None,需要将其调整为自定义顺序None-Mortar-Discs。以下是当前使用的数据、代码及图表:
当前数据
scenedesmus <- structure(list(Temperature = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("20", "30", "40"), class = "factor"), Time = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L), .Label = c("0.5", "1", "2"), class = "factor"), Ratio = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 2L, 2L, 3L, 3L, 1L, 1L, 3L, 3L, 1L, 1L, 2L, 2L), .Label = c("3", "6", "12"), class = "factor"), Pretreatment = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 1L, 1L), .Label = c("None", "Mortar", "Discs"), class = "factor"), PRY = c(7.10618979550317, 6.99107348052751, 9.81654489395678, 10.0937678454159, 15.8872899104855, 16.5147395153748, 15.6085073784574, 15.8904572330355, 9.85155639002801, 10.3291566375677, 9.81557388225615, 10.1774212169006, 12.0972576247432, 11.1350551614397, 14.7591913822601, 14.8846506719242, 9.47697977090569, 10.8328555963545), CRY = c(12.9913707456184, 13.2037056981015, 14.6223886369729, 14.4156689100426, 20.8510599220091, 21.1334682925674, 20.7517385553227, 20.3784601114164, 13.1903022986714, 12.7481614338955, 14.3799945987187, 15.1548695641213, 16.3653561008515, 17.3492383422838, 22.4414097199122, 22.4340213280367, 14.0895227253865, 16.0388931794408), PCR = c(0.546993072143667, 0.529478135939726, 0.671336615218633, 0.700194205929921, 0.761941597689038, 0.781449560798454, 0.752154203217537, 0.779767320305684, 0.746878742196859, 0.810246770966047, 0.682585366418063, 0.671561122571146, 0.739199168670324, 0.641818098394623, 0.657676659642481, 0.663485625438106, 0.672626032522028, 0.675411668071984)), row.names = c(NA, -18L), class = c("tbl_df", "tbl", "data.frame"))
当前代码
scenedesmus %>% mutate(across(Temperature:Pretreatment, as.character)) %>% pivot_longer(Temperature:Pretreatment) %>% mutate(value = factor(value, levels(factor(value))[ c(10:12, 6, 9, 3, 5, 7:8, 1:2, 4)])) %>% ggplot(aes(value, PCR, group = name, color = name)) + geom_point(stat = 'summary', fun = mean) + geom_hline(yintercept = mean(scenedesmus$PCR, na.rm=TRUE),linetype='dotted', col = 'grey', size=1.5) + stat_summary(fun.data="mean_sdl", fun.args = list(mult = 1), geom="errorbar", width = 0.2, size=1, alpha = 0.5) + scale_y_continuous(limits = c(0.5, 0.9)) + geom_line(stat = 'summary', fun = mean, size=1) + facet_grid(~name, scales = 'free_x', switch = 'x') + scale_color_manual(values = c("#0072B2", "#D55E00", "#CC79A7","#009E73")) + coord_cartesian(clip = 'off') + geom_vline(data = data.frame(a = 0.4, name = 'Pretreatment'), aes(xintercept = a)) + theme_classic(base_size = 20) + theme(strip.placement = 'outside', legend.position="none", strip.background = element_blank(), axis.title.x = element_blank(), panel.grid.major.x = element_blank(), panel.spacing.x = unit(0, 'mm'), axis.ticks = element_line(), axis.line.x = element_line(), axis.title.y = element_text(size=20, face="bold"), strip.text = element_text(face = "bold")) + labs(y = "Protein carbohydrate ratio")
当前图表

解决方案
原代码通过索引指定value的levels不够精准,无法针对性调整Pretreatment的顺序。可以在pivot_longer之后,根据name字段单独设置每个因子的水平顺序:
修改后的完整代码:
scenedesmus %>% mutate(across(Temperature:Pretreatment, as.character)) %>% pivot_longer(Temperature:Pretreatment) %>% # 针对每个因子单独设置水平顺序 mutate(value = case_when( name == "Pretreatment" ~ factor(value, levels = c("None", "Mortar", "Discs")), name == "Temperature" ~ factor(value, levels = c("20", "30", "40")), name == "Time" ~ factor(value, levels = c("0.5", "1", "2")), name == "Ratio" ~ factor(value, levels = c("3", "6", "12")), TRUE ~ factor(value) )) %>% ggplot(aes(value, PCR, group = name, color = name)) + geom_point(stat = 'summary', fun = mean) + geom_hline(yintercept = mean(scenedesmus$PCR, na.rm=TRUE),linetype='dotted', col = 'grey', size=1.5) + stat_summary(fun.data="mean_sdl", fun.args = list(mult = 1), geom="errorbar", width = 0.2, size=1, alpha = 0.5) + scale_y_continuous(limits = c(0.5, 0.9)) + geom_line(stat = 'summary', fun = mean, size=1) + facet_grid(~name, scales = 'free_x', switch = 'x') + scale_color_manual(values = c("#0072B2", "#D55E00", "#CC79A7","#009E73")) + coord_cartesian(clip = 'off') + geom_vline(data = data.frame(a = 0.4, name = 'Pretreatment'), aes(xintercept = a)) + theme_classic(base_size = 20) + theme(strip.placement = 'outside', legend.position="none", strip.background = element_blank(), axis.title.x = element_blank(), panel.grid.major.x = element_blank(), panel.spacing.x = unit(0, 'mm'), axis.ticks = element_line(), axis.line.x = element_line(), axis.title.y = element_text(size=20, face="bold"), strip.text = element_text(face = "bold")) + labs(y = "Protein carbohydrate ratio")
说明
- 用
case_when对每个因子单独配置水平顺序,确保Pretreatment按None-Mortar-Discs排列,其他因子保持原有合理顺序。 - 这种方式比索引指定更直观,避免因因子水平数量变化导致的索引失效问题。
内容的提问来源于stack exchange,提问作者David Moldes
相关产品推荐
相关产品推荐

