使用固定纵横比保存时向左对齐的ggplot

我正在构建一个自定义ggplot主题,以标准化我生成的图形的外观。目标比这个最小的示例还要复杂,因此我正在寻找一个通用的解决方案。我有几个主要目标:

  • 我希望所有图形都以相同的大小(宽3000像素,高1500像素)导出。
  • 我想控制绘图面板本身的纵横比。
  • 我想使用textGrobs来包含图形编号。
  • 我希望图像左对齐

我面临的挑战是,当结合这两个约束时,保存的图像将在窗口内的ggplot图形居中,这在默认情况下是有意义的,但在这种情况下看起来很糟糕。

我希望有一个通用的解决方案,在导出时使ggplot面板左对齐。理想情况下,这对于多面图也将类似地工作。

使用gridExtra,gtable,coopplot和egg包中的一种或几种组合似乎应该可以实现某些目标,但是经过几个小时的试验,我有些茫然。有人知道我该怎么做吗?我的代码包含在下面。

这是生成的图像。如您所见,标题在底部是左对齐的,但是ggplot本身是水平居中的。我也希望ggplot图左对齐。

图形输出:https://i.stack.imgur.com/5EM2c.png

library(ggplot2)

# Generate dummy data
x <- paste0("var",seq(1,10))
y <- LETTERS[1:10]
data <- expand.grid(X=x,Y=y)
data$Z <- runif(100,-2,2)

# Generate heatmap with fixed aspect ratio
p1 <- ggplot(data,aes(X,Y,fill= Z)) + 
    geom_tile() +
    labs(title = 'A Heatmap Graph') +
    theme(aspect.ratio = 1)


# A text grob for the footer
figure_number_grob <- grid::textGrob('Figure 10',x = 0.004,hjust = 0,gp = grid::gpar(fontsize = 10,col = '#01A184'))

plot_grid <- ggpubr::ggarrange(p1,figure_number_grob,ncol = 1,nrow = 2,heights = c(1,0.05))

# save it
png(filename = '~/test.png',width = 3000,height = 1500,res = 300,type = 'cairo')
print(plot_grid)
dev.off()
zhangzhuhui 回答:使用固定纵横比保存时向左对齐的ggplot

我确实找到了适合我需要的解决方案,尽管确实有些棘手。

这是核心思想:

  • 在没有固定宽高比的情况下生成图
  • 将图例中的图例作为其自己的组成部分
  • 使用GridExtra的rangingGrob水平组合绘图,分隔符,图例和另一个分隔符
  • 将图的宽度设置为npc(正常父坐标)的一部分,在这种情况下为0.5。这意味着该图将占据输出文件水平空间的50%。
    • 请注意,这与为绘图设置固定的宽高比并不完全相同。如果您知道输出文件的大小,则表示 close 是同一件事,但是轴文本和轴标题的大小会影响面板本身的输出宽高比,因此在关闭时,如果您需要真正固定的宽高比,那不是理想的选择
  • 将间隔物的宽度设置为npc的其余部分(在本例中为0.5),再减去图例的宽度,以使图例在剩余空间中水平居中。

这是我的代码:

library(ggplot2)

# Generate dummy data
x <- paste0("var",seq(1,10))
y <- LETTERS[1:10]
data <- expand.grid(X=x,Y=y)
data$Z <- runif(100,-2,2)

# Generate heatmap WITHOUT fixed aspect ratio. I address this below
p1 <- ggplot(data,aes(X,Y,fill= Z)) + 
    geom_tile() +
    labs(title = 'A Heatmap Graph')

# Extract the legend from our plot
legend = gtable::gtable_filter(ggplotGrob(p1),"guide-box")


plot_output <- gridExtra::arrangeGrob(
    p1 + theme(legend.position="none"),# Remove legend from base plot
    grid::rectGrob(gp=grid::gpar(col=NA)),# Add a spacer
    legend,# Add the legend back
    grid::rectGrob(gp=grid::gpar(col=NA)),# Add a spacer
    nrow=1,# Format plots in 1 row
    widths=grid::unit.c(unit(0.5,"npc"),# Plot takes up half of width
                        (unit(0.5,"npc") - legend$width) * 0.5,# Spacer width
                        legend$width,# Legend width
                        (unit(0.5,"npc") - legend$width) * 0.5)) # Spacer width

# A text grob for the footer
figure_number_grob <- grid::textGrob('Figure 10',x = 0.004,hjust = 0,gp = grid::gpar(fontsize = 10,col = '#01A184'))

plot_grid <- ggpubr::ggarrange(plot_output,figure_number_grob,ncol = 1,nrow = 2,heights = c(1,0.05))

# save it
png(filename = '~/test.png',width = 3000,height = 1500,res = 300,type = 'cairo')
print(plot_grid)
dev.off()

这是输出图像:https://i.stack.imgur.com/rgzFy.png

本文链接:https://www.f2er.com/2442596.html

大家都在问