r - 结合两个 grobs ,其中一个使用 grid.draw 创建
问题描述
我正在尝试grob
将ggplot()
两个图形对象(grid.draw()
ggplot_gtable
library(ggplot2)
library(grid)
library(gridExtra)
plot_gtable <- function(x) {
grid::grid.draw(ggplot_gtable(ggplot_build(x)))
}
plot1 <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
plot2 <- plot_gtable(ggplot(mtcars, aes(mpg)) + geom_dotplot())
grid.arrange(plot1, plot2)
gList(structure(list(wrapvp = structure(list(x = structure(0.5, class = "unit", valid.unit = 0L, unit = "npc")) 中的错误,: "gList" 中只允许“grobs”
由reprex 包(v0.2.1)于 2018 年 12 月 12 日创建
显然,调用grid.draw
结果是NULL
object 而不是 a ,这似乎是在这种情况下失败grob
的原因。grid.arrange()
我尝试了先打电话和不打电话grid::grid.newpage
。
我尝试使用grid::viewport
and gridExtra::arrangeGrob
and ggpubr::ggarrange
and cowplot::plot_grid
and alsopatchwork
包,都无济于事。
如何使用这些对象创建组合图?
解决方案
grid.arrange
当使用你想要使用实际对象而不是试图绘制它来组合绘图和/或 grobs时。这就是为什么plot2
它NULL
被绘制而不是返回,因此不能组合。所以在组合情节之前不要绘制它。
library(ggplot2)
library(gridExtra)
# example ggplot
plot1 <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
# example gtable
plot2 <- ggplotGrob(plot1)
grid.arrange(plot1, plot2)
推荐阅读
- php - 为什么 FILES 数组返回上传错误 3
- python - 按创建日期过滤多个 csv 文件并连接成一个 pandas DataFrame
- python - Django重定向返回200但页面没有改变
- sas - SAS中分组值的组合
- python - 我的终端说我应该添加一条路径,但我已经有了(Windows 10)
- python - Elasticsearch、映射、日期映射问题
- c# - 使用 c# 进程对象获取有关 citrix 进程的唯一信息
- javascript - 如何在 javascript 中定位这些元素?(更多下文)
- python - 实现广度优先搜索
- java - 通过配置文件禁用 spring-context-indexer