R plotresearchIMP research

R | gghalves: 再次实现分半小提琴图、云雨图

2022-09-06  本文已影响0人  shwzhao

gghalves 再实现一下 R | easystats 之 see:分半小提琴图、云雨图

参考:
https://erocoar.github.io/gghalves/

1. 分半小提琴图

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. 云雨图

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()
云雨图
上一篇下一篇

猜你喜欢

热点阅读