0

I am trying to add confidence intervals to the points displayed in the attached plot using ggplot2. (2 datasets, one X axis, 2 Y axes)

The points represent the mean value (val column in the dataframe). The data frame also contains the upper and lower values (L and U columns)

t<-data.frame(rep(c( "AA","AG","GG"),2), c(79.29365,47.67004,22.86688,28.41990,33.29651,36.76852),
           c(71.18651,45.97425,22.04625,27.23392,32.97892,36.57194),
           c(87.40079,49.36582,23.68751,29.60588,33.61411,36.96510),
           c(rep("a",3),c(rep("b",3))))
colnames(t)<-c("x","val","L","U","panel")

d1<-t[1:3,]
d2<-t[4:6,]

p <- ggplot(data = t, mapping = aes(x =t$x, y = t$val))
p <- p + facet_grid(panel ~ ., scale = "free")
p <- p + layer(data = d1, mapping=aes(x = d1$x, y = d1$val), geom = c( "point"), stat = "identity",size=3) 
p <- p + layer(data = d2, mapping =aes(x = d2$x, y = d2$val),geom = "point", stat = "identity",size=3)
p<-p+theme_bw()
p<-p+xlab("Genotypes")+ylab("Quantitative trait")+ggtitle("RS")
p<-p+scale_color_hue()
p

enter image description here

I have managed to plot the error bars in a simple ggplot graph for one dataset, however I am failing to correctly add them to both plots as I have tried to do above.

ggplot(d1, aes(x = d1$x, y = d1$val)) +  geom_point(colour="gray", size = 4)+geom_errorbar(aes(ymax = d1$U, ymin = d1$L),width=0.05) +theme_bw()

Thanks in advance.

1 Answer 1

1

Just add the following command to the existing plot and everything will work fine:

geom_errorbar(aes(ymax = U, ymin = L), width = 0.05) 

Note. I refer to U and L instead of d1$U and d1$L. The errorbar limits of both layers are already present in t.

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.