How to plot a hybrid boxplot: half boxplot with jitter points on the other half?

前端 未结 3 519
梦谈多话
梦谈多话 2020-11-28 10:35

I\'m trying to make a similar plot to Fig. 2d-f in an article published on Nature this year. It\'s basically a half boxplot with points on the other half.

Can anyone

3条回答
  •  忘掉有多难
    2020-11-28 11:29

    Adding another option: gghalves package developed by @erocoar

    library(tidyverse)
    library(ggbeeswarm)
    # if (!require(devtools)) {
    #   install.packages('devtools')
    # }
    # devtools::install_github('erocoar/gghalves')
    library(gghalves)
    
    # default
    ggplot(dat_long, aes(x = type, y = value, color = type)) +
      facet_grid(loc ~ key, scales = 'free_y') +
      geom_half_boxplot(nudge = 0.05, outlier.color = NA) +
      geom_half_point() +
      theme_light() +
      theme(legend.position = "bottom") +
      guides(color = guide_legend(nrow = 1))
    

    # plot half violin
    ggplot(dat_long, aes(x = type, y = value)) +
      facet_grid(loc ~ key, scales = 'free_y') +
      geom_half_boxplot(nudge = 0.05) +
      geom_half_violin(aes(fill = type),
                       side = "r", nudge = 0.01) +
      theme_light() +
      theme(legend.position = "bottom") +
      guides(fill = guide_legend(nrow = 1))
    

    # using ggbeeswarm for plotting points
    ggplot(dat_long, aes(x = key, y = value, color = type)) +
      facet_grid(loc ~ ., scales = 'free_y') +
      geom_half_boxplot(position = position_dodge(width = 0.9),
                        nudge = 0.05, outlier.color = NA) +
      geom_half_point(transformation = position_quasirandom(width = .9, groupOnX = TRUE)) +
      theme_light() +
      theme(legend.position = "bottom") +
      guides(color = guide_legend(nrow = 1))
    

    Created on 2020-04-30 by the reprex package (v0.3.0)

提交回复
热议问题