Common Main Title for Multiple Plots in Base R & ggplot2 (Example) | patchwork Package Composition

preview_player
Показать описание
R code of this video:

x2 = 2:7)

par(mfrow = c(2, 2)) # Specify grid of plots
plot(data$x1, data$x2) # Draw plots
plot(density(data$x1), main = "")
hist(data$x1, main = "")
barplot(data$x1)
mtext("My Multiplot Title", # Add main title
side = 3,
line = - 2,
outer = TRUE)

library("ggplot2")

ggp1 <- ggplot(data, aes(x1, x2)) + # Create ggplot2 plots
geom_point()
ggp2 <- ggplot(data, aes(x1)) +
geom_density()
ggp3 <- ggplot(data, aes(x1)) +
geom_histogram()
ggp4 <- ggplot(data, aes(x1, x2)) +
geom_bar(stat = "identity")

library("patchwork")

ggp_all <- (ggp1 + ggp2) / (ggp3 + ggp4) + # Create grid of plots with title
plot_annotation(title = "My Multiplot Title") &
ggp_all # Draw grid of plots with title

Follow me on Social Media:

Рекомендации по теме
Комментарии
Автор

@StatisticsGlobe, great video - very helpful. I want two main tiltles - for example, one title for (ggp1 + ggp2) and another title for (ggp3 + ggp4). And finally combine 4 plots into one. Do you have any suggestion?

ZahidulAbedin
Автор

Joachim,
A very nice presentation. Typically I would choose ggplot2 over the base plots. However, in this case the base R plot code seems actually simpler than ggplot2! Fewer lines of code etc. Patchwork is indeed a very nice package but makes the code even longer (of course one could just make a template and thus use it over and over). By the way in base R the y axis tick labels can be straightened up with by adding the simple 'las = 1' to each graph. Finally, I tried simplifying the ggplot2 code but came up empty. Thanks!

haraldurkarlsson
Автор

Great video, just wanted to know something. Is it possible to add a second x axis when doing a ggplot. I've been trying to add it but it keeps coming up with an error that the data is not monotonic.

emileonaicker
Автор

Hello! I'm biology student and I started see R right now in my ungraduate. Could you explain to me, what '&' does? Why I cannot use + instead? Hugs from brazil!

joaovitormio
visit shbcf.ru