【简说基因】画图时,对称或接近对称的图形,可以绘制其一半,从而为其他几何图形预留空间。 画图时我们都希望展示数据更多的细节,但绘图区域空间有限,多种图形混合在一起,有时候会显得拥挤。一种解决方法是:对称或接近对称的图形,只展示其一半,另一半节省下来的空间用于绘制其他图形。而 gghalves 包就是专为这样一种需求而开发的,它提供 3 个半几何图形,分别是箱线图、小提琴图和散点图: geom_half_boxplot geom_half_violin geom_half_point 先来感受一下这 3 种图形: library(gghalves)
library(ggbeeswarm)
library(patchwork)
library(dplyr)
p1 = ggplot(iris, aes(x = Species, y = Sepal.Width)) +
geom_half_boxplot()
p2 = ggplot(iris, aes(x = Species, y = Sepal.Width)) +
geom_half_violin()
p3 = ggplot(iris, aes(x = Species, y = Sepal.Width)) +
geom_half_point()
p4 = ggplot(iris, aes(x = Species, y = Sepal.Width)) +
geom_boxplot()
p5 = ggplot(iris, aes(x = Species, y = Sepal.Width)) +
geom_violin()
p6 = ggplot(iris, aes(x = Species, y = Sepal.Width)) +
geom_point()
(p1 / p2 /p3) | (p4/p5/p6)
![](data:image/svg+xml;utf8,svg%20xmlns='http://www.w3.org/2000/svg'%20width='768'%20height='672'/svg) 可以看到: 箱线图和小提琴图分别展示了一半。散点图并不是绘制大量的半个点,而是绘制中轴线一边的点。另外与 ggplot2 的散点不同,gghalves 的散点自带抖动效果。箱线图 + 散点图ggplot(iris, aes(x = Species, y = Sepal.Width, fill = Species)) +
geom_half_boxplot() +
geom_half_point()
小提琴图 + 散点图ggplot(iris, aes(x = Species, y = Sepal.Width, fill = Species)) +
geom_half_violin() +
geom_half_point()
与其他 ggplot2 扩展包协作p1 = ggplot(iris, aes(x = Species, y = Sepal.Width, fill = Species)) +
geom_half_boxplot() +
geom_beeswarm(side = 1)
p2 = ggplot(iris, aes(x = Species, y = Sepal.Width, fill = Species)) +
geom_half_violin() +
geom_beeswarm(side = 1)
p1 / p2
组合不同的几何图形ggplot() +
geom_half_boxplot(
data = iris %>% filter(Species=="setosa"),
aes(x = Species, y = Sepal.Length, fill = Species), outlier.color = NA) +
ggbeeswarm::geom_beeswarm(
data = iris %>% filter(Species=="setosa"),
aes(x = Species, y = Sepal.Length, fill = Species, color = Species), side=1
) +
geom_half_violin(
data = iris %>% filter(Species=="versicolor"),
aes(x = Species, y = Sepal.Length, fill = Species), side="r") +
geom_half_dotplot(
data = iris %>% filter(Species=="versicolor"),
aes(x = Species, y = Sepal.Length, fill = Species), method="histodot", stackdir="down") +
geom_half_boxplot(
data = iris %>% filter(Species=="virginica"),
aes(x = Species, y = Sepal.Length, fill = Species), side = "r", errorbar.draw = TRUE,
outlier.color = NA) +
geom_half_point(
data = iris %>% filter(Species=="virginica"),
aes(x = Species, y = Sepal.Length, fill = Species, color = Species), side = "l") +
scale_fill_manual(values = c("setosa" = "#cba1d2", "versicolor"="#7067CF","virginica"="#B7C0EE")) +
scale_color_manual(values = c("setosa" = "#cba1d2", "versicolor"="#7067CF","virginica"="#B7C0EE")) +
theme(legend.position = "none")
## Bin width defaults to 1/30 of the range of the data. Pick better value with
## `binwidth`.
![](data:image/svg+xml;utf8,svg%20xmlns='http://www.w3.org/2000/svg'%20width='672'%20height='480'/svg) 参考资料: https://github.com/erocoar/gghalves 如果这篇文章对你有用,请为我点一个【免费】的【赞】,谢谢。也欢迎你关注【简说基因】。
|