[英]Gluing together ggplots to as closely as possible mimic the face_wrap()
我想知道是否有一種方法可以將p1
和p2
圖粘合在一起,以盡可能接近地獲得我的Desired_plot
的外觀(例如,單個xlab
和ylab
, vs
值打印在gray strip
)?
library(ggplot2)
dat1 = subset(mtcars, vs == 0)
dat2 = subset(mtcars, vs == 1)
p1 <- ggplot(dat1, aes(mpg,vs)) + geom_point()
p2 <- ggplot(dat2, aes(mpg,vs)) + geom_point()
Desired_plot <- ggplot(mtcars, aes(mpg,vs)) + geom_point() +
facet_wrap(~vs)
要獲得條帶,您需要將幾何圖形或注釋擬合到圖上。 我認為更簡單的方法是將它們制作成單獨的 ggplots 並使用拼湊將它們粘在上面:
library(patchwork)
p3 <- ggplot(mapping = aes(x = 1, y = 1)) +
theme_void() +
theme(panel.background = element_rect(color = NA, fill = 'gray85'),
plot.margin = margin(6, 6, 0, 6))
p4 <- p3 + geom_text(aes(label = 1))
p3 <- p3 + geom_text(aes(label = 0))
p2 <- p2 +
theme(axis.ticks.y = element_blank(),
axis.text.y = element_blank(),
axis.title = element_blank(),
plot.margin = margin(0, 3, 6, 3))
p1 <- p1 +
theme(plot.margin = margin(0, 6, 6, 6),
axis.title.x = element_text(hjust = 1))
(p3 + p4) / (p1 + p2) + plot_layout(heights = c(1, 15))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.