r - 使用 grid.arrange 创建的 ggplot 中标题上方的边距
问题描述
我有两个可以像这样创建的 ggplots:
df1 = data.frame(x=1:10, y1=11:20, y2=21:30)
gg1 = ggplot(df1) + geom_point(aes(x=x, y=y1))
gg2 = ggplot(df1) + geom_point(aes(x=x, y=y2))
grid.arrange(gg1, gg2, top=textGrob("Here should be some space above",
gp=gpar(fontsize=18,
fontfamily="Times New Roman")))
现在输出如下所示:
在这里看不到它,因为它是白底白字,但我想在标题上方的输出图像中创建更多空间。而且我不确定该怎么做。这里描述了如何在 grid.arrange 中的单个图之间添加空间 Margins,但我没有设法仅在标题上方添加空间。
解决方案
利用arrangeGrob
您可以通过zeroGrob
如下方式在标题顶部添加一些边距:
library(ggplot2)
library(gridExtra)
library(grid)
df1 = data.frame(x=1:10, y1=11:20, y2=21:30)
gg1 = ggplot(df1) + geom_point(aes(x=x, y=y1))
gg2 = ggplot(df1) + geom_point(aes(x=x, y=y2))
title <- textGrob("Here should be some space above",
gp=gpar(fontsize=18, fontfamily="Times New Roman"))
# Add a zeroGrob of height 2cm on top of the title
title <- arrangeGrob(zeroGrob(), title,
widths = unit(1, 'npc'),
heights = unit(c(2, 1), c('cm', 'npc')),
as.table = FALSE)
grid.arrange(gg1, gg2, top = title)
推荐阅读
- express - NextJS getInitialProps 被 babel asyncToGenerator 调用了两次
- r - 用 Tidyverse 调查数据中的文字替换“其他 - 写入”
- r - 在 for 循环中使用 mutate 时遇到问题
- joomla3.9 - 安全令牌不匹配。该请求被中止以防止任何安全漏洞。请再试一次
- mongodb - 在无服务器框架中保持 MongoDB 连接打开
- laravel - Laravel Vue VueRouter 历史模式
- java - 理解嵌套注释的结构/语法
- java - Java中的自定义排序算法?
- javascript - 如果最终用户无论如何都没有得到所有这些,节点模型文件夹怎么会这么大?
- reactjs - 解析模块说明符时出错:从 API 进行动态导入时做出反应