r - 如何使用 r 中的 ggplot2 和 gridExtra 包将一个 grob 对象排列在另一个对象中

标签 r ggplot2 gridextra

我想知道如何将一个 grob 对象(表格)排列在另一个对象中。考虑以下示例:

library(ggplot2)
library(gridExtra)

p1 <- qplot(data=mtcars, x = mpg, y = hp, facets = ~ hp, geom="point")
t1 <- tableGrob(head(mtcars))
print(arrangeGrob(p1, t1))

这会产生:

Plot 01

我想做的是将表格放在内部另一个对象中,如下所示:

Desired Result

是否可以使用 gridArrangegrid 和/或 gridExtra 中的其他方法来做到这一点?

最佳答案

这是使用网格函数的一种可能性:

png("SO.png", width = 1440, height = 720)

plot(p1)

vp <- viewport(x = 0.95, y = 0.02, 
               width = unit(0.4, "npc"), height = unit(0.2, "npc"),
               just = c("right", "bottom"))
pushViewport(vp)
grid.draw(t1)

dev.off()

resulting plot

关于r - 如何使用 r 中的 ggplot2 和 gridExtra 包将一个 grob 对象排列在另一个对象中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30128467/

相关文章:

css - 在 RStudio 的 bookdown 中创建自定义 block

r - 有人能告诉我为什么 R 没有将整个 data.frame 用于这个 chisq.test 吗?

RSQLite:如何增加列数和参数?

r - ggplot 多线图上缺少图例

r - 将列表 ggplot 放在一起时保持绘制高度固定

r - ggplot2:具有单个图例的单行中有多个图

r - 我创建的返回 ggplot 对象的函数返回错误

r - ggplot 用不同的颜色填充每个标准差

r - ggplot2 可视化绘制在彼此之上的点数 : stat_bin2d or geom_tile or point size?

r - 使用 grid.arrange 创建的 ggplot 中标题上方的边距