R | gghalves: 再次实现分半小提琴图、云雨图
2022-09-06 本文已影响0人
shwzhao
参考:
https://erocoar.github.io/gghalves/
1. 分半小提琴图
-
geom_half_violin()
split
: 映射拆分
position
side
:"l"
|"r"
: left (default) | right -
geom_half_boxplot()
center
library(tidyverse)
library(gghalves)
df_iris <- iris %>%
pivot_longer(-Species, names_to = "Type", values_to = "Length") %>%
filter(Species %in% c("virginica", "versicolor")) %>%
mutate(Species = factor(Species, levels = c("virginica", "versicolor")))
ggplot(df_iris) +
geom_half_violin(aes(Type, Length, split = Species, fill = Species),
position = "identity") +
scale_fill_manual(values = c("#3182bd", "#e6550d")) +
geom_half_boxplot(data = filter(df_iris, Species == "virginica"),
aes(Type, Length),
width = 0.15,
side = "l") +
geom_half_boxplot(data = filter(df_iris, Species == "versicolor"),
aes(Type, Length),
width = 0.15,
side = "r") +
theme_bw()
分半小提琴图
2. 云雨图
-
geom_half_dotplot()
binwidth
dotsize
stackdir
:"up"
|"down"
ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) +
geom_half_violin(side = "r") +
geom_half_dotplot(binwidth = 0.05,
dotsize = 2,
stackdir = "down") +
scale_fill_manual(values = c("#FFCC00", "#009999", "#CC3333")) +
theme_bw()
云雨图