[R] confidence intervals in dotplots in a for loop
ilai
keren at math.montana.edu
Wed Mar 7 19:14:24 CET 2012
On Tue, Mar 6, 2012 at 8:55 PM, Byerly, Mike M (DFG)
<mike.byerly at alaska.gov> wrote:
>
> estimates <-
c(67.42,30.49,32.95,23.53,10.26,6.03,23.53,0.93,50.72,24.2,25.84,18.54,
7.16,3.6,9.35,0.33,87.28,37.25,40.16,28.59,13.77,8.92,40.74,1.68,48.28,23.09,
24.49,17.7,6.63,3.28,7.79,0.26,91.63,38.74,41.6,29.74,14.49,9.51,44.16,1.88)
> estimates.m<- matrix(estimates, 8,5)
> colnames(estimates.m) <- list("est","lci90","uci90","lci95","uci95")
> id <- c(1,1,2,2,3,3,4,4)
> size <- c("All","All","Large","Large","Medium","Medium","Small","Small")
> gear <- rep(c("Camera","Dredge"),4)
> cds.est <- data.frame(id,size,gear,estimates.m)
# From this point try this instead:
library(lattice)
library(gridExtra)
dat <- by(cds.est,cds.est$id,function(x) x)
plots <- lapply(dat,function(dat){
dotplot(gear ~ est , data=dat, xlim = c(min(dat$lci95-10),
max(dat$uci95 +10)), xlab='Density (num / Nmi^2)',
main = as.vector(unique(dat$size)),
panel=function(x,y) {
panel.xyplot(x,y,pch=16,cex=1)
panel.segments(dat$lci95,as.numeric(y),dat$uci95,as.numeric(y),
lty=1, col=1)
panel.segments(dat$lci90,as.numeric(y),dat$uci90,as.numeric(y),
lty=1,lwd=4, col='grey60')
panel.xyplot(x,y,pch=16,cex=1.2,col='white')
panel.xyplot(x,y,pch=1,cex=1.1, col='black')
}
)
})
do.call(grid.arrange, plots)
> # cds.est data frame and stores the plots in a list. Since dotplot() is
> part of the # lattice package, I used grid.arrange to tile the plots.
In my opinion you are going about it the wrong way. You are not using
any of the functionality provided by lattice or dotplot for scaling
and panel arrangement. All you use are panel.xyplot and
panel.segments, manually calculating limits, labels and arranging the
plots yourself - you could have just done this in base graphics (and a
loop). But if that's how you want it to look...
HTH
>
>
>
> library(grid)
>
> library(gridExtra)
>
>
>
> id2 <- 1:max(cds.est$id)
>
> plots <- vector("list", length(id2))
>
> j <- 0
>
> for (i in id2) {
>
> dat <- cds.est[cds.est$id == i,]
>
> plots[[ j <- j+1]] <-
>
> dotplot(gear ~ est , data=dat, xlim = c(min(dat$lci95
> -10), max(dat$uci95 +10)), xlab='Density (num / Nmi^2)',
>
> main = as.vector(unique(dat$size)),
>
> panel=function(x,y) {
>
> panel.xyplot(x,y,pch=16,cex=1)
>
>
> panel.segments(dat$lci95,as.numeric(y),dat$uci95,as.numeric(y), lty=1,
> col=1)
>
>
> panel.segments(dat$lci90,as.numeric(y),dat$uci90,as.numeric(y), lty=1,
> lwd=4, col='grey60')
>
>
> panel.xyplot(x,y,pch=16,cex=1.2,col='white')
>
> panel.xyplot(x,y,pch=1,cex=1.1,
> col='black')
>
> })
>
>
>
> }
>
> do.call(grid.arrange, plots)
>
>
>
> # The script runs and produces all the plots with the correct estimates,
> but the CI's are not
>
> # plotting correctly. Does anyone have suggestions on what is causing
> this?
>
>
>
> Thanks, Mike
>
>
>
>
>
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list