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

如何在ggplot+patchwork组合图中去除重复的X/Y轴刻度

问题描述

使用R语言ggplot包绘制了4个包含小提琴图、箱线图的子图,通过patchwork包将它们组合为2行2列的组合图。当前每个子图都显示重复的X轴与Y轴刻度,希望调整为仅保留外围坐标轴的样式——即仅左侧列子图保留Y轴、底部行子图保留X轴,其余内部子图隐藏对应重复坐标轴。

解决思路

通过为每个子图单独设置theme元素,精准控制坐标轴的刻度、标签的显示状态,再用patchwork完成组合:

  • 第一行左子图:保留Y轴,隐藏X轴的刻度与标签
  • 第一行右子图:隐藏Y轴和X轴的刻度与标签
  • 第二行左子图:保留Y轴和X轴的所有元素
  • 第二行右子图:隐藏Y轴的刻度与标签,保留X轴
修改后的完整代码
# 绘制子图并调整坐标轴显示
Plot_T_1 <- ggplot(T_1, aes(x = Season, y = Ter_1, fill=Season, color = Season)) +
  geom_violin(adjust=0.75, position = "dodge", alpha = 0.7, fill = "transparent") +
  geom_boxplot(width = 0.05, position = position_dodge(0.75), color = "black", alpha = 0) +
  geom_text(data=count_Ter_1,aes(x = as.factor(Season), y = 0, label = paste("N =", n)))+
  ggtitle("Ter_1") +
  theme_minimal()+
  theme(legend.position = "none",
        # 隐藏X轴刻度和标签
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank())+
  scale_y_continuous(limits = c(0, 0.5), breaks = seq(0, 0.5, by = 0.1))+
  guides(fill = FALSE)

Plot_T_2 <- ggplot(T_2, aes(x = Season, y = Ter_2, fill=Season, color = Season)) +
  geom_violin(adjust=0.75, position = "dodge", alpha = 0.7, fill = "transparent") +
  geom_boxplot(width = 0.05, position = position_dodge(0.75), color = "black", alpha = 0) +
  geom_text(data=count_Ter_2,aes(x = as.factor(Season), y = 0, label = paste("N =", n)))+
  ggtitle("Ter_2") +
  theme_minimal()+
  theme(legend.position = "none",
        # 隐藏Y轴和X轴的刻度、标签
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank())+
  scale_y_continuous(limits = c(0, 0.5), breaks = seq(0, 0.5, by = 0.1))+
  guides(fill = FALSE)

Plot_T_3 <- ggplot(T_3, aes(x = Season, y = Ter_3, fill=Season, color = Season)) +
  geom_violin(adjust=0.75, position = "dodge", alpha = 0.7, fill = "transparent") +
  geom_boxplot(width = 0.05, position = position_dodge(0.75), color = "black", alpha = 0) +
  geom_text(data=count_Ter_3,aes(x = as.factor(Season), y = 0, label = paste("N =", n)))+
  ggtitle("Ter_3") +
  theme_minimal()+
  theme(legend.position = "none")+
  scale_y_continuous(limits = c(0, 0.5), breaks = seq(0, 0.5, by = 0.1))+
  guides(fill = FALSE)

Plot_T_4 <- ggplot(T_4, aes(x = Season, y = Ter_4, fill=Season, color = Season)) +
  geom_violin(adjust=0.75, position = "dodge", alpha = 0.7, fill = "transparent") +
  geom_boxplot(width = 0.05, position = position_dodge(0.75), color = "black", alpha = 0) +
  geom_text(data=count_Ter_4,aes(x = as.factor(Season), y = 0, label = paste("N =", n)))+
  ggtitle("Ter_4") +
  theme_minimal()+
  theme(legend.position = "none",
        # 隐藏Y轴刻度和标签
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank())+
  scale_y_continuous(limits = c(0, 0.5), breaks = seq(0, 0.5, by = 0.1))+
  guides(fill = FALSE)

library(patchwork)

# 组合子图
combined_plot <- Plot_T_1 + Plot_T_2 + Plot_T_3 + Plot_T_4 + 
  plot_layout(nrow = 2, guides = "collect") + 
  plot_annotation(title = "Combined Plot")

print(combined_plot)
补充说明

如果希望连坐标轴的轴线也隐藏,可以在对应子图的theme中添加axis.line.x = element_blank()或axis.line.y = element_blank(),根据需求调整即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 20:17:49