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

前端 未结 3 521
梦谈多话
梦谈多话 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:20

    I find this hybrid boxplot very, very lovely so I wanted to recreate it too.

    I wrote a geom_boxjitter that inherits from geom_boxplot and only adds minor changes:

    • It draws the geom_rect only on the left half.
    • It jitters the points with default width of the right half, default height 0.4*resolution and can also take a seed argument.
    • It adds additional whiskers (the horizontal ones) if errorbar.draw is set to TRUE. Their length can also be adjusted.

    You can check the code here. I think it is great how easy it has become to alter existing geoms with slight changes. Using part of your data:

    library(tidyverse)
    library(cowplot)
    library(ggparl)
    
    P <- ggplot(
      dat_long %>% filter(key %in% c("p1", "p2")), 
      aes(x = type, y = value, fill = key)) +
      geom_boxjitter(outlier.color = NA, jitter.shape = 21, jitter.color = NA, 
                     jitter.height = 0.05, jitter.width = 0.075, errorbar.draw = TRUE) +
      theme(legend.position = "none") +
      ylim(c(-0.05, 1.05)) + 
      scale_fill_manual(values = c("#ecb21e", "#812e91"))
    P
    

提交回复
热议问题