Showing posts with label error bar. Show all posts
Showing posts with label error bar. Show all posts

Thursday, May 2, 2013

RG#105: Line range or Crossbar plot


set.seed (1234)
require(ggplot2)
Xv <- c(rnorm (500, 10,3), rnorm (500, 50, 20), rnorm (500, 70, 20))
Yv <- c(rnorm (500, 10,3), rnorm (500, 70, 5), rnorm (500, 30, 5))
myd <- data.frame (Xv, Yv )

m <- ggplot(myd, aes(x = Xv, y = Yv)) +
  geom_point() + geom_density2d() + theme_bw()
  
m


myd <- data.frame (Xv = c("A", "B", "C", "E", "F"), Yv = c( 35, 35, 69, 15, 60),
                    errs = c(5, 10, 3, 5, 12))

se <- ggplot(myd, aes(Xv, Yv,
  ymin = Yv - errs, ymax= Yv + errs, colour = Xv))
  
# line range plot 

se + geom_linerange() + theme_bw()





se + geom_linerange()  + coord_flip() + theme_bw()

se + geom_crossbar(width = 0.5) + theme_bw()



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





Tuesday, April 30, 2013

RG#96: Basic point and line graph with error bars (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))
                   
require(ggplot2)
require(grid)
# line and point plot
f1 = ggplot(data = myd, aes(x = X, y = Y, group = group) )  # lesion becomes a classifying factor
f2 <- f1 + geom_errorbar(aes(ymin = Y - error, ymax = Y + error), width=0.3) +
geom_line() + geom_point(aes(shape=group, fill=group), size=5)

 f3 <- f2 +  scale_x_continuous("X (units)", breaks=1:12) +
     scale_y_continuous("Y (units)", limits = c(0, 40), breaks=seq(0, 40, by = 5)) +
     scale_shape_manual(values=c(24,21)) +
     scale_fill_manual(values=c("white","black")) +
     stat_abline(intercept=0, slope=0, linetype="dotted") +
     annotate("text", x=11, y=10, label="X") +
     theme_bw()

   optns <- 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(),
          legend.position = c(0.2,0.8),
          legend.title = element_blank(),
          legend.text = element_text(size=12),
          legend.key.size = unit(1.5, "lines"),
          legend.key = element_blank()
     )
f3 +  ggtitle ( "MY awsome plot for publication") + optns



Sunday, April 7, 2013

RG#26: centepede plot


set.seed(9001)
 p.value <- vector()
 dataf <- as.data.frame(matrix(rep(NA, 2000), nrow=100))
names(dataf) <- c(paste("V",1:19, sep=""), "y")
 for(i in 1:20) {
        dataf[,i] <- rnorm(20, 50, 10)
              }
#plot
require (plotrix)
seg<-get.segs(dataf)
 centipede.plot(seg, main= "A centipede plot",vgrid=c(10))
#adding grids to the plot
grid(nx = 10, ny = 19, col = "gray50")


Friday, April 5, 2013

RG#8: multiple arranged error bar plot (trallis type)

 # multiple arranged error bar plot (trallis type)
#data
env <- c(rep("E1", 9), rep("E2", 9))
trt <- c(rep (c("A101", "B234", "c777"), each = 3))
group <- c(rep (1:3, 6))
yld <- c(3,4,6, 3,8,4, 1,2,6, 3,4,5, 6,7,7, 1,4,8)
se <- c(0.3, 0.6, 0.3, 0.6, 0.1, 0.9, 0.21, 0.4,
    0.5, 0.2, 0.1, 0.3, 0.4, 0.2, 0.3, 0.4, 0.4, 0.3)
dataf <- data.frame (env, trt, group, yld, se)
limits <- aes(ymax = yld + se, ymin=yld - se)



# using ggplot2
require(ggplot2)
plt1 <- ggplot(dataf, aes(fill=factor(group), y=yld, x=trt))
plt1 + geom_bar(position= position_dodge(width=0.9)) +
geom_errorbar(limits, position= position_dodge(width=0.9), width=0.8) +
theme_bw( ) + facet_grid(.~env)


 

RG#7: bar chart with error bar


# data
  cmon<- c(600,250,150,135,350, 124)
cities <- c("A","B","C","D","E", "F")

error <- c(50, 33, 20, 15, 18, 12)
#plot
#using lattice plot package (often comes with base installation)
# using col pallette heat.colors so that we have different color for different bars

 require(lattice)
barchart (cmon~ cities, col= heat.colors(5), ylab = "CM level (ppm)", xlab = " cities ")


 
#using ggplot2
 require(ggplot2)
 plt <- qplot(cities, cmon, geom="bar", fill = cities, ylab = " carbon mono oxide level(ppm) ", xlab = " cities ") + theme_bw( )


# adding error bars
plt +  geom_errorbar(aes(ymin=cmon-error, ymax=cmon+error), width=.1, position=position_dodge(.1))