[R] xyplot() and confidence interval polygons

Martin Ralphs martin.ralphs at ons.gsi.gov.uk
Thu Oct 13 16:04:17 CEST 2011


Dear all,

I'm trying to replicate the panel function behaviour that Deepayan and
others discuss here:
https://stat.ethz.ch/pipermail/r-help/2007-April/130779.html

and Oscar Lamigueiro refers to here and provides sample code for:
http://www.r-bloggers.com/confidence-bands-with-lattice-and-r/

where a polygon is generated around lines on panels to display the
confidence interval bounds for that line as a polygon around the line.  My
data consists of counts by ageband and confidence intervals around those
counts.  I cannot seem to get the prepanel function that the above threads
describe to function with my data - I get a message that "Argument is not
interpretable as logical".  I am clearly doing something wrong, but I can't
work out where.

Here is some sample code and pseudo-data that shows the problem, which is
somewhere in the panel.superpose call or the my.panel.bands function as I
have defined it.  Any guidance much appreciated!

Martin Ralphs
Methodology Directorate
Office for National Statistics
UK

# Setup package libraries...
library(lattice)

# Setup sample dataset...
area <- c(rep("A1",7),rep("A2",7),rep("A3",7),rep("A4",7))
areanm <- c(rep("ZONE1",7),rep("ZONE2",7),rep("ZONE3",7),rep("ZONE4",7))
ageband <- c("25-29","30-34","35-39","40-44","45-49","50-54","55-59")
ageband <- rep(ageband,4)
est <-
c(1153,917,691,337,53,144,108,8454,6912,5423,7158,5728,4500,3565,17427,13753,
14466,14464,10303,9231,9248,7807,7814,9259,9492,8568,7339,5939)
lci <-
c(521,380,119,0,0,0,0,6693,5271,4046,5536,4220,3185,2340,14907,11497,
12140,12140,8401,7409,7320,6069,6157,7320,7499,6693,5714,4481)
uci <-
c(1879,1620,1080,639,296,477,296,10307,8528,6954,8863,7180,5815,4660,20093,
16102,16860,16860,12399,11190,11080,9531,9642,11080,11300,10307,9086,7518)

data1 <- as.data.frame(cbind(area,areanm,ageband),stringsAsFactors=TRUE)
data2 <- cbind(est,lci,uci)
data <- cbind(data1,data2)

# Setup colour palette (blue, red, green)
pallc1 <- c("#2F54CB","#BE2C2C","#326C39")

# Divide populations by 1000
data$est <- data$est / 1000
data$lci <- data$lci / 1000
data$uci <- data$uci / 1000

my.panel.bands <-
    function(x,y,upper,lower,subscripts, ...,font,fontface)
{
    upper <- upper[subscripts]
    lower <- lower[subscripts]
    panel.polygon(c(x, rev(x)), c(upper,
rev(lower)),col=fill,border=FALSE,...)
}

ptitle <- paste("Test Plot\n","Estimate vs Other Sources in Region",sep="")
lp1 <- xyplot(data$ageband ~ data$est | data$areanm,
      main=list(label=ptitle,cex=1),
      layout=c(2,2),

      panel=function(x,y,...){
            panel.superpose(x,y,upper=data$uci,lower=data$lci,
            panel.groups='my.panel.bands',type="l",col='gray',...)
            panel.xyplot(x,y,...,col=pallc1,type="l",lty=1,lwd=2)
      },
      as.table=TRUE,
      grid="h",
      key=list(   text=list(c("Estimate")),
                        space="right",
                        between=0.5,
                        size=2,
                        fontface="plain",
                        cex=0.7,
                        lines=list(col=pallc1,lty=1,lwd=2)
                  ),
      aspect=565/800,
      ylab="Age Band",
      xlab="Count (Thousands)",
      scales=list(
      y=list(fontface="plain",cex=0.6),
      x=list(fontface="plain",relation="free",cex=0.6)),

strip=strip.custom(bg="#FFFFFF",par.strip.text=list(fontface="plain",cex=0.55)))

plot(lp1)


For the latest data on the economy and society consult National Statistics at http://www.ons.gov.uk

*********************************************************************************


Please Note:  Incoming and outgoing email messages are routinely monitored for compliance with our policy on the use of electronic communications
*********************************************************************************


Legal Disclaimer  :  Any views expressed by the sender of this message are not necessarily those of the Office for National Statistics
*********************************************************************************


The original of this email was scanned for viruses by the Government Secure Intranet virus scanning service supplied by Cable&Wireless Worldwide in partnership with MessageLabs. (CCTM Certificate Number 2009/09/0052.) On leaving the GSi this email was certified virus free.
Communications via the GSi may be automatically logged, monitored and/or recorded for legal purposes.



More information about the R-help mailing list