r - 在拼凑图 R 中添加一个 grob
问题描述
我有一个返回拼凑图的函数,我无法对其进行任何更改。我想rectGrob()
在上面添加一个。当我尝试这样做时,我删除了两个图。
library(gridExtra)
library(patchwork)
library(ggplot2)
p1 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point()
p2 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) + geom_point()
p3 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width), col = 'blue') + geom_point()
p <- p1 + p2 + p3
grob_top <- grobTree(rectGrob(gp=gpar(fill='#F0F0F0',col= 'black')), textGrob('P1,P2, P3'))
grid.arrange(grob_top, p, heights = c(0.1, 0.9))
解决方案
使用patchwork::wrap_elements()
效果比gridExtra::grid.arrange()
patchwork::wrap_elements(grob_top) /
patchwork::wrap_elements(p) /
patchwork::wrap_elements(p) +
patchwork::plot_layout(ncol = 1, heights = c(0.1, 0.45, 0.45))
推荐阅读
- excel - 基于空白单元格删除非连续范围
- c# - setsimplecolumn 不使用 C# 中的 itextsharp 5 将文本定位到 PDF 中的给定坐标
- sql - SQL 查询以动态追加具有计算值的列
- sql-server - 需要帮助来计算单列
- javascript - 使用javascript单击按钮后如何更改视频源
- flutter - 如何使 Flutter 应用程序字体响应平板电脑?
- html - 我的输入字段必填选项不起作用
- python - Python:if 语句中的行级计算
- java - AppEngine Java Google Cloud 解析依赖项
- python - 将带有字符串的json文件加载到python中的pandas数据框中