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

如何在ggplot中设置一致的绘图边距与尺寸?

我有以下两段ggplot代码:

第一段代码(生成PDF密度图):

v <- melt(tvalB) 
data1 <- as.data.frame(cbind(tval, v$value)) 
sizez <- 14 
ggplot(data = data1, aes(x = T-value)) + 
  geom_density(aes(tval, fill = "Actual"), alpha = 0.5, adjust=8) + 
  geom_density(aes(V2, fill = "Simulation"), alpha = 0.5, adjust=8) + 
  scale_colour_manual("", breaks = c("Actual", "Simulation"), values = c("red", "blue")) + 
  scale_y_continuous("density") + 
  scale_x_continuous("t(alpha)", limits = c(-6.5,6.5)) + 
  labs(title ="") + 
  theme(plot.title = element_text(hjust = 0.5)) + 
  guides(fill=guide_legend(title=NULL)) + 
  theme(legend.title=element_text(size=sizez+1),
        legend.text=element_text(size=sizez), 
        axis.text=element_text(size=sizez-1), 
        axis.title=element_text(size=sizez), 
        legend.key = element_rect(size = 0.05), 
        legend.key.height = unit(1.25, "cm"), 
        legend.key.width = unit(1.25, "cm"))

第二段代码(生成CDF图,代码重复为笔误,已修正):

data1 %>% 
  gather(key, value) %>% 
  ggplot(aes(value, color=key)) + 
  stat_ecdf(size=0.8)+ 
  xlim(-4.5,4.5) + 
  labs(x = "t(alpha)", y = "probability", color=NULL) + 
  scale_colour_discrete(labels = c("Actual","Simulation")) + 
  theme(legend.title=element_text(size=sizez+1), 
        legend.text=element_text(size=sizez), 
        axis.text=element_text(size=sizez-1), 
        axis.title=element_text(size=sizez), 
        legend.key = element_rect(size = 4), 
        legend.key.height = unit(1.3, "cm"), 
        legend.key.width = unit(1.3, "cm"))

它们分别生成了CDF图和PDF图,从图中可见两者的灰色绘图区域高度不一致。请问是否可以预定义绘图边距,使它们在报告中并排时高度等属性完全一致?感谢解答!

解决ggplot图并排时绘图区域高度不一致的问题

当然可以!两个图的绘图区域高度不一致,通常是因为图例尺寸、坐标轴标签占用空间或者默认边距的细微差异导致的。下面提供几种可靠的解决方法:

方法1:统一所有主题参数(包括边距)

首先把两个图的主题参数完全统一,尤其是图例尺寸、plot.margin(绘图边距)和坐标轴相关设置,确保非绘图区域的占用空间完全一致。

修改后的代码示例:

# 统一定义通用主题,避免重复代码
sizez <- 14
common_theme <- theme(
  legend.title = element_text(size = sizez + 1),
  legend.text = element_text(size = sizez),
  axis.text = element_text(size = sizez - 1),
  axis.title = element_text(size = sizez),
  # 统一图例尺寸,消除两个图的图例差异
  legend.key = element_rect(size = 0.05),
  legend.key.height = unit(1.25, "cm"),
  legend.key.width = unit(1.25, "cm"),
  # 统一绘图边距,四个值对应上、右、下、左的边距
  plot.margin = margin(10, 10, 10, 10, unit = "pt"),
  plot.title = element_text(hjust = 0.5)
)

# 第一段:密度图(PDF)
v <- melt(tvalB) 
data1 <- as.data.frame(cbind(tval, v$value)) 
p_density <- ggplot(data = data1, aes(x = tval)) + 
  geom_density(aes(fill = "Actual"), alpha = 0.5, adjust=8) + 
  geom_density(aes(x = V2, fill = "Simulation"), alpha = 0.5, adjust=8) + 
  scale_fill_manual("", breaks = c("Actual", "Simulation"), values = c("red", "blue")) + 
  scale_y_continuous("density") + 
  scale_x_continuous(expression(t(alpha)), limits = c(-6.5,6.5)) + 
  labs(title = "") + 
  guides(fill=guide_legend(title=NULL)) +
  common_theme

# 第二段:CDF图
p_cdf <- data1 %>% 
  gather(key, value) %>% 
  ggplot(aes(value, color=key)) + 
  stat_ecdf(size=0.8)+ 
  xlim(-4.5,4.5) + 
  labs(x = expression(t(alpha)), y = "probability", color=NULL) + 
  scale_colour_discrete(labels = c("Actual","Simulation")) + 
  common_theme

注:我修正了密度图中aes(x = T-value)的错误(应为x = tval),并将颜色映射从scale_colour_manual改为scale_fill_manual(匹配你使用的fill美学)。

方法2:使用patchwork包强制对齐绘图区域

如果统一主题后仍有差异,推荐用patchwork包——它专门用于ggplot图的排版,自带完美的对齐功能:

# 先安装包(如果未安装)
# install.packages("patchwork")
library(patchwork)

# 并排展示两个图,自动垂直对齐绘图区域
p_density + p_cdf + plot_layout(ncol = 2, align = "v")

align = "v"表示垂直对齐,不管图例、坐标轴标签的差异,都能确保两个图的绘图区域高度完全一致。

方法3:手动调整grob对象的高度

如果你不想额外安装包,可以把ggplot对象转换成grob,然后强制设置相同的绘图区域高度:

library(grid)

# 转换成grob对象
g_density <- ggplotGrob(p_density)
g_cdf <- ggplotGrob(p_cdf)

# 找到绘图区域的索引(通常是第6个元素)
panel_index <- which(g_density$layout$name == "panel")
# 强制设置两个图的绘图区域高度一致
g_cdf$heights[panel_index] <- g_density$heights[panel_index]

# 绘制对齐后的图
grid.newpage()
grid.draw(cbind(g_density, g_cdf, size = "last"))

这三种方法都能解决你的问题,其中patchwork包的方法最简便可靠,推荐优先使用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 07:16:20