【问题标题】:R: ggplot2 make two geom_tile plots have equal heightR:ggplot2 使两个 geom_tile 图具有相等的高度
【发布时间】:2014-06-12 16:04:47
【问题描述】:

我有两个 ggplot geom_tile 图,我使用 gridExtra 库中的 grid.arrange() 将它们放在一起。这是结果

library(ggplot2)
library(plyr)

p3 <- ggplot2(...)
p4 <- ggplot2(...)
grid.arrange(p3, p4, ncol=2)

我想得到它,以便两个地块具有相同的高度并对齐。我用 gtable 摆弄了一下直接改变尺寸,但这似乎没有成功。

gA <- ggplot_gtable(ggplot_build(p3))
gB <- ggplot_gtable(ggplot_build(p4))
maxHeight <- unit.pmax(gA$heights[2:3], gB$heights[2:3])
gA$heights[2:3] <- maxHeight
gB$heights[2:3] <- maxHeight
grid.arrange(gA, gB, ncol=2)

错误信息:

Error in grid.Call.graphics(L_setviewport, pvp, TRUE) : 
non-finite location and/or size for viewport

有什么想法吗?

--- 可重现的例子---

library(ggplot2)
library(plyr)

temp_plot <- structure(list(Sample = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L), 
    .Label = c("1", "2", "3", "4", "5", "6", "7", "8"), class = "factor"), 
    Gene = structure(c(1L, 9L, 21L, 22L, 1L, 7L, 8L, 1L, 2L, 17L, 18L, 1L, 3L, 4L, 16L, 5L, 6L, 19L, 20L, 1L, 10L, 11L, 12L, 13L, 2L, 3L, 4L, 5L, 6L, 14L, 15L), 
    .Label = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22"), class = "factor"), 
    Effect = c("fusion", "missense", "missense", "missense", "fusion", "missense", "missense", "fusion", "frameshift", "missense", "nonsense", "fusion", "missense", "missense", "missense", "missense", "missense", "missense", "missense", "fusion", "multiple", "missense", "missense", "missense", "frameshift", "nonsense", "missense", "missense", "missense", "missense", "missense"),
    ind = c(1L, 2L, 1L, 2L, 1L, 2L, 3L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 6L, 7L)), 
    .Names = c("Sample", "Gene", "Effect", "ind"), 
    row.names = c(NA, -31L), class = "data.frame")

tt <- expand.grid(levels(temp_plot$Gene),levels(temp_plot$Sample))
colnames(tt) <- c("Gene", "Sample")
temp_plot2 <- merge(temp_plot,tt, all.y=T)

p3 <- ggplot(temp_plot2, aes(x=Gene, y=Sample)) + 
    geom_tile(aes(fill=Effect),colour="white",size=1.1) + 
    coord_equal() + theme_bw() + theme(axis.text.x = element_blank(), 
        axis.line = element_blank(), axis.title.y = element_blank(), axis.title.x = element_blank(),
        axis.ticks.y = element_blank(), axis.ticks.x = element_blank(), 
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
        panel.border = element_blank(), panel.background = element_blank(), 
        legend.position = "none" ) 

temp_collapse_plot <- ddply(temp_plot, .(Sample), function(df){
  df <- df[order(df$Gene),]
  df <- unique(df)
  data.frame(df,ind=1:nrow(df))
})

p4 <- ggplot(temp_collapse_plot, aes(y=Sample, x=ind)) + 
    geom_tile(aes(fill=Effect), colour="white", size=1.1) + coord_equal() +
    scale_x_discrete(labels=levels(temp_plot$Gene)[order(nchar(levels(temp_plot$Gene)), decreasing=T)]) + 
    theme_bw() + theme(axis.line = element_blank(), 
        axis.text.x = element_blank(), axis.text.y = element_blank(), 
        axis.ticks.x = element_blank(), axis.title.y = element_blank(), 
        axis.ticks.y = element_blank(), axis.title.x = element_blank(), 
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
        panel.border = element_blank(), panel.background = element_blank())

【问题讨论】:

    标签: r plot ggplot2 gridextra gtable


    【解决方案1】:

    p3b <- ggplot_build(p3)
    p4b <- ggplot_build(p4)
    g3 = ggplot_gtable(p3b) ; g4 = ggplot_gtable(p4b)
    
    # work out the number of horizontal tiles
    n3 <- length(p3b[["panel"]][["ranges"]][[1]][["x.major_source"]])
    n4 <- length(p4b[["panel"]][["ranges"]][[1]][["x.major_source"]])
    
    require(gtable)
    require(grid)
    g <- cbind(g3, g4, size = "first")
    #adjust the panel widths in proportion to n4/n3
    panels <- g$layout$l[grep("panel", g$layout$name)]
    g$widths[panels] <- lapply(c(1,n4/n3), unit, "null")
    grid.newpage()
    grid.draw(g)
    

    【讨论】:

    • 这可以纠正高度,但不幸的是现在宽度也一样了,所以右手图中的块失去了它们的方形度。
    • 请提供可重现的代码,这样我们就不必在点之间猜测。
    • 亲爱的 baptiste,我在问题中添加了一个可重现的示例。谢谢
    • @yoda230 代码无法运行(请尝试缩进)。
    猜你喜欢
    • 2016-05-22
    • 2016-05-12
    • 2015-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-30
    • 2020-08-27
    • 2010-11-15
    相关资源
    最近更新 更多