【问题标题】:How to color jitter by category in ggboxplot如何在ggboxplot中按类别对抖动进行着色
【发布时间】:2022-01-05 04:35:29
【问题描述】:

我有以下数据框:

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")

正确的做法是什么?

【问题讨论】:

    标签: ggplot2 tidyverse ggpubr


    【解决方案1】:

    实现所需结果的一种方法是通过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")
    

    【讨论】:

      猜你喜欢
      • 2021-11-27
      • 1970-01-01
      • 2014-08-22
      • 2015-01-20
      • 2020-12-24
      • 1970-01-01
      • 2020-03-19
      • 2019-01-18
      • 1970-01-01
      相关资源
      最近更新 更多