如何在R中绘制共享单一X轴的垂直排列分组条形图
R语言多分组条形图拼接实现方案
推荐使用patchwork包完成多图无缝拼接,配合ggplot2主题参数即可实现边框贴合、统一坐标轴和图例的需求,完整实现代码如下:
# 加载依赖包 library(tidyverse) library(patchwork) # 读入原始数据(与你提供的代码一致) dataframe2 = read.table(text="sl zone meangpp 1 1 5.4153407 2 2 4.2429236 3 3 4.5719178 4 4 3.1215946 5 5 4.9222054 6 6 3.0384872 7 7 1.9293729 8 8 8.9709741 9 9 7.8904906 10 10 6.6410986 11 12 5.5011823", header=T) dataframe1 = read.table(text="sl zone meangpp 1 1 4.050161 2 2 7.729265 3 3 3.408220 4 4 4.884040 5 5 4.258422 6 6 2.906374 7 7 2.241984 8 8 4.703197 9 9 3.617657 10 10 2.712997 11 12 3.589406", header=T) dataframe3 = read.table(text="sl zone meannpp 1 1 5.4153407 2 2 4.2429236 3 3 4.5719178 4 4 3.1215946 5 5 4.9222054 6 6 3.0384872 7 7 1.9293729 8 8 8.9709741 9 9 7.8904906 10 10 6.6410986 11 12 5.5011823", header=T) dataframe4 = read.table(text="sl zone meannpp 1 1 4.050161 2 2 7.729265 3 3 3.408220 4 4 4.884040 5 5 4.258422 6 6 2.906374 7 7 2.241984 8 8 4.703197 9 9 3.617657 10 10 2.712997 11 12 3.589406", header=T) # 绘制GPP分组条形图 df_gpp <- bind_rows("dataframe1" = dataframe1, "dataframe2" = dataframe2, .id = "groups") p1 <- df_gpp %>% ggplot(aes(x=factor(zone), y=meangpp, fill = groups)) + geom_col(position = position_dodge(0.9), width = 0.8) + # 替换X轴标签为A-L,可根据实际需求修改映射规则 scale_x_discrete(name = "Zone", labels = setNames(LETTERS[1:12], as.character(1:12))) + scale_y_continuous(expand = c(0, 0)) + labs(y = "Mean GPP") + theme_classic() + theme( # 左图右侧边距设为0,和右图无缝贴合 plot.margin = margin(r = 0, unit = "pt"), legend.position = "bottom" ) # 绘制NPP分组条形图 df_npp <- bind_rows("dataframe3" = dataframe3, "dataframe4" = dataframe4, .id = "groups") p2 <- df_npp %>% ggplot(aes(x=factor(zone), y=meannpp, fill = groups)) + geom_col(position = position_dodge(0.9), width = 0.8) + scale_x_discrete(name = "Zone", labels = setNames(LETTERS[1:12], as.character(1:12))) + scale_y_continuous(expand = c(0, 0)) + labs(y = "Mean NPP") + theme_classic() + theme( # 右图左侧边距设为0,和左图无缝贴合 plot.margin = margin(l = 0, unit = "pt"), # 隐藏右图Y轴元素,共用左图坐标轴 axis.title.y = element_blank(), axis.text.y = element_blank(), axis.line.y = element_blank(), axis.ticks.y = element_blank(), legend.position = "bottom" ) # 拼接两张图,统一图例 p1 + p2 + plot_layout( nrow = 1, # 自动合并相同图例,仅展示一次 guides = "collect" ) & theme( legend.position = "bottom", legend.title = element_text("分组") )
关键设置说明
- X轴标签替换:通过
scale_x_discrete的labels参数,将zone的数值映射为A-L的字母标签,可根据实际分类需求调整映射内容 - 边框无缝贴合:调整两个子图的
plot.margin参数,将左图右边距、右图左边距都设为0,实现边框完全贴合 - 统一坐标轴:隐藏右图的Y轴刻度、轴线和标题,两个子图共用同一套X轴分类
- 统一图例:通过
patchwork的guides = "collect"参数自动合并相同图例,仅在底部展示一次
更简洁的分面实现方案
如果不需要单独调整两个子图的细节,也可以将数据整合后用facet_wrap分面实现,代码更简洁:
# 整合所有数据为长格式 df_gpp$type <- "Mean GPP" df_npp$type <- "Mean NPP" names(df_npp)[names(df_npp) == "meannpp"] <- "value" names(df_gpp)[names(df_gpp) == "meangpp"] <- "value" df_all <- bind_rows(df_gpp, df_npp) # 分面绘图 df_all %>% ggplot(aes(x=factor(zone), y=value, fill = groups)) + geom_col(position = position_dodge(0.9), width = 0.8) + scale_x_discrete(name = "Zone", labels = setNames(LETTERS[1:12], as.character(1:12))) + scale_y_continuous(expand = c(0, 0)) + labs(y = "数值") + facet_wrap(~type, nrow = 1, strip.position = "left") + theme_classic() + theme( strip.background = element_blank(), strip.placement = "outside", # 分面间距设为0,实现边框贴合 panel.spacing = unit(0, "pt"), legend.position = "bottom" )
内容的提问来源于stack exchange,提问作者Hallie Sheikh
相关产品推荐
相关产品推荐

