Reputation: 11
I have 6 different plots in R that I want to arrange in one plot with grid.arrange. They share the same legend, but whenever I want to put a common legend, the sizes of the plots are adjusted accordingly, so the plot with the legend looks smaller. I also want to use two titles on the top left corner of each column but so far I only managed to get 6 plots in one figure and a single title in the middle of the first column.
This is the snippet of the code for the plot that is functioning (without the real data) with one of the titles for each column and a legend:
library(ggplot2)
library(grid)
library(gridExtra)
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)
Upvotes: 1
Views: 157
Reputation: 7540
Something like this?
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")
Created on 2024-03-19 with reprex v2.1.0
Upvotes: 1