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

Deepayan Sarkar deep@y@n@@@rk@r @end|ng |rom gm@||@com
Sun Jan 22 18:43:36 CET 2023


On Sun, Jan 22, 2023 at 6:48 PM Laurent Rhelp <LaurentRHelp using free.fr> wrote:

> 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.
>

You have mistyped

  ans$left$ticks$at <- tick.at

as

  ans$left$tick$at <- tick.at

You might think that partial matching will help you, but partial matching
doesn't work for assignment (for obvious reasons). Consider

> foo <- list(ticks = list(at = 1:10))
> foo$tick$at <- "bar"
> str(foo)
List of 2
 $ ticks:List of 1
  ..$ at: int [1:10] 1 2 3 4 5 6 7 8 9 10
 $ tick :List of 1
  ..$ at: chr "bar"

I don't know if there is a good argument for this assignment to work at all
(other than that NULL and an empty list are conceptually equivalent), but
it is what it is.

This is why I tend to set options(warnPartialMatchDollar = TRUE). In this
case it would give you an inaccurate warning, but at least it will give you
a warning.

As for the range of the scales, these are determined by 'ylim' and
'prepanel', so you would need to specify one of these.

As you are using scales$axs = "i", you can simply provide ylim = c(0, NA),
which forces the lower limit to 0, and lets the upper limit be decided as
usual.

For finer control, you can specify a prepanel function, e.g.,

prepanel = function(x, y, ...) list(ylim = c(0, max(y)))

Hope this helps,

Best,
-Deepayan


>
> 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
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list