首页 > 解决方案 > ggplots 的 ggarrange() 重叠 annotate() 文本

问题描述

我在使用ggarrange()来自egg库的绘图时遇到问题。当我可视化每个图(使用 RStudio)时,它们是完美的。但是,当我将三个图合并到一个图中时,每个网格的相对大小相对于每个图的大小都会发生变化,并且注释文本会相互重叠,并且字体的相对大小也会发生变化。这似乎是一个可视化问题而不是代码问题,但我不知道。

library(ggplot2)
library(egg)

data <-
  structure(list(Treatment = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L),
                                       .Label = c("A","B", "C", "D"),
                                       class = "factor"),
                 Var1 = c(10L, 12L, 13L, 9L, 14L, 1L, 2L, 1L, 4L, 5L, 5L, 4L, 3L, 8L, 4L, 20L, 20L, 26L, 24L, 17L),
                 Var2 = c(2.5, 2.6, 2.3, 2.8, 2.9, 1.5, 1.3, 1.5, 1.4, 1.3, 1.6, 1.7, 1.6, 1.8, 1.6, 3.2, 3.5, 3.3, 3.2, 3.1),
                 Var3 = c(1.1,1.2, 1.1, 1.1, 1.2, 1.4, 1.5, 1.4, 1.5, 1.4, 1.3, 1.3, 1.2, 1.3, 1.3, 0.9, 0.85, 0.9, 0.8, 0.85)),
            class = "data.frame",
            row.names = c(NA, -20L))

figura2a <-
  ggplot(data, aes(x = Var2, y = Var1)) +
  geom_point(aes(
    color = Treatment,
    shape = Treatment,
    size = 2.5
  )) +
  theme(legend.position = "bottom") +
  geom_smooth(method = 'lm',
              colour = "black",
              se = FALSE) +
  scale_colour_manual(values = c(
    "lightsteelblue4",
    "salmon",
    "purple3",
    "royalblue2",
    "seagreen3"
  )) +
  scale_shape_manual(values = c(15, 16, 25, 18, 17)) +
  theme_light() +
  theme(
    axis.text.x = element_text(colour = "black", size = 22),
    axis.text.y = element_text(colour = "black", size = 22)
  ) +
  theme(text = element_text(
    size = 22,
    family = "Arial",
    color = "black"
  )) +
  theme(legend.title = element_text(size = 20, color = "black"),
        legend.text = element_text(size = 20)) +
  scale_size(guide = 'none') +
  guides(shape = guide_legend(override.aes = list(size = 4))) +
  xlab("Var2") +
  ylab("Var1") +
  theme(legend.position = "none") +
  annotate(
    geom = "text",
    x = 3,
    y = 4,
    label = "R^2 == 0.8639",
    parse = TRUE,
    color = "black",
    size = 5
  ) +
  annotate(
    geom = "text",
    x = 3,
    y = 5.5,
    label = "p < 0.001",
    color = "black",
    size = 5.5,
    fontface = 'italic',
    check_overlap = TRUE
  )
figura2a

figura2b <-
  ggplot(data, aes(x = Var3, y = Var1)) +
  geom_point(aes(
    color = Treatment,
    shape = Treatment,
    size = 2.5
  )) +
  theme(legend.position = "bottom") +
  geom_smooth(method = 'lm',
              colour = "black",
              se = FALSE) +
  scale_colour_manual(values = c(
    "lightsteelblue4",
    "salmon",
    "purple3",
    "royalblue2",
    "seagreen3"
  )) +
  scale_shape_manual(values = c(15, 16, 25, 18, 17)) +
  theme_light() +
  theme(
    axis.text.x = element_text(colour = "black", size = 22),
    axis.text.y = element_text(colour = "black", size = 22)
  ) +
  theme(text = element_text(
    size = 22,
    family = "Arial",
    color = "black"
  )) +
  theme(legend.title = element_text(size = 20, color = "black"),
        legend.text = element_text(size = 20)) +
  scale_size(guide = 'none') +
  guides(shape = guide_legend(override.aes = list(size = 4))) +
  xlab("Var3") +
  ylab("Var1") +
  theme(legend.position = "none") +
  annotate(
    geom = "text",
    x = 1.2,
    y = 22,
    label = "R^2 == 0.8252",
    parse = TRUE,
    color = "black",
    size = 5
  ) +
  annotate(
    geom = "text",
    x = 1.2,
    y = 20,
    label = "p < 0.001",
    color = "black",
    size = 5.5,
    fontface = 'italic'
  )
figura2b

figura2 <- ggarrange(figura2a, figura2b, ncol = 1, nrow = 2)

reprex 包(v0.3.0)于 2020-05-16 创建

标签: rggplot2gridextra

解决方案


这更像是一个可视化问题而不是代码,我使用 ggarrange 遇到了同样的问题,当缩放绘图时,相对大小和位置发生了变化,也许你可以使用 theme() 增加 ggplot() 中两个重叠项目之间的边距使用 ggarrange 之前的功能:

ggplot(data, aes(x = Var3, y = Var1))+
#adjust y label positions
theme(axis.title.y = element_text(margin = margin (r = 10)),
#change the plot margins
plot.margin = margin(l = 15,r=10))

推荐阅读