You need to enable JavaScript to run this app. 基于ggplot2与ggpubr实现双轴图单页合并、共享轴标图例及X轴偏移解决
优惠活动
大模型
产品
解决方案
定价
更多

基于ggplot2与ggpubr实现双轴图单页合并、共享轴标图例及X轴偏移解决

多ggplot合并绘图的需求与问题说明

核心问询

如何将多幅ggplot绘制的图像合并到同一页面,实现公共X、Y轴标签与统一图例的展示效果?

测试准备

已构造两组年度观测数据框:

df1 <- data.frame(
  Date = c('2006-05-01','2006-05-15', '2006-06-01', '2006-06-15', '2006-06-30'),
  obs = c(0.23, 0.22, 0.18, 0.16, 0.17),
  sim = c(0.20, 0.21, 0.22, 0.14, 0.15),
  rain = c(0.76,0, 0.12, 0, 3)
)
df2 <- data.frame(
  Date = c('2007-05-01','2007-05-15', '2007-06-01', '2007-06-15', '2007-06-30'),
  obs = c(0.232, 0.230, 0.21, 0.19, 0.15),
  sim = c(0.22, 0.25, 0.25, 0.15, 0.14),
  rain = c(0.215,0, 0.88, 0.001, 0.05)
)

绘图目标

  • 以Date作为统一X轴
  • 将obs与sim序列映射在左侧主Y轴
  • 将rain以反向柱形图形式映射在右侧次Y轴
  • 最终将两幅年度时间序列图合并至同一页面,实现公共X轴、Y轴标签与统一图例的展示效果

已编写测试代码

首先加载依赖包:

library(ggplot2)
library(ggpubr)

逐幅绘制2006、2007年时间序列图,再调用ggpubr的合并函数排版:

# 绘制2006年子图
Ratio <- max(c(df1$obs, df1$sim), na.rm = TRUE) / max(df1$rain)
cor1 <- cor(df1$obs, df1$sim, use = "pairwise.complete.obs")
RainMax <- max(df1$rain,na.rm = TRUE)
BottomOffset <- 1
plt1 <- ggplot(df1, aes(x=as.Date(Date))) + 
  geom_line(aes(y=obs,color="1")) +
  geom_line(aes(y=sim, color="2")) +
  geom_rect(aes(xmin=as.Date(Date) - 0.1,
                xmax = as.Date(Date) + 0.1,
                ymin = (BottomOffset + RainMax - rain) * Ratio,
                ymax = (BottomOffset + RainMax) * Ratio, 
                color = "3"),
            fill = "red", show.legend = FALSE) + 
  geom_hline(yintercept = (BottomOffset + RainMax) * Ratio, color = "red") +
  geom_hline(yintercept = 0, color = "black") +
  labs(x = "Date", color = "") +
  scale_x_date(breaks = round(seq(min(as.Date(df1$Date)), max(as.Date(df1$Date)), by = 15),1))+
  scale_y_continuous(name = expression('Soil moisture, m'^"3"*' m'^"-3"), 
                     sec.axis = sec_axis(~ BottomOffset + RainMax  - . / Ratio, name = "Precipitation (inch)"),
                     expand = c(0,0)) +
  scale_color_manual(values = c("1" = "blue", "2" = "green", "3" = "red"),
                     labels = c("1" = "In-situ", "2" = "Modeled", "3"= "Precipitation")) +
  theme(axis.line.y.right = element_line(color = "red"), 
        axis.ticks.y.right = element_line(color = "red"),
        axis.text.y.right = element_text(color = "red"),
        axis.title.y.right = element_text(color = "red"),
        axis.line.y.left = element_line(color = "blue"), 
        axis.ticks.y.left = element_line(color = "blue"),
        axis.text.y.left = element_text(color = "blue"),
        axis.title.y.left = element_text(color = "blue"),
        plot.title = element_text(size = 12, face = "bold"),
        axis.text.x = element_text(color = "grey20", size = 8, angle = 30, hjust = .5, vjust = .5, face = "plain"),
        axis.text.y = element_text(color = "grey20", size = 12, angle = 0, hjust = 1, vjust = 0, face = "plain"),  
        axis.title.x = element_text(color = "grey20", size = 12, angle = 0, hjust = .5, vjust = 0, face = "plain"),
        axis.title.y = element_text(color = "grey20", size = 12, angle = 90, hjust = .5, vjust = .5, face = "plain"),
        legend.position = "bottom",
        legend.title=element_text(size=12),
        legend.text=element_text(size=12)) +
  annotate("text", x=min(as.Date(df1$Date))+30, y=0.05, label= (paste0("R = ",format(round(cor1, 2), nsmall = 2))), size=6) +
  ggtitle('Year = 2006')

