R - 如何为复杂的ggplot图像分配屏幕空间

11
我正在尝试编写一个脚本,在单个图像中生成四个不同的绘图。具体来说,我想尽可能精确地重新创建这个图形:

Complex Plot

我的当前脚本可以生成类似于此的四个绘图,但我无法分配屏幕空间。我希望能够:
  1. 修改绘图的高度和宽度,以使所有功能拥有统一的宽度,其中一个比其余的要高得多
  2. 通过坐标定义图例的位置以有效利用屏幕空间
  3. 根据需要明确修改图像的整体形状(也许我在某些时候需要更接近正方形的形状)
生成一些数据以进行绘制
pt_id = c(1:279) # DEFINE PATIENT IDs
smoke = rbinom(279,1,0.5) # DEFINE SMOKING STATUS
hpv = rbinom(279,1,0.3) # DEFINE HPV STATUS
data = data.frame(pt_id, smoke, hpv) # PRODUCE DATA FRAME

添加解剖部位数据

data$site = sample(1:4, 279, replace = T)
data$site[data$site == 1] = "Hypopharynx"
data$site[data$site == 2] = "Larynx"
data$site[data$site == 3] = "Oral Cavity"
data$site[data$site == 4] = "Oropharynx"
data$site_known = 1  # HACK TO FACILITATE PRODUCING BARPLOTS

添加突变频率数据

data$freq = sample(1:1000, 279, replace = F)

定义条形图

require(ggplot2)
require(gridExtra)
bar = ggplot(data, aes(x = pt_id, y = freq)) + geom_bar(stat = "identity") +     theme(axis.title.x = element_blank(), axis.ticks.x = element_blank(), axis.text.x = element_blank()) + ylab("Number of Mutations")
# DEFINE BINARY PLOTS
smoke_status = ggplot(data, aes(x=pt_id, y=smoke, fill = "red")) + geom_bar(stat="identity") + theme(legend.position = "none", axis.title.x = element_blank(), axis.ticks.x = element_blank(), axis.text.x = element_blank()) + ylab("Smoking Status")
hpv_status = ggplot(data, aes(x=pt_id, y = hpv, fill = "red")) + geom_bar(stat="identity") + theme(legend.position = "none", axis.title.x = element_blank(), axis.ticks.x = element_blank(), axis.text.x = element_blank()) + ylab("HPV Status")
site_status = ggplot(data, aes(x=pt_id, y=site_known, fill = site)) +     geom_bar(stat="identity")

生成四个图表并将它们放在一起。
grid.arrange(bar, smoke_status, hpv_status, site_status, nrow = 4)

我怀疑实现这些任务所需的函数已经包含在ggplot2和gridExtra中,但我还没有找到如何使用它们。如果我的代码过于冗长,或者有更简单、更优雅的方法来完成我已经完成的工作,请随时评论。


对于第一个问题,您可以指定一个包含6行的布局,然后将顶部图形放在前3行中。请参见此处的multiplot函数[http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)/]。 - RHA
3
通常需要使用grid包中的工具手动完成此类事情。我建议先查看ggplot_buildggplot_gtable以将绘图分解为组件grobs,然后您可能需要构建自己的布局(grid.layout),并在特定的视口中绘制每个grob。请注意,不要改变原文意思。 - joran
您也可以查看gtable wiki - Henrik
1个回答

11

以下是按照你所描述的布局步骤:

1) 将图例提取为单独的grob(图形对象)。我们可以将图例与绘图分开布局。

2) 让四个图的左边缘对齐,使左边缘和x轴标尺对齐。用于实现此功能的代码来自此 SO答案。该答案有一个函数可对任意数量的图进行对齐,但当我还想改变分配给每个图的比例空间时,我无法让其工作,因此我最终通过单独调整每个图来完成它。

3) 使用grid.arrangearrangeGrob布局图和图例。heights参数将不同比例的总垂直空间分配给每个图。我们还使用widths参数将水平空间分配给一个宽列中的图和另一个窄列中的图例。

4) 以所需的任何大小绘制到设备。这是如何获得特定形状或纵横比。

library(gridExtra)
library(grid)

# Function to extract the legend from a ggplot graph as a separate grob
# Source: https://dev59.com/fWcs5IYBdhLWcg3w1XbZ#12539820
get_leg = function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  legend
}

# Get legend as a separate grob
leg = get_leg(site_status)

# Add a theme element to change the plot margins to remove white space between the plots
thm = theme(plot.margin=unit(c(0,0,-0.5,0),"lines"))

# Left-align the four plots 
# Adapted from: https://dev59.com/ImYr5IYBdhLWcg3w6eQ3#13295880
gA <- ggplotGrob(bar + thm)
gB <- ggplotGrob(smoke_status + thm)
gC <- ggplotGrob(hpv_status + thm)
gD <- ggplotGrob(site_status + theme(plot.margin=unit(c(0,0,0,0), "lines")) + 
                  guides(fill=FALSE))

maxWidth = grid::unit.pmax(gA$widths[2:5], gB$widths[2:5], gC$widths[2:5], gD$widths[2:5])
gA$widths[2:5] <- as.list(maxWidth)
gB$widths[2:5] <- as.list(maxWidth)
gC$widths[2:5] <- as.list(maxWidth)
gD$widths[2:5] <- as.list(maxWidth)

# Lay out plots and legend
p = grid.arrange(arrangeGrob(gA,gB,gC,gD, heights=c(0.5,0.15,0.15,0.21)),
                 leg, ncol=2, widths=c(0.8,0.2))

接下来,您可以通过设置输出设备的参数来确定最终图形的形状或宽高比。(在创建底层图之前,您可能需要调整字体大小,以便将最终布局呈现为所需的样式。)以下是从 RStudio 图形窗口直接保存的 PNG 图片。以下是将图片保存为 PDF 文件的方法(但是您可以使用许多其他“设备”(例如 png、jpeg 等)以不同的格式进行保存):

pdf("myPlot.pdf", width=10, height=5)
p
dev.off()

在此输入图像描述

你还询问了更高效的代码。你可以创建一个包含你多次使用的绘图元素的列表,然后只需将列表对象的名称添加到每个绘图中即可。例如:

my_gg = list(geom_bar(stat="identity", fill="red"),
             theme(legend.position = "none", 
                   axis.title.x = element_blank(), 
                   axis.ticks.x = element_blank(), 
                   axis.text.x = element_blank()),
                   plot.margin = unit(c(0,0,-0.5,0), "lines"))

smoke_status = ggplot(data, aes(x=pt_id, y=smoke)) + 
                  labs(y="Smoking Status") +
                  my_gg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接