尽管patchwork包通过 +,|进行图片拼接非常容易和直观,但是很难以编程方式使用。 接下来介绍{wrap_plots}函数的使用,它是完美的替代解决方案!
library(ggplot2)
p1 <- ggplot(mtcars) + geom_point(aes(mpg, disp))
p2 <- ggplot(mtcars) + geom_boxplot(aes(gear, disp, group = gear))
p3 <- ggplot(mtcars) + geom_bar(aes(gear)) + facet_wrap(~cyl)
p4 <- ggplot(mtcars) + geom_bar(aes(carb))
p5 <- ggplot(mtcars) + geom_violin(aes(cyl, mpg, group = cyl))
wrap_plots(p1, p2, p3, p4, p5)
也可以使用下面这种写法
plots <- list(p1, p2, p3, p4, p5)
wrap_plots(plots)
将图形名称与区域匹配
design <- "#BB
AA#"
wrap_plots(B = p1, A = p2, design = design)
wrap_plots(p1, p2, design = design)
design <- "CB
CA"
wrap_plots(A=p2,B=p1,C=p3,design = design)
design <- "AB
CC"
wrap_plots(A=p2,B=p1,C=p3,design = design)
合并图例至顶部
p1 <- iris %>%
ggplot(aes(Sepal.Length,Petal.Length,color=Species))+
geom_point()+
theme(legend.direction ="horizontal")
p2 <- iris %>%
ggplot(aes(Petal.Width,Petal.Length,color=Species))+
geom_point()+
theme(legend.direction ="horizontal")
wrap_plots(
guide_area(),p1,p2,
design="AA\nBC",
guides = "collect",
heights=c(1/5,4/5)) +
plot_annotation(tag_levels = "A")
网友评论