[R-sig-Geo] Fwd: Re: Adding text to rasterVis levelplot

Mauricio Zambrano Bigiarini mauricio.zambrano at ufrontera.cl
Sat Jul 22 02:10:40 CEST 2017


Thank you very much you both Thiago and Florian for your help.

Just for future searches on Google, the minimal example that allows
creating the plot i was looking for was:

------ START -----
library(rasterVis)

f <- system.file("external/test.grd", package="raster")
r <- raster(f)
s <- stack(r, r+500, r-500, r+200)

col.titles = c('col1','col2','','')
row.titles = c('row1','row2')

levelplot(s, layout=c(2,2),
          names.attr=col.titles,
          ylab=row.titles,
          scales = list(y = list(rot = 90) )
          )

------ END -----

However, the example provided by Florian opened up new possibilities
with the grid package for more customisation.

Kind regards,

Mauricio

Mauricio Zambrano-Bigiarini, PhD

=====================================
Department of Civil Engineering
Faculty of Engineering and Sciences
Universidad de La Frontera, Temuco, Chile
=====================================
"The ultimate inspiration is the deadline"
(Nolan Bushnell)
=====================================
Linux user #454569 -- Linux Mint user


On 21 July 2017 at 04:21, Florian Detsch
<florian.detsch at staff.uni-marburg.de> wrote:
> Ah, I forgot to include the definition of text vectors which must be
> inserted before the for() loop - sorry for that.
>
> ---
> ## labels
> cls <- c("col1", "col2")
> rws <- c("row1", "row2")
> ---
>
>
> -------- Forwarded Message --------
> Subject:        Re: [R-sig-Geo] Adding text to rasterVis levelplot
> Date:   Fri, 21 Jul 2017 10:05:08 +0200
> From:   Florian Detsch <florian.detsch at staff.Uni-Marburg.DE>
> To:     r-sig-geo at r-project.org
>
>
>
> Hi Mauricio,
>
> Complementing Thiago's answer using 'names.att', here is a more manual
> approach which might come in handy when a finer control over text
> annotations is required. It is built upon lattice::trellis.focus() which
> lets you select relevant panels of your trellis graph in an iterative
> manner.
>
> ---
> p <- levelplot(s, layout = c(2, 2), names.att = rep("", 4),
>                 scales = list(y = list(rot = 90)))
>
> library(grid)
>
> png("~/rasterVis.png", width = 14, height = 16, units = "cm", res = 300L)
> grid.newpage()
> print(p, newpage = FALSE)
>
> ## loop over panels to be labelled (ie 1:3)
> panels <- trellis.currentLayout()
> for (i in 1:3) {
>
>    # focus on current panel of interest and disable clipping
>    ids <- which(panels == i, arr.ind = TRUE)
>    trellis.focus("panel", ids[2], ids[1], clip.off = TRUE)
>
>    # add labels
>    if (i %in% c(1, 3)) {
>      if (i == 1) {
>        grid.text(cls[1], x = .5, y = 1.1) # add 'col1'
>        grid.text(rws[1], x = -.35, y = .5, rot = 90) # add 'row1'
>      } else {
>        grid.text(rws[2], x = -.35, y = .5, rot = 90) # add 'row2'
>      }
>    } else {
>      grid.text(cls[2], x = .5, y = 1.1) # add 'col2'
>    }
>
>    trellis.unfocus()
> }
>
> dev.off()
> ---
>
> A very similar use case, from which the above approach definitely took
> some inspiration, can be found here:
> https://stackoverflow.com/questions/7649737/is-it-possible-to-update-a-lattice-panel-in-r.
> Have a look at ?grid.text() and ?gpar() to learn more about
> customization options.
>
> Cheers,
> Florian
>
> --
> Dr. Florian Detsch
> Environmental Informatics
> Department of Geography
> Philipps-Universität Marburg
> Deutschhausstraße 12
> 35032 (parcel post: 35037) Marburg, Germany
>
> Phone: +49 (0) 6421 28-25323
> Web: http://www.uni-marburg.de/fb19/fachgebiete/umweltinformatik/detschf/index.html
>
>
> On 20.07.2017 05:07, Thiago V. dos Santos via R-sig-Geo wrote:
>> You can manually define the names that you want and pass them as arguments to 'names.attr' (the name of each panel) and 'ylab' (the title of the y axis).
>> Following your example:
>> col.titles = c('col1','col2','','')
>> row.titles = c('row1','row2')
>>
>> levelplot(s, layout=c(2,2),
>>            names.attr=col.titles,
>>            ylab=row.titles)
>>
>> HTH,
>> Greetings, -- Thiago V. dos Santos
>> PhD studentLand and Atmospheric ScienceUniversity of Minnesota
>>
>>
>> On Wednesday, July 19, 2017, 11:13:31 AM CDT, Mauricio Zambrano Bigiarini <mauricio.zambrano at ufrontera.cl> wrote:
>>
>> Dear all,
>>
>> I would like to add custom text labels for the columns and rows of a
>> levelplot object created with he rasterVis package, similar to the
>> c("Fall", "Winter", "Spring", Summer") used to label the rows and the
>> c("a",..., "h") used to label the columns in the plot shown int he
>> following link:
>>
>> https://stackoverflow.com/questions/43007241/how-to-add-text-to-a-specific-fixed-location-in-rastervis-levelplot
>>
>> In particular, how can I add c("col1", "col2") and c("row1", "row2")
>> as column and row headers, respectively, to the following example
>> plot:
>>
>> --------- START--------
>> library(rasterVis)
>> f <- system.file("external/test.grd", package="raster")
>> r <- raster(f)
>> s <- stack(r, r+500, r-500, r+200)
>> levelplot(s, layout=c(2,2), names.att=rep("",4))
>> --------- END--------
>>
>>
>> Thanks in advance for your help,
>>
>>
>> Mauricio Zambrano-Bigiarini, PhD
>>
>> =====================================
>> Department of Civil Engineering
>> Faculty of Engineering and Sciences
>> Universidad de La Frontera, Temuco, Chile
>> =====================================
>> "The ultimate inspiration is the deadline"
>> (Nolan Bushnell)
>> =====================================
>> Linux user #454569 -- Linux Mint user
>>
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>
>         [[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

-- 
La información contenida en este correo electrónico y cualquier anexo o 
respuesta relacionada, puede contener datos e información confidencial y no 
puede ser usada o difundida por personas distintas a su(s) destinatario(s). 
Si usted no es el destinatario de esta comunicación, le informamos que 
cualquier divulgación, distribución o copia de esta información constituye 
un delito conforme a la ley chilena. Si lo ha recibido por error, por favor 
borre el mensaje y todos sus anexos y notifique al remitente.



More information about the R-sig-Geo mailing list