【问题标题】:Violin scatter plots with dependent variables in RR中带有因变量的小提琴散点图
【发布时间】:2020-07-23 15:09:29
【问题描述】:

这是我的数据集“细胞计数”:

Treatment DAPI DAPO DAPU 1 DMSO 20 30 40 2 DMSO 24 26 42 3 DMSO 23 24 39 4 EPZ 0.5uM 10 25 22 5 EPZ 0.5uM 12 24 22 6 EPZ 0.5uM 14 24 30 7 EPZ 0.5uM 20 19 32

我正在尝试创建一个小提琴散点图,x 轴上是治疗组,y 轴上是细胞计数(DAPI、DAPO、DAPU),所以我总共应该有六个小提琴图。

p1<-ggplot(cellcounts,aes(x=Treatment,y=DAPI)) +geom_violin(aes(colour="DAPI"),alpha=0.5)+ geom_jitter(data=cellcounts,shape=16,position=position_jitter(0.2),colour="blue")+ geom_boxplot(width=0.1,fill="grey")

获得带有箱线图和散点图的小提琴图shown here

我还可以为 DAPO 和 DAPU 数据添加小提琴图 shown here with

p2<-p1+geom_violin(aes(Treatment,DAPO,colour="DAPO"),alpha=0.5)

p3<-p2+geom_violin(aes(Treatment,DAPU,colour="DAPU"),alpha=0.5)

如果我现在尝试添加抖动图

p3+geom_jitter(data=cellcounts,shape=16,position=position_jitter(0.2),colour="blue")

它只为 DAPI 数据添加,因为这是 ggplot 需求 (?) 我如何也为 DAPO 和 DAPU 数据添加数据点?

我还希望 DAPI Violin 和抖动点为蓝色、DAPO 绿色和 DAPU 红色。

感谢您的宝贵时间

【问题讨论】:

标签: r ggplot2 scatter-plot boxplot violin-plot


【解决方案1】:

您可以尝试以下方法,首先将数据转换为长格式,这样您就无需使用命令指定每一列:

library(ggplot2)
library(tidyr)

p = ggplot(pivot_longer(cellcounts,-Treatment),aes(Treatment,value))+
geom_violin(aes(col=name),position=position_dodge(width=0.3)) +
geom_point(aes(shape=name),col="blue",position=position_dodge(width=0.3))

打印(p)

正如您在上面看到的,我将小提琴绘制为“闪避”,这意味着不在同一条垂直线上,并且要对齐您的数据点,通常我们会使用相同的颜色。由于您希望所有点都是蓝色的,所以我只是使用 shape 作为变量来让它们对齐。

如果您希望它们具有相同的形状,请执行以下操作:

p+scale_shape_manual(values=c(20,20,20))

【讨论】:

    猜你喜欢
    • 2017-08-10
    • 1970-01-01
    • 2019-02-16
    • 2017-08-02
    • 2019-03-03
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    相关资源
    最近更新 更多