[R] axis() after image.plot() does not work except if points() is inserted between

Martin Maechler maechler at stat.math.ethz.ch
Tue Jul 25 13:03:08 CEST 2017


>>>>> Marc Girondot via R-help <r-help at r-project.org>
>>>>>     on Mon, 24 Jul 2017 09:35:06 +0200 writes:

    > Thanks for the proposition. As you see bellow, par("usr") is the same 
    > before and after the points() (the full code is bellow):
    > ....
    >> par("usr")
    > [1] -0.2500000  1.2500000 -0.1666667  1.1666667
    >> # if you remove this points() function, axis will show nothing.
    >> 
    >> points(1.5, 1.5, type="p")
    >> p2 <- par(no.readonly=TRUE)
    >> par("usr")
    > [1] -0.2500000  1.2500000 -0.1666667  1.1666667
    > ...

    > I can reproduce it in Ubuntu and MacosX R Gui and Rstudio (R 3.4.1).

    > Marc

    > Here is the code:
    > library(fields)
    > par(mar=c(5,4.5,4,7))
    > D <- matrix(c(10, 20, 25, 30, 12, 22, 32, 35, 13, 25, 38, 40), nrow=3)

    > p0 <- par(no.readonly=TRUE)
    > image.plot(D, col=rev(heat.colors(128)),bty="n", xlab="Lines",
    >           ylab="Columns", cex.lab = 0.5, 
    >           zlim=c(min(D, na.rm=TRUE),max(D, na.rm=TRUE)),
    >           las=1, axes=FALSE)
    > p1 <- par(no.readonly=TRUE)

    > par("usr")
    > par("xpd")

    > # if you remove this points() function, axis will show nothing.

    > points(1.5, 1.5, type="p")
    > p2 <- par(no.readonly=TRUE)
    > par("usr")
    > par("xpd")

    > ##########
    > axis(1, at=seq(from=0, to=1, length=nrow(D)), labels=0:2, cex.axis=0.5)
    > axis(2, at=seq(from=0, to=1, length=ncol(D)), labels=0:3, las=1,
    > cex.axis=0.5)

    > identical(p1, p2)

Have you ever carefully read the detailed help page about image.plot()?
   
I haven't, but a cursory reading already shows me that the
author of the function did this partly on purpose:

  > Side Effects:
  > 
  >      After exiting, the plotting region may be changed to make it
  >      possible to add more features to the plot. To be explicit,
  >      ‘par()\$plt’ may be changed to reflect a smaller plotting region
  >      that has accommodated room for the legend subplot.

Unfortunately, there it does _not_ mention the following :

>From looking at its code, and then re-reading parts of the help page,
I see that there is a 'graphics.reset' argument which you can
set to TRUE in such a case:

 image.plot(D, col=rev(heat.colors(128)),bty="n", xlab="Lines",
           ylab="Columns", cex.lab = 0.5, 
           zlim= range(D, na.rm=TRUE),
	   graphics.reset = TRUE, # <<<<< the solution
           las=1, axes=FALSE)

Also note that
     zlim = range(D ...)
is infinitely more elegant than
     zlim = c(min((D, ...), max(D, ...)))


Martin Maechler
ETH Zurich (and R core)



More information about the R-help mailing list