[R] lattice xyplot: trouble about the use of yscale.components to start the yscale from zero

Laurent Rhelp L@urentRHe|p @end|ng |rom |ree@|r
Sun Jan 22 14:18:07 CET 2023


Dear RHelp-list,

  I want to choice my scale for every panel in a lattice graph according to
  the ylim range of every panel, with 10 ticks and with a start from 0. 
Also I want to plot a grid
   according to the y ticks (I did that in the panel argument with the 
panel.abline function) .
  So I decided to use the yscale.components argument and I write the 
script below.
  Before using the pretty function I introduce the 0 value in my range but
it does not work : the labels are not displayed and the panel.abline do 
nothing. I am not able to understand why.
   If I comment the line ylim <- c(0,max(lim)) the labels appear but of 
course they do not start from 0.

Thank you for your help.

Laurent


  --------------------------o<----------------------------------------------->o-----------------------------------------
library(lattice)
library(zoo)
##
## Rq : mydata is a zoo object
##
mydata <- structure(c(0.190991684047867, 0.186639474048368, 
0.188562286982088,
             0.187781290093149, 0.188242724296645, 0.190412570465429, 
0.188922969182772,
             0.194037520889193, 0.191973884842229, 0.197032138922638, 
1.03204611806177,
             1.02831610106898, 1.0280323738983, 1.03595907392095, 
1.03316162925952,
             1.04755124287765, 1.04403986225312, 1.05686325668364, 
1.04672225664295,
             1.05462971668107), dim = c(10L, 2L), dimnames = list(c("f1",
          "f1", "f1", "f1", "f1", "f1", "f1", "f1", "f1", "f1"), 
c("col1","col2"))
          , index = c(1.27904891967773, 1.27909898757935, 1.27915000915527
       , 1.27920007705688, 1.2792489528656, 1.27929902076721, 
1.27935004234314
       ,1.27939891815186, 1.27944993972778, 1.27950000762939), class = 
"zoo")

options(digits=17)

yscale.components.n.ticks <- function(lim,...){

   ans <- yscale.components.default(lim = lim,...)
   ylim <- lim
   ## I want to start from 0
   ylim <- c(0,max(lim))
   tick.at <- pretty(ylim, n=10, min.n = 9)
    cat("lim yscale : ",ylim,"\n")

   mylabels_y <- formatC(   tick.at
                          , format = "f"
                          , digits=3
                          , width=9
                          ,flag=" ")

   print(cbind( mylabels_y = mylabels_y, tick.at_y = tick.at))

   ans$left$tick$at <- tick.at
   ans$left$labels$at <- tick.at
   ans$left$labels$labels <- mylabels_y
   ans
}


xscale.components.n.ticks <- function(lim, ... ){

   ans <- xscale.components.default(lim = lim,...)
   tick.at <- pretty(lim,20,9)
   mylabels_x <- formatC(   tick.at
                          , format = "f"
                          , digits=6
                          , width=9
                          , flag=" ")

   print(cbind( mylabels_x = mylabels_x, tick.at_x = tick.at))

   ans$bottom$tick$at <- tick.at
   ans$bottom$labels$at <- tick.at
   ans$bottom$labels$labels <- mylabels_x
   ans
}

# to see the x values
time(mydata)

gr <- xyplot(  mydata
                , main = "title"
                , layout = c(1,2)
                , pch = 20
                , panel = function(...) {

                  ylim <- current.panel.limits()$ylim
                  xlim <- current.panel.limits()$xlim

                  ## I create here the same ticks that those created
                  ##  in xscale.components and yscale.components
                 ylim <- c(0,max(ylim))
                 cat("ylim panel : ",ylim,"\n")
                 y.tick.at <- pretty(ylim,10,9)
                 print(cbind(y.tick.at.panel= y.tick.at))

                  x.tick.at <- pretty(xlim,20,9)

                 panel.abline( h = y.tick.at, v = x.tick.at, col = 
"lightgrey")

                  panel.xyplot(...)
                }
##               , ylim = c(0,1.5)
                , scales = list(
                  y= list(   relation = "free"
                           , axs="i"
                           , rot = 45
                           ),
                  x = list(
                              axs="i"
                            , rot = 90
                           )
                )
                , yscale.components = yscale.components.n.ticks
                , xscale.components = xscale.components.n.ticks

)

print(gr)

-- 
Cet e-mail a été vérifié par le logiciel antivirus d'Avast.
www.avast.com



More information about the R-help mailing list