Home > database >  Arranging several ggplots in same window
Arranging several ggplots in same window

Time:08-22

I have created below ggplots in same window

library(ggplot2)
library(patchwork)

x <- seq(0.01, .99, length.out = 100)
df <- data.frame(
  x = rep(x, 2),
  y = c(qlogis(x), 2 * qlogis(x)),
  group = rep(c("a","b"),
  each = 100)
)
p1 <- ggplot(df, aes(x=x, y=y, group=group))
p2 <- ggplot(df, aes(x=x, y=y, group=group))
p3 <- ggplot(df, aes(x=x, y=y, group=group))
p4 <- ggplot(df, aes(x=x, y=y, group=group))
wrap_plots(A = p1, B = p2, C = p4, design = "AABB\n#CC#")

These generated below plot window

enter image description here

However now, I also want to squeeze my 3rd ggplot p3 within the bottom-left white-space in the same window, without disturbing current structure.

Is there any way to achieve this. Any pointer will be very help.

CodePudding user response:

Here is one solution:

library(tidyverse)
library(patchwork)

x <- seq(0.01, .99, length.out = 100)
df <- data.frame(
  x = rep(x, 2),
  y = c(qlogis(x), 2 * qlogis(x)),
  group = rep(c("a","b"),
              each = 100)
)
p1 <- ggplot(df, aes(x=x, y=y, group=group))
p2 <- ggplot(df, aes(x=x, y=y, group=group))
p3 <- ggplot(df, aes(x=x, y=y, group=group))
p4 <- ggplot(df, aes(x=x, y=y, group=group))

layout <- "
AAABBB
CDDD##
"
p1   p2   p3   p4   
  plot_layout(design = layout)

Created on 2022-08-22 by the reprex package (v2.0.1)


Or perhaps:

library(tidyverse)
library(patchwork)

x <- seq(0.01, .99, length.out = 100)
df <- data.frame(
  x = rep(x, 2),
  y = c(qlogis(x), 2 * qlogis(x)),
  group = rep(c("a","b"),
              each = 100)
)
p1 <- ggplot(df, aes(x=x, y=y, group=group))
p2 <- ggplot(df, aes(x=x, y=y, group=group))
p3 <- ggplot(df, aes(x=x, y=y, group=group))
p4 <- ggplot(df, aes(x=x, y=y, group=group))

layout <- "
AAABBB
AAABBB
CDDD##
#DDD##
"
p1   p2   p3   p4   
  plot_layout(design = layout)

Created on 2022-08-22 by the reprex package (v2.0.1)

  • Related