[R] Barplot with confidence intervals
    Marc Schwartz 
    marc_schwartz at me.com
       
    Fri Nov 27 14:04:42 CET 2009
    
    
  
On Nov 26, 2009, at 3:23 PM, Jonhnny Weslley wrote:
> I have a file in the following format:
>
> Scenario1 Scenario1CIL Scenario1CIU Scenario2 Scenario2CIL  
> Scenario2CIU
> 60 57 62 45 48 50
> 110 101 111 51 50 52
> 120 117 122 64 62 66
> 192 190 194 79 75 79
>
> where:
> First  column  = Scenario1 mean value
> Second  column  = Scenario1 Low Confidence Interval
> Third  column  = Scenario1 Upper Confidence Interval
> Fourth  column  = Scenario2 mean value
> Fifth  column  = Scenario2 Low Confidence Interval
> Sixth  column  = Scenario2 Upper Confidence Interval
>
> Then, I tried this:
>
> library(gplots)
> data <- read.table("data.file", header=T, sep=" ")
> legend <- c("line1","line2","line3","line4")
>
> ci.l <- as.matrix(c(c(data$Scenario1CIL), c(data$Scenario2CIL)))
> ci.u <- as.matrix(c(c(data$Scenario1CIU), c(data$Scenario2CIU)))
>
> barplot2(as.matrix(c(c(data$Scenario1), c(data$Scenario2))),  
> beside=TRUE,
>        legend=legend, ylim=c(0, 200), main="Experiment X", ylab="Total
> size",
>        font.main=4, cex.axis=1.2, cex.lab=1.5, cex.names=1.5,
>        plot.ci=TRUE, ci.l=ci.l, ci.u=ci.u, plot.grid=TRUE)
>
> But this code doesn't group the values in Scenario1 and Scenario2, as
> expected.
> All plotted bars are joined side by side.
>
> Considering the letter 'H' a bar, the expected result was: 'HHHH   
> HHHH'
> but the result of the above code was: 'HHHHHHHH' (no space between the
> Scenario1's values and Scenario2'values)  and no legends (Scenario1  
> and
> Scenario2).
>
> How I must to do?
>
> Thanks in advance!
The data structures that you are passing to barplot2() are single  
column matrices. They need to be multicolumn matrices, where each  
column is a group and as per ?barplot2, the ci.l and ci.u arguments  
need to have the same structure as the height argument.
Thus:
 > DF
   Scenario1 Scenario1CIL Scenario1CIU Scenario2 Scenario2CIL
1        60           57           62        45           48
2       110          101          111        51           50
3       120          117          122        64           62
4       192          190          194        79           75
   Scenario2CIU
1           50
2           52
3           66
4           79
height <- as.matrix(DF[, c(1, 4)])
ci.l <- as.matrix(DF[, c(2, 5)])
ci.u <- as.matrix(DF[, c(3, 6)])
 > height
      Scenario1 Scenario2
[1,]        60        45
[2,]       110        51
[3,]       120        64
[4,]       192        79
 > ci.l
      Scenario1CIL Scenario2CIL
[1,]           57           48
[2,]          101           50
[3,]          117           62
[4,]          190           75
 > ci.u
      Scenario1CIU Scenario2CIU
[1,]           62           50
[2,]          111           52
[3,]          122           66
[4,]          194           79
library(gplots)
legend <- c("line1", "line2", "line3", "line4")
barplot2(height, plot.ci = TRUE, ci.l = ci.l, ci.u = ci.u,
          beside = TRUE, legend = legend, ylim = c(0, 200),
          main = "Experiment X", ylab = "Total size",
          font.main = 4, cex.axis = 1.2, cex.lab = 1.5,
          cex.names = 1.5, plot.grid = TRUE)
BTW, once you get the plot created, you may want to note some errors  
in your CI values.
HTH,
Marc Schwartz
    
    
More information about the R-help
mailing list