gghalves:画半图,sao气逼人
- 该包让half-half图的实现变得更容易,比如一张图,左边一半是散点右边一半是小提琴
- 特点:基于ggplot2的语法,用以下函数,来拓展选定的geom
加载包,用iris数据感受一下
library(ggplot2) library(gghalves)
基础版
ggplot(iris, aes(x = Species, y = Sepal.Width)) + geom_half_point() + geom_half_boxplot() + geom_half_violin()
进阶版
library(ggplot2) library(gghalves) library(ggsci) library(ggpubr)
ggplot(iris, aes(x = Species, y = Sepal.Width, fill = Species) ) + geom_half_violin( aes(fill = Species), adjust = 1.5, position = position_nudge(x = 0.05), width = 0.3, side = “r”) + geom_half_boxplot(aes(fill = Species), outlier.shape = NA, width = 0.2) + geom_point(aes(x = as.numeric(Species) -0.1, #注意先画前面两个半图,最后画geom_point y = Sepal.Width, colour = Species)) + scale_fill_lancet() + scale_color_lancet() + geom_signif(comparisons = list(c(“setosa”, “versicolor”)), map_signif_level = c(“*” = 0.001, ““ = 0.01, “*” = 0.5)) + theme_bw()
- 可选参数:- side:= l 把半图放左边,= r 把半图放右边**patchwork包: 灰常灰常强大的拼图,大家一定要一起学,值得入手!!**<br />这个包学习的时候,可以把待拼图的画布想象成一个网格grid,你想在哪一块画图,就往里面填- 特点:+即可拼图- 由于该包是针对于ggplot2,所以对ggplo2或者ggplot2的延展包可以很好的适用。但对其他非ggplot2的包的图就不适用了,如pheatmap的热图,可以使用AI或者其他拼图软件完成。
library(patchwork)
p1 <- ggplot(mtcars) + geom_point(aes(mpg, disp)) p2 <- ggplot(mtcars) + geom_boxplot(aes(gear, disp, group = gear)) p1 + p2

plot_layout:可以对图形排列以及每个图的范围进行指定
p1 + p2 +plot_layout(ncol = 1, heights = c(4,1)) p1 + p2 + plot_layout(ncol = 2, width = c(1, 2)) + plot_annotation(tag_levels = ‘A’) #添加标记
plot_spacer()填充空白格
p1 + plot_spacer() + p2

增加花括号的使用进行嵌套可以布置更复杂的图形:
p3 <- ggplot(mtcars) + geom_smooth(aes(disp, qsec)) p4 <- ggplot(mtcars) + geom_bar(aes(carb)) p4 + { p1 + { p2 + p3 + plot_layout(ncol = 1) } } + plot_layout(ncol = 1)

plot_layout()
p5 <- ggplot(mtcars) + geom_point(aes(mpg, disp, colour = mpg, size = wt)) p6 <- ggplot(mtcars) + geom_point(aes(hp, wt, colour = mpg)) 3 p5 + p6 + plot_layout(guides = ‘collect’) #收集图例,去除重复图例
<br />**design: 自由设计图形格式,非常灵活便捷**
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))
design: 自由设计图形格式,非常灵活便捷, ##note:”#”代表一个空格,每个字母代表占用一个格子
design <- “
BB
AACD
AA#D
“
p1 + p2 + p3 + p4 + p5 + plot_layout(design = design)
```

