I am trying to compose a figure with multiple panels. I have a lot of empty space and I am trying to overlap some of the rectangular panels, so I can make some of them larger. However, setting in theme() either plot.background or plot.background to element_rect(fill = "transparent") worked.
Here an example of what I am working with:
library(ggplot2)
library(gridExtra)
library(patchwork)
library(ggpubr)
d1 <- data.frame(
val1=rnorm(500,1,1),
val2=rnorm(500,1,2),
val3=rnorm(500,1,3)
)
pA <- ggplot(d1) +
geom_density(aes(x=val1)) +
theme(aspect.ratio = 1,
plot.background = element_rect(fill = "transparent", color="#808080"))
pC <- ggplot(d1) +
geom_density(aes(x=val2)) +
theme_pubr() +
theme(
plot.background = element_rect(fill = "transparent", color="#808080"))
pD <- ggplot(d1) +
geom_density(aes(x=val3)) +
theme_pubr() +
theme(
plot.background = element_rect(fill = "transparent", color="#808080"),
panel.background = element_rect(fill = "transparent", color="#808080")
)
df_mtcars <- as.data.frame(mtcars)
df_mtcars$name <- rownames(df_mtcars)
df_mtcars$cyl <- as.factor(df_mtcars$cyl)
pB <- ggplot(df_mtcars) +
geom_col(aes(x=hp, y=name, fill=cyl)) +
theme_minimal() +
scale_x_continuous(position="top") +
scale_y_discrete(limits=rev) +
theme(legend.position = "bottom",
plot.background = element_rect(fill = "transparent", color="#808080")) +
labs(y=NULL, x="HP") +
guides(fill=guide_legend(
title.position="top",
byrow=TRUE,
nrow=3
))
grid.arrange(
patchworkGrob(pA + plot_layout(widths=1) +
plot_annotation(tag_levels = list(c('A')) ) &
theme(plot.tag = element_text(size = 15, face = "bold"))
),
patchworkGrob(pB +
plot_annotation(tag_levels = list(c('B')) ) &
theme(plot.tag = element_text(size = 15, face = "bold"))
),
patchworkGrob(
pC +
plot_spacer() +
pD +
plot_spacer() +
plot_layout(widths = c(1,
0.01,
1, -0.65)) +
plot_annotation(tag_levels = list(c('C', 'D')) ) &
theme(plot.tag = element_text(size = 15, face = "bold"))
),
widths=c(1, 0.75),
heights=c(1, 0.65),
layout_matrix=rbind(c(1,2),c(3,2))
)
This code generates this figure:
As you can see, despite using plot.background = element_rect(fill = "transparent", color="#808080"), panel D has an opaque background that blocks the text on the y axis of panel B.
It seems that the issue lies in having the transparency in separate patchworkGrob.
If I draw both plots in the same grob, the transparency works fine:
pC+plot_spacer()+pD+plot_layout(widths=c(1,-0.35,1)) +
+ plot_annotation(tag_levels = list(c('C', 'D')) )
However, if the overlap is between plots in separate grobs, the transparency no longer works:
grid.arrange(
patchworkGrob(pA +
plot_annotation(tag_levels = list(c('A')) )
),
patchworkGrob( plot_spacer() + pD +
plot_layout(widths=c(-0.35, 1)) +
plot_annotation(tag_levels = list(c('D')) )
),
nrow=1
)
Is there any way to be able to build the composite figure using several patchworkGrob calls and still have the transparency working between panels??
This is just a mock-up, but the original figure I am trying to build is more complex and I haven't been able to build its layout without using separate calls.



