使用`grid.arrange`调整标题和图例

问题描述 投票:0回答:1

我在 R 中有 6 个不同的图,我想用 grid.arrange 将它们排列在一个图中。它们共享相同的图例,但每当我想放置一个共同的图例时,图的大小都会相应调整,因此带有图例的图看起来更小。我还想在每列的左上角使用两个标题,但到目前为止,我只在一个图中获得了 6 个图,并在第一列中间使用了一个标题。

这是正在运行的绘图的代码片段(没有真实数据),其中每列的标题之一和图例:

set.seed(123)
a_SYS <- data.frame(
  Sampling_NR = rep(1:10, 2),
  ACM = c(runif(10, 100, 130), runif(10, 110, 140)),
  ACS = c(runif(10, 1, 5), runif(10, 1, 6)),
  Stress = rep(c("one", "two"), each = 10)
)

# Plot
plot_A <- ggplot(data = a_SYS, aes(x = Sampling_NR, y = ACM, 
                                     group = Stress, 
                                     colour = Stress,
                                     shape = Stress)) +
  geom_line() +
  geom_errorbar(aes(ymin=ACM-ACS, ymax=ACM+ACS), width= .2) +
  geom_point() +
  geom_vline(xintercept = c(a_SYS$Sampling_NR[4], a_SYS$Sampling_NR[5]), 
             linetype = "dashed", color = "gray80") +
  ggtitle("") +
  theme_minimal() +
  scale_y_continuous(breaks = seq(90,140,20), limits = c(90, 140), name = "") +
  labs(x = "Time in min",
       y = "") +
  scale_color_manual(values = c("gray50", "black"),
                     labels = c("C-TSST", "TSST"),
                     name = "SYS") +
  scale_x_discrete(labels = c("-80", "-60", "-40", "-20", "0", "+20", "+40", "+60", "+80", "+100")) +
  theme(legend.position = "none",
        legend.box = "horizontal",    
        axis.title.x = element_text(size = 9.5),
        axis.title.y = element_text(size = 9.5),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.line = element_line(color = "black"),
        axis.ticks = element_line(color = "black"))

tg1 <- textGrob('A', gp = gpar(fontsize = 17, fontface = 'bold'))
BM3 <- grid.arrange(tg1,NULL,plot_A,plot_A,plot_A,plot_A,  plot_A,plot_A,ncol = 2, nrow = 5, widths = widths, heights = heights, padding = padding)

r ggplot2 plot legend subtitle
1个回答
0
投票

有这样的事吗?

library(patchwork)
library(ggplot2)

set.seed(123)
a_SYS <- data.frame(
  Sampling_NR = rep(1:10, 2),
  ACM = c(runif(10, 100, 130), runif(10, 110, 140)),
  ACS = c(runif(10, 1, 5), runif(10, 1, 6)),
  Stress = rep(c("one", "two"), each = 10)
)

# Plot
plot_A <- ggplot(data = a_SYS, aes(x = Sampling_NR, y = ACM, 
                                   group = Stress, 
                                   colour = Stress,
                                   shape = Stress)) +
  geom_line() +
  geom_errorbar(aes(ymin=ACM-ACS, ymax=ACM+ACS), width= .2) +
  geom_point() +
  geom_vline(xintercept = c(a_SYS$Sampling_NR[4], a_SYS$Sampling_NR[5]), 
             linetype = "dashed", color = "gray80") +
  ggtitle("") +
  theme_minimal() +
  scale_y_continuous(breaks = seq(90,140,20), limits = c(90, 140), name = "") +
  labs(x = "Time in min",
       y = "") +
  scale_color_manual(values = c("gray50", "black"),
                     labels = c("C-TSST", "TSST"),
                     name = "SYS") +
  scale_x_discrete(labels = c("-80", "-60", "-40", "-20", "0", "+20", "+40", "+60", "+80", "+100")) +
  theme(# legend.position = "none",
        legend.box = "horizontal",    
        axis.title.x = element_text(size = 9.5),
        axis.title.y = element_text(size = 9.5),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.line = element_line(color = "black"),
        axis.ticks = element_line(color = "black"))

(plot_A + ggtitle("Left")) + (plot_A + ggtitle("Right")) + plot_A + plot_A + plot_A + plot_A + 
  plot_layout(ncol = 2, guides = "collect")

创建于 2024-03-19,使用 reprex v2.1.0

© www.soinside.com 2019 - 2024. All rights reserved.