19 Apr 2011

Lattice Plots - Usage of Panel Functions - Different Axes For Panel-Rows - Alternating Axis Titles

I present code for a stacked graph with common axes only for panels of the same row and with axis titles at different sides. This admittedly took me days (because i had not much of a clue how to use lattice), but eventually I did it and maybe someone can use this for his/her own purpose:









library(lattice)

y1 <- rnorm(120,100,10)
y2 <- rnorm(120,10,1)

facs <- expand.grid(Sites=rep(c("Site I","Site II",
                       "Site III"),20),Treatment = c("A","B"))

trellis.par.set(clip = list(panel = "off",strip = "off"),
   layout.widths = list(right.padding = 10,
   left.padding = 8))

PanFun<-function(...){
   panel.dotplot(...)
     if(is.element(panel.number(),1:2))
       {at<-pretty(c(min(y1),max(y1)))
        panel.axis("right",at = at,outside = T,
        labels = F,half = F)}
     if(panel.number() == 3)
       {at<-pretty(c(min(y1),max(y1)))
        panel.axis("right",at = at,outside = T,
        labels = T,half = F)}
     if(panel.number() == 4)
       {at<-pretty(c(min(y2),max(y2)))
        panel.axis("left",at = at,outside = T,
        labels = T,half = F)}
     if(is.element(panel.number(),5:6))
       {at<-pretty(c(min(y2),max(y2)))
        panel.axis("left",at = at,outside = T,
        labels = F,half = F)}}

dotplot(y1+y2 ~ Treatment|Sites,
        outer = TRUE,data = facs,
        scales = list(
             x = list(rot = 0, tck=c(1,0)),
             y = list(draw=F,relation="free",
        limits = list(range(y1),range(y1),range(y1),
                    range(y2),range(y2),range(y2)))),
        ylab = list("response one (y1)", y = 0.75, x = -4),
        ylab.right = list("response two (y2)", y = 0.25, 
                          rot = 270, x = 4),
        xlab = c("Site 1", "Site 2","Site 3"),
        strip = FALSE, panel=PanFun)

R package citation:
Sarkar, Deepayan (2008) Lattice: Multivariate Data Visualization with
R. Springer, New York. ISBN 978-0-387-75968-5

See also the thread:
http://www.mail-archive.com/r-help@r-project.org/msg107565.html

..and examples from:
http://lmdvr.r-forge.r-project.org/figures/figures.html

2 comments :

  1. See also http://latticeextra.r-forge.r-project.org/#combineLimits&theme=default

    I just discovered that I have re-invented the wheel at least twice this month now that I've read through the contents of that package more thoroughly.

    ReplyDelete