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

如何移除组合ggplot2图中y轴的多余线段?

解决ggplot2组合图中反转y轴后出现的多余线段问题

问题背景

使用ggplot2绘制多幅图形,通过gridExtra包的grid.arrange()将图形组合为复合图后,左上角的yplot和右下角的xplot柱状图在应用scale_y_reverse()后,y轴底部出现多余线段,多次调整绘图边距均无法解决。

解决方案

问题核心是先定义scale_y_continuous()再调用scale_y_reverse()时,前者的expand参数会被后者覆盖,而scale_y_reverse()默认的轴扩展值会导致线段溢出。只需将扩展参数直接整合到反转轴的设置中,即可解决该问题;若需进一步优化,可通过主题设置隐藏冗余轴元素。

修改后的完整代码

library(ggplot2)
library(gridExtra)
library(ggpubr) # 用于rotate()函数

# 生成数据
set.seed(444)  

var1 <- rnorm(100, mean = 50, sd = 10)  
var2 <- runif(100, min = 0, max = 100)
categories <- c("Category A", "Category B", "Category C")
var3 <- sample(categories, 100, replace = TRUE)

jesper_data_random <- data.frame(
  Forces = var1,
  Separation = var2,
  Coloring = var3
)

# 绘制主图
main_plot <- jesper_data_random %>%
  ggplot(aes(
    x = Separation,
    y = Forces,
    colour=Coloring
  )) +
  geom_point() +
  labs(x="Distance", y="Force")+
  scale_x_continuous(position="top",
                     expand = expansion(mult = c(0.005, 0.005))
                     ) +
  scale_y_continuous(position="right",
                     expand = expansion(mult = c(0.005, 0.005))
                     ) +
  theme(
    plot.margin = margin(0,0,-70,-70)
    ) +
  theme_bw() +
  coord_cartesian(clip = "off") +
  rremove("legend")

# 绘制xplot(右下直方图)
xplot <- ggplot(jesper_data_random, aes(x = Separation)) +
  geom_histogram(aes(fill = Coloring, color = Coloring), bins = 30) +
  scale_x_continuous(
    expand = expansion(mult = c(0.005, 0.005))
  ) +
  theme_classic() +
  theme(
    axis.ticks.x = element_blank(),
    axis.title.x = element_blank(),
    axis.text.x = element_blank(),
    plot.margin = margin(0,40,0,-29)
  ) +
  # 将expand参数整合到scale_y_reverse中,替代之前的scale_y_continuous
  scale_y_reverse(expand = expansion(mult = c(0.005, 0.005))) +
  rremove("legend")

# 绘制yplot(左上直方图)
yplot <- ggplot(jesper_data_random, aes(x = Forces)) +
  geom_histogram(aes(fill = Coloring, color = Coloring), bins = 30) +
  scale_x_continuous(
    expand = expansion(mult = c(0.005, 0.005))
  ) +
  theme_classic() +
  theme(
    axis.ticks.y = element_blank(),
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    plot.margin = margin(35,0,-23,0)
  ) +
  rotate() +
  # 将expand参数整合到scale_y_reverse中
  scale_y_reverse(expand = expansion(mult = c(0.005, 0.005))) +
  rremove("legend")

# 空白图用于布局
blank_plot <- ggplot() + 
  theme_void() + 
  theme(plot.background = element_blank(),
        panel.grid = element_blank(),
        plot.margin = margin(0,0,0,0))

# 组合图形
grid.arrange(grobs = list(yplot, main_plot, blank_plot, xplot), ncol = 2)

额外优化建议

如果仍有细微的冗余线段,可在对应图形的theme()中添加以下设置,隐藏不需要的轴线条:

  • 对于xplot:axis.line.y = element_blank()(若不需要y轴线条)
  • 对于yplot:axis.line.y = element_blank()(旋转后原x轴变为y轴,可根据需求隐藏)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 22:09:56