[R] Lattice: how to make x axis to appear on only one non-bottom plot?

Wladimir Eremeev wl at eimb.ru
Mon Jul 25 19:16:15 CEST 2005


Hi,

PM> Does the following do what you want?

PM> xyplot(<whatever>)
PM> trellis.focus("panel", 4, 2, clip.off=TRUE, highlight=FALSE)
PM> panel.axis("bottom", check.overlap=TRUE, outside=TRUE)

Yes, it does. Thank you very much!
With the exception, that first call must be
trellis.focus("panel", 3, 3, clip.off=TRUE, highlight=FALSE)

PM> (it would be easier to help if you could provide code that others can run)
I am sorry.
Here it is if any one still interested.
Any other comments concerned to the R code below will be appreciated.

====8<====

# The draw.key behaves slightly incorrect, and I have redefined it.
# (this was discussed in R-help on May 19-21, 2005)
source('../common_r_things/draw.key.r')

# Lattice tunings, like colors, cex-s, etc.
source('../common_r_things/wl_theme.r')

# I need also two additional curves on each plot panel,
# and a slope and its significance.
# The function below plots everything.

# Colors for additional curves, mentioned above.
# These variables are global, as the curves will appear in the plot key,
# and I would like to have a single place to define the plot and key parameters.
i1.col<-"tomato3"
i2.col<-"gray50"

plot.panel<-function(x,y,panel.counter,...){
# Plot additional curves. They are constant.
  i2<-c(-0.7,-1.7,0.2,0.8,-0.4,-1.7,-0.8,-0.4,0.9,0.7,0.5,0.7,1.1,1.5,-1.7)
  i2<-(i2-min(i2))*(max(y)-min(y))/(max(i2)-min(i2))+min(y)

  i1<-c(7.39230,-0.67956,2.65596, 4.44415, 0.73161, 1.66836,-2.52039, 2.52273,
           -2.51820,-0.89912, 1.89464,-3.26774,3.58680, 0.58910,-2.89624)
  i1<-(i1-min(i1))*(max(y)-min(y))/(max(i1)-min(i1))+min(y)

  panel.lines(x,y=i2,lwd=2,col=i2.col)
  panel.lines(x,y=i1,lwd=2,col=i1.col)

# Plot actual data.
  panel.xyplot(x,y,...);

# Calculate and print the slope and its significance.
  m<-lm(y~x);
  v<-pf(summary(m)$fstatistic[1],summary(m)$fstatistic[2],
        summary(m)$fstatistic[3]);
  if(v=="NaN") v<-0;
  if(v>0.99) {
    sign.exp<-expression(bold(S>0.99))
  }else{
    sign.exp<-substitute(expression(bold(S==V)),list(V=round(v,2)))
  }
  sl<-formatC(summary(m)$coefficients[2,1]*10,digits=2,format="f");
  err<-formatC(summary(m)$coefficients[2,2]*10,digits=2,format="f");

  grid.text(label=c(eval(substitute(expression(bold(paste("Slope: ",Sl%+-%Err~~dec^-1))),
                                    list(Sl=sl,Err=err))),
                    eval(sign.exp)),
             x=0.1,

     # The place for the text is chosen so, that it appears on the empty space.
     # In my case, first 6 panels have empty top and the rest 5 ones have empty bottom.
             y=if(panel.counter<7) c(1,0.85) else c(0.25,0.1),
             
             default.units="npc",just= c("left", "top")
            )
}

trellis.device("png",filename="general_plot2.png",theme=wl.theme,width=1000,height=1200)

# Data frame flow contains monthly mean data and some their aggregations
# That's why month variable varies from 1 to 29.
# Year varies from 1990 through 2004.
# However, now I need only a subset of the data.

print(
xyplot(A3.index~year|factor(month),
       strip=strip.custom(factor.levels=c("January",
             "Integral (Jan-Feb)","Integral (Jan-Mar)","Integral (Jan-Apr)","Integral (Jan-May)","Integral (Jan-Jun)",
             "Integral (Jan-Jul)","Integral (Jan-Aug)","Integral (Jan-Sep)","Integral (Jan-Oct)","Integral (Jan-Nov)")),
       subset=month %in% c(1,19:28),
       data=flow,as.table=TRUE,type=c("o","g","r"),layout=c(3,4),pch=1,col="black",
       ylab="flow index",xlab=NULL,
       scales=list(x=list(alternating=1),y=list(alternating=1)),
       between=list(x=0.2,y=0.2),
       panel=plot.panel,
       key=list(size=2,between=2,col="black",lty=1,border=TRUE,divide=1,
                x=0.95,y=0.07,corner=c(1,0),columns=1,
                lines=list(pch=c(32,1,32,32,32,32,32),c(0,1,0,1,0,1,0),lwd=c(0,1,0,2,0,2,0),
                           col=c(0,"black",0,i1.col,0,i2.col,0),type="b"),
                text=list(c("","Flow index","","i1 index(scaled)","",
                               "i2 index (scaled)",""),cex=1.1)
               )

      )
)
trellis.focus("panel", 3, 3, clip.off=TRUE, highlight=FALSE)
panel.axis("bottom", check.overlap=TRUE, outside=TRUE)

dev.off()

====8<====

---
Best regards,
Wladimir                mailto:wl at eimb.ru




More information about the R-help mailing list