[R-sig-Geo] Layering text outside the plot region in a levelplot

Oscar Perpiñan oscar.perpinan at upm.es
Fri Feb 7 14:14:06 CET 2014


Hi, Jooil

You are using a mailing list devoted to spatial data. You should use
R-help (https://stat.ethz.ch/mailman/listinfo/r-help) or stackoverflow
(http://stackoverflow.com/tags/r) for this kind of questions.
Regarding your question, latticeExtra::layer modifies the panel
function used by levelplot. Therefore, what you include in layer()
will be displayed inside the panel area. This should work:

png("output.png")
p
grid.text('here and there', x=.98, y=.5, rot=-90,
          gp = gpar(col=4,
              fontfamily="HersheyGothicEnglish", cex=2))
dev.off()


Best,

Oscar.
-----------------------------------------------------------------
Oscar Perpiñán Lamigueiro
Dpto. Ingeniería Eléctrica (ETSIDI-UPM)
Grupo de Sistemas Fotovoltaicos (IES-UPM)
URL: http://oscarperpinan.github.io
Twitter: @oscarperpinan


2014-02-07 Jooil Kim <kji2080 at gmail.com>:
> Hello list,
>
> I've always wanted to figure out how to put legend captions in my
> levelplots, and so when I came across this example today (from
> http://r.789695.n4.nabble.com/scale-caption-on-levelplot-td3072400.html):
>
>    library(grid)
>    x <- sort(rnorm(100,50,10))
>    y <- sort(runif(100,0,20))
>    d <- expand.grid(x=x, y=y)
>    d$z <- x + y
>    graphics.off()
>    p <- levelplot(z ~ x*y, d,
>              par.settings=list(
>                layout.widths=list(right.padding=4),
>                layout.heights=list(top.padding=6)),
>              colorkey = TRUE)
>    print(p)
>
>    ## now add the text
>    grid.text('here and there', x=.98, y=.5, rot=-90,
>      gp = gpar(col=4,
>          fontfamily="HersheyGothicEnglish", cex=2))
>
> This works and is fine, except in my code I use the layer() function and
> save to a png, which changes the code as such:
>
>    library(grid)
>    library(latticeExtra)
>    x <- sort(rnorm(100,50,10))
>    y <- sort(runif(100,0,20))
>    d <- expand.grid(x=x, y=y)
>    d$z <- x + y
>    graphics.off()
>    p <- levelplot(z ~ x*y, d,
>              par.settings=list(
>                layout.widths=list(right.padding=4),
>                layout.heights=list(top.padding=6)),
>              colorkey = TRUE)
>    #print(p)
>
>    ## now add the text layer
>    p <- p + layer(grid.text('here and there', x=.98, y=.5, rot=-90,
>      gp = gpar(col=4,
>          fontfamily="HersheyGothicEnglish", cex=2)))
>
>    png("output.png")
>    plot(p)
>    dev.off()
>
> Now, with these changes, the text moves inside the plot region, instead of
> outside in the margins. It seems like once the grid.text is  converted by
> the layer(), the x and y positions become relative to the plot region
> instead of the whole graph, and any values of x and y larger than 1 will
> move the text out of view.
>
> So, is there a way I can use layer() to add text in the margins outside the
> plot region? Or are there suggestions on other ways in which I could add
> text to a plot margin?
>
> Thanks,
>
> Jooil
>
>
> --
> #############################################
> Jooil Kim
> Postdoc Fellow
> Scripps Institution of Oceanography, UC San Diego
> Mailing address:
> 9500 Gilman Drive # 0244
> La Jolla, CA 92093-0244, USA
> For FedEx/UPS:
> 8675 Discovery Way, Vaughan Hall Rm. 447
> La Jolla, CA 92037, USA
> jooilkim at ucsd.edu (kji2080 at gmail.com)
> tel)+1-858-534-2599
> fax)+1-858-455-8306
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list