tmap icon indicating copy to clipboard operation
tmap copied to clipboard

Legends and titles for RasterStacks in view mode

Open see24 opened this issue 3 years ago • 3 comments

This is a couple of related issues when plotting RasterStacks in "view" mode.

  1. There are two legends in view mode when as.layers = TRUE. This makes the plot quite crowded and is not helpful when the legends are the same. I tried free.scales.raster = FALSE thinking that might remove the second legend but it did not.
library(tmap)
library(raster)

data(land)

land_rast <- as(land, "Raster")

cover_cls <- land_rast$cover_cls

cover_cls2 <- reclassify(cover_cls, matrix(c(7, 8), ncol = 2))

stk <- stack(cover_cls, cover_cls2)

tm_examp <- tm_shape(stk,) +
  tm_raster(style = "cat")

tm_examp

# has two legends that are the same (including the titles)
cur_mode <- tmap_mode("view")

tm_examp+tm_facets(as.layers = TRUE, free.scales.raster = FALSE)
  1. it is not clear in the documentation how to name the layers when using a RasterStack. In tm_shape it says that name determines the name of the layer in view mode but it does not work for a raster stack. Using group in tm_raster seems to work but it gives a warning.
tm_examp2 <- tm_shape(stk, name = c("name1", "name2")) +
  tm_raster(style = "cat", title = c("title1", "title2"))+
  tm_layout(title = NA)+
  tm_facets(as.layers = TRUE, free.scales.raster = FALSE)

tm_examp2
# name has no effect 

tm_examp3 <- tm_shape(stk) +
  tm_raster(style = "cat", title = c("title1", "title2"), 
            group = c("group1", "group2"))+
  tm_layout(title = NULL)+
  tm_facets(as.layers = TRUE, free.scales.raster = FALSE)

tm_examp3
# group works but get this warning message 4 times
# 1: In if (is.na(gpl$raster.group)) { :
#     the condition has length > 1 and only the first element will be used
  1. I also can't figure out how to get rid of the global title in the top left in view mode. I tried NA and NULL above but they did not work.
tm_shape(stk) +
  tm_raster(style = "cat", title = c("title1", "title2"), 
            group = c("group1", "group2"))+
  tm_layout(title = "")+
  tm_facets(as.layers = TRUE, free.scales.raster = FALSE)

tm_shape(stk) +
  tm_raster(style = "cat", title = c("title1", "title2"), 
            group = c("group1", "group2"))+
  tm_layout(title = "globaltitle")+
  tm_facets(as.layers = TRUE, free.scales.raster = FALSE)

tmap_mode(cur_mode)

see24 avatar Feb 14 '22 17:02 see24

Re 2.: I just noticed that using group does not actually work. It changes the names in the layer list but when you uncheck the layers it has no effect.

see24 avatar Feb 15 '22 15:02 see24

Thanks for reporting these issues @see24

tmap is very buggy in this application. I will use this example in the development of tmap4.

Ideally, there should be a set of radio buttons rather than independent check boxes to select one layer.

@tim-salabim How would you tackle this (and the other issues) in mapview?

mtennekes avatar Mar 18 '22 21:03 mtennekes

Just a heads up that what used to produce a warning message now produces an error: Error in if (is.na(gpl$raster.group)) { : the condition has length > 1

For me this meant that maps in a shiny app just didn't show up and I wasn't seeing the error message. Not sure if that was a tmap:renderTmap issue or a shiny one but it made it quite confusing so I thought I would mention in case others have this issue

see24 avatar Jul 14 '23 20:07 see24