【问题标题】:ggplot grobs align with tableGrobggplot grobs 与 tableGrob 对齐
【发布时间】:2016-10-26 14:51:15
【问题描述】:

我很难找到对齐 ggplot grob 和 table grob 的解决方案。我尝试按照here 的说明进行操作,但仍然没有给出我想要的结果。

library(grid)
library(gridExtra)
library(ggplot2)
library(tibble)
library(gtable)
dat <- tibble::rownames_to_column(mtcars, "car") #convert rownames to first col

plot1 <- ggplot(dat, aes(car, mpg)) +
    geom_bar(stat = "identity") +
    coord_flip()

g1 <- ggplotGrob(plot1)
tb1 <- tableGrob(dat$cyl)
g1 <- gtable_add_cols(g1, unit(0.2, "npc"))
g1 <- gtable_add_grob(g1, grobs = tb1, t=3, l=ncol(g1), b=6, r=ncol(g1))

grid.newpage()
grid.draw(g1)

我希望表格中的每个单元格都与直方图中的相关条对齐,但仍然无法理解如何从布局中实现 t,l,b,r。This is the output I got

【问题讨论】:

    标签: ggplot2 gridextra gtable


    【解决方案1】:

    在尝试使用 ggplot2 在 R 中制作类似森林图的东西时,我遇到了与上述类似的问题,但没有发现任何其他解决方案符合我的需求。上面的答案对我不起作用 - 桌子没有出现。所以我拼凑了一个代码方面不是那么漂亮的解决方案,但我实际上有点喜欢整洁的视觉输出。

    我喜欢这个解决方案的地方是:

    • 我对齐了一组自定义文本,而不是在表格中,而只是在右侧的图中,其中对齐与图中的每个文本条目和每个标签匹配。
    • 我使用居中的 ggtitle 来对齐每组文本上方的“列标题”。这些可以是任何类型的字符串(在我的实际使用中,我有点估计和置信区间)。
    library(gridExtra)
    library(ggplot2)
    
    dat <- data.frame(
        label = c("A", "B", "C"),
        point_est = c(1,2,3),
        lb_ci = c(.5, 1.5, 2.5),
        ub_ci = c(1.5, 2.5, 3.5),
        n = c(50, 100, 150),
        total = c(75, 150, 200)
    )
    
    plot1 <- ggplot(dat, aes(x=point_est, y=label)) +
        geom_point() +
        geom_errorbarh(aes(xmin=lb_ci, xmax=ub_ci), height=.5) +
        ggtitle("Some measure") +
        ylab(NULL) + xlab("some effect estimate")
    
    tab_base <- ggplot(dat, aes(y=label)) +
        ylab(NULL) + xlab("  ") + 
        theme(plot.title = element_text(hjust = 0.5, size=12), ## centering title on text
            axis.text.x=element_text(color="white"), ## need text to be printed so it stays aligned with figure but white so it's invisible
            axis.line=element_blank(),
            axis.text.y=element_blank(),axis.ticks=element_blank(),
            axis.title.y=element_blank(),legend.position="none",
            panel.background=element_blank(),panel.border=element_blank(),panel.grid.major=element_blank(),
            panel.grid.minor=element_blank(),plot.background=element_blank())
    
    tab1 <- tab_base + 
        geom_text(aes(x=1, label=n)) + 
        ggtitle("n")
    
    tab2 <- tab_base +
        geom_text(aes(x=1, label=total)) + 
        ggtitle("total")
    
    lay <-  matrix(c(1,1,1,1,1,1,2,3), nrow=1)
    grid.arrange(plot1, tab1, tab2, layout_matrix = lay)
    

    【讨论】:

    • 感谢您的替代解决方案!
    • 只是想感谢您发布此解决方案@Nicholas G Reich。我花了几个小时寻找与图表完美对齐的表格的解决方案。非常感谢!
    【解决方案2】:

    默认情况下,单元格高度具有绝对大小以容纳文本,但您 can change them to relative units 以便它们随绘图面板缩放,

    library(grid)
    library(gridExtra)
    library(ggplot2)
    library(tibble)
    library(gtable)
    dat <- tibble::rownames_to_column(mtcars, "car") #convert rownames to first col
    
    plot1 <- ggplot(dat, aes(car, mpg)) +
      geom_bar(stat = "identity") +
      coord_flip()
    
    g1 <- ggplotGrob(plot1)
    tb1 <- tableGrob(dat$cyl, theme = ttheme_default(10))
    tb1$heights = unit(rep(1/(nrow(tb1)), nrow(tb1)), "npc")
    tb1$widths = unit.pmax(tb1$widths, unit(2, "lines"))
    g1 <- gtable_add_cols(g1, sum(tb1$widths))
    g1 <- gtable_add_grob(g1, grobs = tb1, t=6, l=ncol(g1), b=6, r=ncol(g1))
    
    grid.newpage()
    grid.draw(g1)
    

    【讨论】:

    • 感谢指导,但我还是不明白为什么 t=6 和 b=6。当我用gtable_show_layout(g1) 查看布局时,单元格是 (3,6)
    • 我现在看到你没有使用我正在使用的相同的 plot1 图,它给出了不同的布局。再次感谢您的解释
    • 你介意分享你的 plot1 代码吗?我很难复制带有轴标题的解决方案,并在图的底部打勾。
    • 当我运行此代码时,我在图的 RHS 上得到一个空白区域,这与上面显示的图不同。也许对其中一个包的一些更新使它不再起作用?
    猜你喜欢
    • 2020-09-04
    • 2023-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-18
    相关资源
    最近更新 更多