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

时间:2015-12-10 21:07:10

标签: r plot ggplot2 gridextra

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

Complex Plot

我当前的脚本生成了四个类似于这些的图,但我无法弄清楚如何相应地分配屏幕空间。我想:

  1. 修改地块的高度和宽度,使四个地方都有均匀的宽度,一个高于其他高度均匀的高度
  2. 通过坐标定义图例的位置,以便我可以有效地使用屏幕空间
  3. 根据需要明确修改我的图像的整体形状(也许我需要在某个时候更接近方形)
  4. 生成一些数据

    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)
    

    DEFINE BARPLOT

    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中,但我无法弄清楚如何。此外,如果我的任何代码过于冗长或者有一种更简单,更优雅的方式来完成我已经完成的工作 - 请随时对此进行评论。

1 个答案:

答案 0 :(得分:11)

以下是获取您描述的布局的步骤:

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

2)左对齐四个图的边,使左边和x刻度正确对齐。执行此操作的代码来自this SO answer。这个答案有一个函数可以对齐任意数量的图,但是当我还想改变分配给每个图的比例空间时,我无法使它工作,所以我最终做了#34漫长的"通过分别调整每个图。

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://stackoverflow.com/a/12539820/496488
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://stackoverflow.com/a/13295880/496488
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文件(但还有许多其他&#34;设备&#34;您可以使用(例如,png,jpeg等)以不同的格式保存):

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

enter image description here

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

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