Wednesday, May 1, 2013

RG#97: Error bar plot with significance (line connecting) - publication purpose


myd <- data.frame (X = c(1:12,1:12),
                   Y = c(8, 12, 13, 18,  22, 16, 24, 29,  34, 15, 8, 6,
                         9, 10, 12, 18, 26, 28, 28, 30, 20, 10, 9, 9),
                   group = rep (c("A-group", "B-group"), each = 12),
                   error = rep (c(2.5, 3.0), each = 12))

plot1 = ggplot(data = myd, aes(x=X, y=Y, fill=group, width=0.8) ) +
     geom_errorbar(aes(ymin=Y, ymax=Y+error, width = 0.2), position=position_dodge(width=0.8)) +
     geom_bar(stat="identity", position=position_dodge(width=0.8)) +
     geom_bar(stat="identity", position=position_dodge(width=0.8), colour="black", legend=FALSE) +
     scale_fill_manual(values=c("grey70", "white")) +
     scale_x_discrete("X", limits=c(1:12)) +
     scale_y_continuous("Y (units)", expand=c(0,0), limits = c(0, 40), breaks=seq(0, 40, by=5)) + ggtitle ("My nice plot") +
     theme_bw() +
    theme( plot.title = element_text(face="bold", size=14),
          axis.title.x = element_text(face="bold", size=12),
          axis.title.y = element_text(face="bold", size=12, angle=90),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          axis.text.y=element_text(angle=90, hjust=0.5),
          legend.title = element_blank(),
          legend.position = c(0.85,0.85),
          legend.key.size = unit(1.5, "lines"),
          legend.key = element_rect()
     )

print(plot1)





   # connecting segments
   plot1 + geom_segment(aes(x=5.7, y=16, xend=5.7, yend=28.5), col = "gray80")+
   geom_segment(aes(x=5.7, y=28.5, xend=6.1, yend=28.5), col = "gray80")      +
   geom_segment(aes(x=6.1, y=28.5, xend=6.1, yend=28), col = "gray80") +
      annotate("text", x=5.7, y=31.2, label="***")





No comments:

Post a Comment

Note: Only a member of this blog may post a comment.