Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lattice or latticeExtra combine multiple plots different yscaling (log10 and non-transformed)

Tags:

r

lattice

I have a multiple variable time series were some of the variables have rather large ranges. I wish to make a single-page plot with multiple stacked plots of each variable were some of the variables have a log10 y-axis scaling. I am relatively new to lattice and have not been able to figure out how to effectively mix the log10 scaling with non-transformed axes and get a publication quality plot. If print.trellis is used the plots are not aligned and the padding needs some work, if c.trellis is used the layout is good, but only the y-scaling from only one plot is used. Any suggestions for an efficient solution, where I can replicate the output of c.trellis using the different y-scaling for each (original) object? Example below:

    require(lattice)
    require(latticeExtra)

    # make data.frame
    d.date <- as.POSIXct(c("2009-12-15",  "2010-01-15",  "2010-02-15",  "2010-03-15", "2010-04-15"))                   
    CO2dat <- c(100,200,1000,9000,2000)
    pHdat <- c(10,9,7,6,7)
    tmp <- data.frame(date=d.date ,CO2dat=CO2dat ,pHdat=pHdat)

   # make plots 
   plot1 <- xyplot(pHdat ~ date, data=tmp
   , ylim=c(5,11)
   , ylab="pHdat"
   , xlab="Date"
   , origin = 0, border = 0
   , scales=list(y=list(alternating=1))
   , panel = function(...){
     panel.xyarea(...)
     panel.xyplot(...)
     }
     )

     # make plot with log y scale
     plot2 <- xyplot(CO2dat ~ date, data=tmp
            , ylim=c(10,10^4)
            , ylab="CO2dat"
            , xlab="Date"
            , origin = 0, border = 0
            , scales=list(y=list(alternating=1,log=10))
                                 , yscale.components = yscale.components.log10ticks
            , panel = function(...){
              panel.xyarea(...)
              panel.xyplot(...)
              # plot CO2air uatm
              panel.abline(h=log10(390),col="blue",type="l",...)
            }
            )

      # plot individual figures using split
      print(plot2, split=c(1,1,1,2), more=TRUE)
      print(plot1, split=c(1,2,1,2), more=F)   

      # combine plots (more convenient)
      comb <- c(plot1, plot2, x.same=F, y.same=F, layout = c(1, 2))

      # plot  combined figure
      update(comb, ylab = c("pHdat","log10 CO2dat"))
like image 515
Edward P. Morris Avatar asked Nov 05 '22 04:11

Edward P. Morris


1 Answers

Using @joran's idea, I can get the axes to be closer but not exact; also, reducing padding gets them closer together but changes the aspect ratio. In the picture below I've reduced the padding perhaps by too much to show the not exactness; if this close were desired, you'd clearly want to remove the x-axis labels on the top as well.

I looked into the code that sets up the layout and the margin on the left side is calculated from the width of the labels, so @joran's idea is probably the only thing that will work based on the printing using split, unless one were to rewrite the plot.trellis command. Perhaps the c method could work but I haven't found a way yet to set the scale components separately depending on the panel. That does seem more promising though.

mtheme <- standard.theme("pdf")
mtheme$layout.heights$bottom.padding <- -10
plot1b <- update(plot1, scales=list(y=list(alternating=1, at=5:10, labels=paste("    ",c(5:10)))))
plot2b <- update(plot2, par.settings=mtheme)
pdf(file="temp.pdf")
print(plot2b, split=c(1,1,1,2), more=TRUE)
print(plot1b, split=c(1,2,1,2), more=F)   

enter image description here

like image 169
Aaron left Stack Overflow Avatar answered Nov 07 '22 21:11

Aaron left Stack Overflow