Adding mean and error onto scatterplot data
1
0
Entering edit mode
3.7 years ago

Hi there!

I have a small dataset that contains the following: SNP Count (numerical value), Caste (categorical value, "nymph", "worker", "soldier"), and Location (categorical value, "boston", "raleigh", "toronto"):

Location    Caste   SNP Count
Toronto Worker  150536
Raleigh Worker  156609
Boston  Worker  118797
Toronto Soldier 150585
Raleigh Soldier 155153
Boston  Soldier 128518
Raleigh Nymph   155056
Boston  Nymph   131711

I used ggplot2 in R studio to create a graph to represent all of the data:

df2 <- read.csv("VariantsTable.csv")
p<-ggplot(df2,aes(x=df$Caste,y=df$SNP.Count))
Location<-factor(df2$Location)
p<-p+geom_point(aes(colour=Location),size=3) + theme_bw()
p+xlab("Caste")+ylab("SNP Count")

Which gives this output: enter image description here

I want to add three values onto this plot, a mean for each of the caste groups (with error bars). So there would be an extra dot within each caste group to represent the mean and SD of the existing dots. I have been trying to manipulate existing code to do so, but have not succeeded. Does anyone have any suggestions? Thanks.

rstudio rna-seq ggplot • 916 views
ADD COMMENT
2
Entering edit mode
3.7 years ago

This should do it.

ggplot(df, aes(x=Caste, y=SNP_Count)) +
  geom_point(stat="summary", fun=mean, size=2) +
  geom_errorbar(stat="summary", fun.max=~mean(.x) + sd(.x), fun.min=~mean(.x) - sd(.x), width=0.1) +
  geom_point(aes(color=Location), size=3) +
  theme_bw() +
  xlab("Caste") +
  ylab("SNP Count")

enter image description here

ADD COMMENT
0
Entering edit mode

Thank you so much! I was playing around with geom_point and geom_error arguments but had some problems I couldn't work out (I am pretty new to this). This is awesome, thanks once again.

ADD REPLY

Login before adding your answer.

Traffic: 1138 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6