You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将两个独立折线图合并为堆叠式折线图(保留原可视化效果)

合并堆叠式折线图方案(基于ggplot2与patchwork)

现有如下数据集:

# A tibble: 18 × 3
     std  near   off
   <dbl> <dbl> <dbl>
 1   0.3 0.849 0.904
 2   0.4 0.850 0.916
 3   0.5 0.859 0.924
 4   0.6 0.872 0.930
 5   0.7 0.885 0.936
 6   0.8 0.891 0.938
 7   0.9 0.902 0.938
 8   1   0.897 0.938
 9   1.1 0.902 0.938
10   1.2 0.898 0.937
11   1.3 0.897 0.936
12   1.4 0.898 0.934
13   1.5 0.896 0.932
14   1.6 0.894 0.928
15   1.7 0.888 0.927
16   1.8 0.890 0.924
17   1.9 0.881 0.917
18   2   0.876 0.914

原代码生成两个独立折线图,分别展示near/off列随std的变化,且各自标记特定点。要合并为堆叠式图并保留原有可视化效果,推荐使用patchwork包实现,步骤如下:

1. 安装并加载必要包

除原代码中的包外,需新增patchwork用于图形拼接:

library(ggplot2)
library(dplyr)
library(tibble)
library(data.table)
library(patchwork) # 新增图形拼接包

2. 数据读取与预处理

保留原数据处理逻辑:

wd <- "path/"
df <- fread(paste0(wd, "r2_la.csv"))

# 格式化数据(原逻辑保留)
df$near <- format(df$near, digits = 4) %>% as.numeric()
df$off <- format(df$off, digits = 4) %>% as.numeric()
df$std <- format(df$std, digits = 2) %>% as.numeric()

# 准备标记点数据
g1 <- subset(df, std == 0.8)
g2 <- subset(df, std == 1)

3. 定义统一主题

提取原代码中的重复主题设置,避免冗余:

common_theme <- theme(
  plot.title = element_text(color = "black", size = 17, face = "bold", hjust = 0.5),
  plot.subtitle = element_text(size = 10, face = "bold", hjust = 0.5, color = "black"),
  plot.caption = element_text(face = "italic", hjust = 0), 
  panel.background = element_rect(fill = 'transparent'), 
  axis.line.x = element_line(size = 1, linetype = "solid", colour = "lightgrey"),
  axis.line.y = element_line(size = 1, linetype = "solid", colour = "lightgrey"),
  axis.title.x = element_text(size = 20),
  axis.title.y = element_text(size = 20),
  axis.text = element_text(size = 17, color = "black")
)

4. 绘制单个子图

保留原有的折线、点样式及标记逻辑:

# 绘制near子图
p_near <- ggplot(df, aes(x = std, y = near)) + 
  geom_line(color = "midnightblue", linewidth = 0.3, group = 1) +
  geom_point(shape = 24, fill = "midnightblue", size = 4, alpha = 0.3) +
  geom_point(data = g1, shape = 24, fill = "midnightblue", size = 4) +
  geom_text(data = g1, label = "0.8", vjust = -0.8, hjust = 1) +
  scale_x_continuous(breaks = seq(0.3, 2.1, 0.2)) +
  common_theme +
  xlab("PSF width in units of pixels") + 
  labs(y = expression("R"^2), title = "Near") # 新增标题区分子图

# 绘制off子图
p_off <- ggplot(df, aes(x = std, y = off)) + 
  geom_line(color = "springgreen4", linewidth = 0.3, group = 1) +
  geom_point(shape = 21, fill = "springgreen4", size = 4, alpha = 0.3) +
  geom_point(data = g2, shape = 21, fill = "springgreen4", size = 4) +
  geom_text(data = g2, label = "1", vjust = -0.8, hjust = -0.1) +
  scale_x_continuous(breaks = seq(0.3, 2.1, 0.2)) +
  common_theme +
  xlab("PSF width in units of pixels") + 
  labs(y = expression("R"^2), title = "Off") # 新增标题区分子图

5. 堆叠拼接图形

使用patchwork将两个子图垂直堆叠,共享x轴并优化布局:

# 垂直堆叠,仅底部子图显示x轴标签,统一布局
p_near + p_off +
  plot_layout(ncol = 1, guides = "collect") + # 垂直排列,合并重复图例
  plot_annotation(tag_levels = 'A') # 可选:添加子图标签(A/B)

关键说明

  • patchwork的plot_layout(ncol=1)实现垂直堆叠,guides="collect"自动合并重复图例(若后续添加图例可生效)
  • 完全保留原代码中所有样式细节:点形状、颜色、透明度、标记点及文本标注
  • 子图可独立适配y轴范围(ggplot会自动根据数据调整,无需额外设置)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 06:27:35