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

使用Patchwork的plot_layout()合并轴:岩心图像与测井曲线绘图问题

问题描述

我使用ggplot2、magick和patchwork实现了程序化绘制沉积物岩心测井图,左侧y轴已对齐缩放后的岩心照片,但目前无法移除图像与数据图之间的深度y轴。尝试使用patchwork的plot_layout(axes = "collect_y")但未生效,由于是混合缩放图像与绘图的特殊场景,想询问问题出在哪里,以及R中是否有更好的实现方法。

当前效果:左侧显示岩心图像,右侧是两个分面的数据图,图像与第一个数据图之间存在一个深度y轴,且每个数据图左侧都有独立的y轴。
目标效果:仅在岩心图像右侧保留一个统一的深度y轴,数据图之间不再显示重复的y轴。

示例数据已提供。

原代码:

# Load Required Packages
library(ggplot2)
library(patchwork)
library(magick)

# Read in plot data
data <- read.csv(file = "Data/data.csv")

# Plotting ----
## L* and b* in log format
logplot <- ggplot(data) +
  geom_path(aes(x=value, y=depth)) +
  ylim(1.2,0) +
  scale_y_reverse() +
  labs(title='Core 10004_6A_1_2') +
  facet_grid(cols = vars(metric), scales = 'free_x')

## Read in RGB plot image
plotimg <- "Data/10004_6A_1_2.png"
image<-image_read(plotimg)
raster<-as.raster(image)

## Plot Core Image
image <- ggplot(data,aes(x,depth)) + 
  scale_y_reverse() +
  ylim(1.2,0) +
  xlim(0,0.044) +
  coord_flip() + 
  coord_fixed(expand = TRUE) +
  annotation_raster(raster, 0, 0.044, -1.182, 0)
image

## Patchwork Plots
(image | logplot) + plot_layout(axes = "collect_y") & ylim(1.2, 0)

问题分析
  1. 坐标轴类型不匹配:绘制岩心图像时使用了coord_flip(),导致depth对应的坐标轴从y轴转为x轴,这使得patchwork的axes = "collect_y"无法识别并合并图像与数据图的depth轴——两者的轴类型(x vs y)不一致,自然无法生效。
  2. 图像绘图未清理坐标轴:左侧图像的ggplot默认保留了坐标轴元素,即使视觉上不明显,也会干扰拼接后的轴布局逻辑。

解决方案

步骤1:修正岩心图像的坐标轴逻辑

移除coord_flip(),直接将depth作为y轴,用theme_void()彻底清除所有坐标轴元素,同时设置固定坐标比例保证图像与depth轴对齐。

步骤2:优化数据图的轴合并

确保数据图的depth轴统一,利用patchwork的轴收集功能,只保留最左侧的一个y轴。


修改后的完整代码
# 加载所需包
library(ggplot2)
library(patchwork)
library(magick)

# 读取数据
data <- read.csv(file = "Data/data.csv")

# 绘制数据图(L*和b*)
logplot <- ggplot(data) +
  geom_path(aes(x = value, y = depth)) +
  scale_y_reverse(limits = c(1.2, 0)) +  # 合并ylim与scale_y_reverse,简化代码
  labs(title = 'Core 10004_6A_1_2') +
  facet_grid(cols = vars(metric), scales = 'free_x')

# 读取并处理岩心图像
plotimg <- "Data/10004_6A_1_2.png"
image <- image_read(plotimg)
raster <- as.raster(image)

# 绘制岩心图像(修正坐标轴逻辑)
image_plot <- ggplot() +
  # 用depth范围(0到1.2)作为y轴,x轴匹配图像宽度
  annotation_raster(raster, xmin = 0, xmax = 0.044, ymin = 0, ymax = 1.2) +
  scale_y_reverse(limits = c(1.2, 0)) +
  scale_x_continuous(limits = c(0, 0.044)) +
  coord_fixed(expand = FALSE) +  # 锁定图像比例,避免拉伸
  theme_void()  # 移除所有坐标轴、网格等冗余元素

# 拼接图像与数据图
(image_plot | logplot) + 
  plot_layout(axes = "collect_y", widths = c(1, 4)) +  # 收集y轴,调整左右宽度比例
  theme(plot.title = element_text(hjust = 0.5))  # 标题居中(可选优化)

关键说明
  • theme_void():彻底清除左侧图像的所有坐标轴元素,避免干扰拼接后的轴布局。
  • 移除coord_flip():让depth始终作为y轴,确保与右侧数据图的轴类型一致,此时plot_layout(axes = "collect_y")才能正确合并轴,只保留最左侧的一个y轴。
  • coord_fixed(expand = FALSE):保证岩心图像的宽高比例固定,与depth轴完全对齐,不会出现拉伸错位。

替代方案(可选)

如果需要更精细的分面轴控制,可以使用ggh4x包的facet_grid2()单独设置每个分面的坐标轴显示;或者用gridExtra包进行底层网格布局,但上述patchwork的修改已经能满足需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 11:50:56