如何在ggboxplot中按类别对抖动进行着色

How to color jitter by category in ggboxplot

我有以下数据框:

library(tidyverse)
dat <- structure(list(feat = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L), .Label = c("A", "KyteDoo", "FoldIndex"
), class = "factor"), dose = c("0.3mM", "1mM", "3mM", "10mM", 
"20mM", "0.3mM", "1mM", "3mM", "10mM", "20mM", "0.3mM", "1mM", 
"3mM", "10mM", "20mM"), vimp = c(-0.0025, 0, 0.0328571428571429, 
0.025, 0.0425, 0.005, 0.015, 0.0228571428571429, 0.0175, 0.02, 
0.0125, 0.01, 0.02, 0.0325, 0.015)), class = c("tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -15L))

看起来像这样:

> dat
# A tibble: 15 × 3
   feat      dose     vimp
   <fct>     <chr>   <dbl>
 1 A         0.3mM -0.0025
 2 A         1mM    0     
 3 A         3mM    0.0329
 4 A         10mM   0.025 
 5 A         20mM   0.0425
 6 KyteDoo   0.3mM  0.005 
 7 KyteDoo   1mM    0.015 
 8 KyteDoo   3mM    0.0229
 9 KyteDoo   10mM   0.0175
10 KyteDoo   20mM   0.02  
11 FoldIndex 0.3mM  0.0125
12 FoldIndex 1mM    0.01  
13 FoldIndex 3mM    0.02  
14 FoldIndex 10mM   0.0325
15 FoldIndex 20mM   0.015 

并使用此代码:

 library(ggpubr)
 p <- ggboxplot(dat, x = "feat", y = "vimp",  add = "jitter") + 
 theme(axis.text.x=element_text(angle = 90, hjust = 1, vjust = 0.5, size = 12))  + 
 xlab("") + 
 ylab("Variable Importance")

我可以得到这个情节:

我想做的是根据dose类别给抖动中的每个点上色。 我试过了但失败了

 ggboxplot(dat, x = "feat", y = "vimp",  add = "jitter", color = "dose")

正确的做法是什么?

实现所需结果的一个选项是通过 geom_jitter 层添加抖动点:


library(ggpubr)
#> Loading required package: ggplot2
library(ggplot2)

ggboxplot(dat, x = "feat", y = "vimp") +
  geom_jitter(aes(color = dose)) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5, size = 12)) +
  xlab("") +
  ylab("Variable Importance")