用 gghalves 再實(shí)現(xiàn)一下 R | easystats 之 see:分半小提琴圖、云雨圖
參考:
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()