1
votes

Here is my dataset 'cellcounts':

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

I am trying to create a violin scatter plot with the Treatment groups on the x-axis and the cell counts (DAPI, DAPO, DAPU) on the y-axis, so in total I should have six violin plots.

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

Obtains a Violin plot with box plot and scatter point shown here

I can add the Violin plots for the DAPO and DAPU data also 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)

If I try to add the jitter plot now with

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

it is only added for the DAPI data as this is what's used the the ggplot demand (?) how can I add the data points for the DAPO and DAPU data also?

I also want DAPI Violin and jitter points to be blue, DAPO green and DAPU red.

Thank you for your time

1
You might be interested in this stackoverflow.com/questions/52630293/…Tung

1 Answers

0
votes

You can try something like below, first you pivot the data into long format so you don't need to specify each column with a command:

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

print(p)

enter image description here

As you can see above, I plotted the violins "dodged", meaning not on the same vertical line, and for your data points to be aligned, normally we would use color to be the same. Since you wanted blue for all dots, I just use shape as a variable to allow them to align.

If you like them to be the same shape, just do:

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