添加geom_text后ggplot2 facet_wrap面板顺序错乱的修复方案
解决ggplot2 facet_wrap面板顺序错乱问题
问题根源
你代码里有两个关键错误:
- 因子重排的代码位置错误,还和
facet_wrap()语法混写,完全没生效 geom_text()用的临时数据框里,SiteName是字符类型,和主数据的因子类型不匹配,导致facet顺序被重置
修正步骤
- 提前处理主数据的因子顺序:在创建ggplot之前,把主数据的
SiteName转成指定顺序的因子,这一步必须单独执行,不能和ggplot语法混在一起。 - 同步临时数据的因子类型:
geom_text()的临时数据框里,SiteName也要转成和主数据完全一致的因子,确保facet分组规则统一。
修改后的完整代码
library(ggplot2) library(tidyverse) # 1. 提前处理主数据的SiteName因子顺序,单独执行这一步 custom_order <- c("DRLC01a", "DRCC01", "NWBB2") NWBB2_5_19_23_XRF_and_DeadRun_fulldata$SiteName <- factor( NWBB2_5_19_23_XRF_and_DeadRun_fulldata$SiteName, levels = custom_order ) # 2. 创建geom_text用的临时数据,同步SiteName的因子类型 text_data <- data.frame( label = c("A", "B", "C"), SiteName = factor(c("DRLC01a", "DRCC01", "NWBB2"), levels = custom_order), x = c(11, 11, 11), y = c(0.5, 0.5, 0.5) ) # 生成图表 ggplot(data = NWBB2_5_19_23_XRF_and_DeadRun_fulldata) + geom_path(mapping = aes(x = Na2O, y = Mid_point_m), size = .80, color = "blue") + geom_point(mapping = aes(x = Na2O, y = Mid_point_m, color = "Na2O"), shape = 3, size = 1.0) + geom_path(mapping = aes(x = MgO, y = Mid_point_m), size = .80, color = "green") + geom_point(mapping = aes(x = MgO, y = Mid_point_m, color = "MgO"), shape = 15, size = 2) + geom_path(mapping = aes(x = K2O, y = Mid_point_m), size = .80, color = "sandybrown") + geom_point(mapping = aes(x = K2O, y = Mid_point_m, color = "K2O"), shape = 17, size = 2) + geom_path(mapping = aes(x = CaO, y = Mid_point_m), size = .80, color = "black") + geom_point(mapping = aes(x = CaO, y = Mid_point_m, color = "CaO"), shape = 19, size = 2) + facet_wrap(~SiteName, ncol = 3, scales = "free_x", strip.position = "top") + theme_minimal() + theme( panel.spacing.x = unit(1.5, "lines"), strip.background = element_blank(), strip.background.x = element_blank(), strip.placement = "outside", legend.box.background = element_blank(), legend.margin = margin(0), legend.key = element_rect(color = "white", fill = "white"), legend.position = "bottom", legend.spacing.x = unit(0, "cm") ) + scale_y_reverse("Depth (m)") + scale_y_continuous( breaks = c(0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10), labels = c("0", "", "1", "", "2", "", "3", "", "4", "", "5", "", "6", "", "7", "", "8", "", "9", "", "10"), expand = c(0, 0) ) + coord_cartesian(ylim = c(9.5, 0)) + labs(y = "Depth (m)", x = "Concentration %") + scale_x_continuous(breaks = seq(0, 12, 1), limits = c(0, 12), position = "bottom", expand = c(0, 0)) + theme( axis.line = element_line(size = 0.5), axis.ticks = element_line(colour = "black"), panel.border = element_rect(color = "black", fill = NA), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.text = element_text(colour = "black"), strip.background = element_rect(fill = "transparent"), legend.position = "bottom", legend.text = element_text(size = 10), legend.title = element_blank(), legend.key = element_rect(fill = "white") ) + scale_color_manual( name = "Element", values = c("Na2O" = "blue", "MgO" = "green", "K2O" = "sandybrown", "CaO" = "black"), breaks = c("Na2O", "MgO", "K2O", "CaO"), labels = c("Na2O", "MgO", "K2O", "CaO"), guide = guide_legend(override.aes = list(shape = c(3, 15, 17, 19))) ) + geom_text(data = text_data, aes(x = x, y = y, label = label))
额外说明
- 原来你把因子重排的代码写在
facet_wrap()后面,还和它用+连接,这不符合R语法,根本不会执行,所以主数据的因子顺序没被修改。 - 临时数据的
SiteName如果是字符型,ggplot会默认按字符排序,覆盖你主数据的因子顺序,所以必须同步转成相同顺序的因子。
内容的提问来源于stack exchange,提问作者Kyle F
相关产品推荐
相关产品推荐

