美文网首页R plotresearchIMP research
R | gghalves: 再次实现分半小提琴图、云雨图

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

作者: shwzhao | 来源:发表于2022-09-06 10:19 被阅读0次

    gghalves 再实现一下 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()
    
    云雨图

    相关文章

      网友评论

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

        本文链接:https://www.haomeiwen.com/subject/bkrinrtx.html