# 绘制2007年子图
Ratio <- max(c(df2$obs, df2$sim), na.rm = TRUE) / max(df2$rain)
cor1 <- cor(df2$obs, df2$sim, use = "pairwise.complete.obs")
RainMax <- max(df2$rain,na.rm = TRUE)
BottomOffset <- 1
plt2 <- ggplot(df2, aes(x=as.Date(Date))) + 
  geom_line(aes(y=obs,color="1")) +
  geom_line(aes(y=sim, color="2")) +
  geom_rect(aes(xmin=as.Date(Date) - 0.1,
                xmax = as.Date(Date) + 0.1,
                ymin = (BottomOffset + RainMax - rain) * Ratio,
                ymax = (BottomOffset + RainMax) * Ratio,
                color = "3"),
            fill = "red", show.legend = FALSE) + 
  geom_hline(yintercept = (BottomOffset + RainMax) * Ratio, color = "red") +
  geom_hline(yintercept = 0, color = "black") +
  labs(x = "Date", color = "") +
  scale_x_date(breaks = round(seq(min(as.Date(df2$Date)), max(as.Date(df2$Date)), by = 15),1)) +
  scale_y_continuous(name = expression('Soil moisture, m'^"3"*' m'^"-3"), 
                     sec.axis = sec_axis(~ BottomOffset + RainMax  - . / Ratio, name = "Precipitation (inch)"),
                     expand = c(0,0)) +
  scale_color_manual(values = c("1" = "blue", "2" = "green", "3" = "red"),
                     labels = c("1" = "In-situ", "2" = "Modeled", "3"= "Precipitation")) +
  theme(axis.line.y.right = element_line(color = "red"), 
        axis.ticks.y.right = element_line(color = "red"),
        axis.text.y.right = element_text(color = "red"),
        axis.title.y.right = element_text(color = "red"),
        axis.line.y.left = element_line(color = "blue"), 
        axis.ticks.y.left = element_line(color = "blue"),
        axis.text.y.left = element_text(color = "blue"),
        axis.title.y.left = element_text(color = "blue"),
        plot.title = element_text(size = 12, face = "bold"),
        axis.text.x = element_text(color = "grey20", size = 8, angle = 30, hjust = .5, vjust = .5, face = "plain"),
        axis.text.y = element_text(color = "grey20", size = 12, angle = 0, hjust = 1, vjust = 0, face = "plain"),  
        axis.title.x = element_text(color = "grey20", size = 12, angle = 0, hjust = .5, vjust = 0, face = "plain"),
        axis.title.y = element_text(color = "grey20", size = 12, angle = 90, hjust = .5, vjust = .5, face = "plain"),
        legend.position = "bottom",
        legend.title=element_text(size=12),
        legend.text=element_text(size=12)) +
  annotate("text", x = min(as.Date(df2$Date))+30, y=0.05, label= (paste0("R = ", format(round(cor1, 2), nsmall = 2))), size = 6) +
  ggtitle('Year = 2007')

# 合并两幅图
ggarrange(plt1, plt2, ncol = 2, nrow = 1, common.legend = TRUE, legend = "bottom")

运行输出与现存问题

代码运行后的初始效果参考如下:
代码运行初始输出效果
当前存在的问题:

  • 合并后的输出图像出现X轴偏移错位
  • 2006年对应子图中的红色、黑色水平基准线未与X轴刻度对齐
  • 需要定位X轴偏移的根因,掌握可实现共享轴标签、统一图例的多ggplot合并方法

内容的提问来源于stack exchange,提问作者raghav

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.12.04 01:45:01