0

我有以下情节,我使用 ggplot2 和拼凑而成的面板图。每一行都对不同的目标数据集进行预测,所以我认为为每一行设置一个标题可能会有所帮助,我只是用空创建的标题ggplot + annotate() 如下: 在此处输入图像描述

示例代码

library(ggplot2)
library(patchwork)
p1 <- ggplot(mtcars) +
  geom_point(aes(mpg, disp)) +
  ggtitle("Plot 1")

p2 <- ggplot(mtcars) +
  geom_boxplot(aes(gear, disp, group = gear)) +
  ggtitle("Plot 2")

p3 <- ggplot(mtcars) +
  geom_point(aes(hp, wt, colour = mpg)) +
  ggtitle("Plot 3")

p4 <- ggplot(mtcars) +
  geom_bar(aes(gear)) +
  facet_wrap(~cyl) +
  ggtitle("Plot 4")

ggplot() +
  annotate(geom = "text", x = 1, y = 1, label = "Predictions on Dataset 1",size=8) +
  theme_void() -> title1
ggplot() +
  annotate(geom = "text", x = 1, y = 1, label = "Predictions on Dataset 2",size=8) +
  theme_void() -> title2


title1 /
  (p1 | p2) /
  title2 /
  (p3 | p4) +
  plot_annotation(
    tag_levels = list(c("","A.1) Model Name 1", "B.1) Model Name 2", "","A.2) Model Name 1", "B.2) Model Name 2")),
    theme = theme(plot.title = element_text(size = 24))
  ) &
  theme(

  )

问题
如何减少title1andtitle2和其他地块之间的空间?

期望的输出: 在此处输入图像描述

4

1 回答 1

1
title1 / (p1 | p2) /
  title2 / (p3 | p4) +
  plot_layout(heights = c(1,6,1,6)) +
  ....

在此处输入图像描述

并且您可以调整您的文本大小title1title2使其与其他文本更加一致。这是size = 5

在此处输入图像描述

于 2021-10-07T16:26:39.763 回答