使用 grid.arrange 创建的 ggplot 标题上方的边距
Margin above title in ggplot created with grid.arrange
我有两个可以像这样创建的 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")))
现在输出如下所示:
这里看不到它,因为它是白色的白色,但我想在标题上方的 output-image 中创建更多 space。而且我不太确定该怎么做。这里描述了如何在单个地块 之间添加 space ,但我没有设法在 header.
上方添加 space
利用 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)
你能接受另一个包裹吗?
library(patchwork)
plot_spacer()/ (gg1 + ggtitle("there is now space above")) / gg2 + plot_layout(heights = c(.5,1,1))
或者在第一个plot中设置title margin
gg1 = ggplot(df1) + geom_point(aes(x=x, y=y1)) +
ggtitle("there is now space above") +
theme(plot.title = element_text(margin = margin(t = 1, unit = "in")))
gg1 / gg2
我有两个可以像这样创建的 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")))
现在输出如下所示:
这里看不到它,因为它是白色的白色,但我想在标题上方的 output-image 中创建更多 space。而且我不太确定该怎么做。这里描述了如何在单个地块
利用 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)
你能接受另一个包裹吗?
library(patchwork)
plot_spacer()/ (gg1 + ggtitle("there is now space above")) / gg2 + plot_layout(heights = c(.5,1,1))
或者在第一个plot中设置title margin
gg1 = ggplot(df1) + geom_point(aes(x=x, y=y1)) +
ggtitle("there is now space above") +
theme(plot.title = element_text(margin = margin(t = 1, unit = "in")))
gg1 / gg2