[R-sig-Geo] Adding text to rasterVis levelplot

Florian Detsch florian.detsch at staff.uni-marburg.de
Fri Jul 21 10:05:08 CEST 2017


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
>



More information about the R-sig-Geo mailing list