Tmap: Categorical rasters are empty

Created on 18 Apr 2020  ·  9Comments  ·  Source: mtennekes/tmap

library(tmap)
library(sf)
#> Linking to GEOS 3.7.1, GDAL 2.3.2, PROJ 5.2.0
library(raster)
#> Loading required package: sp
library(spData)
library(spDataLarge)
california = dplyr::filter(us_states, NAME == "California")
california_borders = st_cast(california, "MULTILINESTRING")
raster_template2 = raster(extent(california), resolution = 0.5,
                          crs = st_crs(california)$proj4string)

california_raster1 = rasterize(california_borders, raster_template2)

# works
plot(california_raster1)

# fails
tm_shape(california_raster1) + 
  tm_raster()

Created on 2020-04-18 by the reprex package (v0.3.0)

Related: https://github.com/Robinlovelace/geocompr/issues/511 and https://github.com/Robinlovelace/geocompr/issues/514

Most helpful comment

That's tmap :-) Will look into it soon.

And yes, a black box around the legend items would be better in this example, but not in general, since black lines will influence the perception of colors, especially with tiny grid cells. Normally, I would advise to draw the legend outside the map with + tm_layout(legend.outsize = TRUE).

All 9 comments

Also:

library(raster)
#> Loading required package: sp
library(tmap)
library(spData)
tm_shape(grain) + 
  tm_raster()
#> Linking to GEOS 3.7.1, GDAL 2.3.2, PROJ 5.2.0

Created on 2020-04-18 by the reprex package (v0.3.0)

Ok, should be fixed now, but it's more a workaround.

@edzer There seems to be a little problem with conversion from factorized rasters to stars:

california_stars1 = stars::st_as_stars(california_raster1)
california_stars1[[1]]
# Error in as.character.factor(x) : malformed factor
levels(california_stars1[[1]])
# character(0)

Similar for the other example:

grain_stars <- stars::st_as_stars(grain)
grain_stars[[1]]
# Error in as.character.factor(x) : malformed factor
levels(grain_stars[[1]])
# character(0)

The workaround I did, was: when levels() returns an empty vector, regard the variable as integer.

Should work now from the stars side.

Thanks. Works now.

There still seems to be an issue:

library(tmap)
library(spData)
tm_shape(grain) +
  tm_raster()
#> Linking to GEOS 3.8.1, GDAL 3.0.4, PROJ 6.3.1

Created on 2020-05-09 by the reprex package (v0.3.0)

Session info

devtools::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 4.0.0 (2020-04-24)
#>  os       Fedora 32 (Thirty Two)      
#>  system   x86_64, linux-gnu           
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       Europe/Warsaw               
#>  date     2020-05-09                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package      * version    date       lib source                              
#>  abind          1.4-5      2016-07-21 [1] CRAN (R 4.0.0)                      
#>  assertthat     0.2.1      2019-03-21 [1] CRAN (R 4.0.0)                      
#>  backports      1.1.6      2020-04-05 [1] CRAN (R 4.0.0)                      
#>  base64enc      0.1-3      2015-07-28 [1] CRAN (R 4.0.0)                      
#>  callr          3.4.3      2020-03-28 [1] CRAN (R 4.0.0)                      
#>  class          7.3-16     2020-03-25 [2] CRAN (R 4.0.0)                      
#>  classInt       0.4-3      2020-04-07 [1] CRAN (R 4.0.0)                      
#>  cli            2.0.2      2020-02-28 [1] CRAN (R 4.0.0)                      
#>  codetools      0.2-16     2018-12-24 [2] CRAN (R 4.0.0)                      
#>  colorspace     1.4-1      2019-03-18 [1] CRAN (R 4.0.0)                      
#>  crayon         1.3.4      2017-09-16 [1] CRAN (R 4.0.0)                      
#>  crosstalk      1.1.0.1    2020-03-13 [1] CRAN (R 4.0.0)                      
#>  curl           4.3        2019-12-02 [1] CRAN (R 4.0.0)                      
#>  DBI            1.1.0      2019-12-15 [1] CRAN (R 4.0.0)                      
#>  desc           1.2.0      2018-05-01 [1] CRAN (R 4.0.0)                      
#>  devtools       2.3.0      2020-04-10 [1] CRAN (R 4.0.0)                      
#>  dichromat      2.0-0      2013-01-24 [1] CRAN (R 4.0.0)                      
#>  digest         0.6.25     2020-02-23 [1] CRAN (R 4.0.0)                      
#>  e1071          1.7-3      2019-11-26 [1] CRAN (R 4.0.0)                      
#>  ellipsis       0.3.0      2019-09-20 [1] CRAN (R 4.0.0)                      
#>  evaluate       0.14       2019-05-28 [1] CRAN (R 4.0.0)                      
#>  fansi          0.4.1      2020-01-08 [1] CRAN (R 4.0.0)                      
#>  fs             1.4.1      2020-04-04 [1] CRAN (R 4.0.0)                      
#>  glue           1.4.0      2020-04-03 [1] CRAN (R 4.0.0)                      
#>  highr          0.8        2019-03-20 [1] CRAN (R 4.0.0)                      
#>  htmltools      0.4.0      2019-10-04 [1] CRAN (R 4.0.0)                      
#>  htmlwidgets    1.5.1      2019-10-08 [1] CRAN (R 4.0.0)                      
#>  httr           1.4.1      2019-08-05 [1] CRAN (R 4.0.0)                      
#>  KernSmooth     2.23-16    2019-10-15 [2] CRAN (R 4.0.0)                      
#>  knitr          1.28       2020-02-06 [1] CRAN (R 4.0.0)                      
#>  lattice        0.20-41    2020-04-02 [2] CRAN (R 4.0.0)                      
#>  leafem         0.1.1      2020-04-05 [1] CRAN (R 4.0.0)                      
#>  leaflet        2.0.3      2019-11-16 [1] CRAN (R 4.0.0)                      
#>  leafsync       0.1.0      2019-03-05 [1] CRAN (R 4.0.0)                      
#>  lifecycle      0.2.0      2020-03-06 [1] CRAN (R 4.0.0)                      
#>  lwgeom         0.2-3      2020-04-12 [1] CRAN (R 4.0.0)                      
#>  magrittr       1.5        2014-11-22 [1] CRAN (R 4.0.0)                      
#>  memoise        1.1.0      2017-04-21 [1] CRAN (R 4.0.0)                      
#>  mime           0.9        2020-02-04 [1] CRAN (R 4.0.0)                      
#>  munsell        0.5.0      2018-06-12 [1] CRAN (R 4.0.0)                      
#>  pkgbuild       1.0.8      2020-05-07 [1] CRAN (R 4.0.0)                      
#>  pkgload        1.0.2      2018-10-29 [1] CRAN (R 4.0.0)                      
#>  png            0.1-7      2013-12-03 [1] CRAN (R 4.0.0)                      
#>  prettyunits    1.1.1      2020-01-24 [1] CRAN (R 4.0.0)                      
#>  processx       3.4.2      2020-02-09 [1] CRAN (R 4.0.0)                      
#>  ps             1.3.3      2020-05-08 [1] CRAN (R 4.0.0)                      
#>  R6             2.4.1      2019-11-12 [1] CRAN (R 4.0.0)                      
#>  raster         3.2-2      2020-05-09 [1] Github (rspatial/raster@9ee5eaa)    
#>  RColorBrewer   1.1-2      2014-12-07 [1] CRAN (R 4.0.0)                      
#>  Rcpp           1.0.4.6    2020-04-09 [1] CRAN (R 4.0.0)                      
#>  remotes        2.1.1      2020-02-15 [1] CRAN (R 4.0.0)                      
#>  rlang          0.4.6.9000 2020-05-07 [1] Github (r-lib/rlang@4bea875)        
#>  rmarkdown      2.1        2020-01-20 [1] CRAN (R 4.0.0)                      
#>  rprojroot      1.3-2      2018-01-03 [1] CRAN (R 4.0.0)                      
#>  scales         1.1.0      2019-11-18 [1] CRAN (R 4.0.0)                      
#>  sessioninfo    1.1.1      2018-11-05 [1] CRAN (R 4.0.0)                      
#>  sf           * 0.9-3      2020-05-07 [1] Github (r-spatial/sf@83180aa)       
#>  sp             1.4-1      2020-02-28 [1] CRAN (R 4.0.0)                      
#>  spData       * 0.3.5      2020-04-06 [1] CRAN (R 4.0.0)                      
#>  spDataLarge    0.4.1      2020-04-28 [1] Github (nowosad/spDataLarge@15b9603)
#>  stars          0.4-2      2020-05-07 [1] Github (r-spatial/stars@c57fd85)    
#>  stringi        1.4.6      2020-02-17 [1] CRAN (R 4.0.0)                      
#>  stringr        1.4.0      2019-02-10 [1] CRAN (R 4.0.0)                      
#>  testthat       2.3.2      2020-03-02 [1] CRAN (R 4.0.0)                      
#>  tmap         * 3.1        2020-05-09 [1] Github (mtennekes/tmap@85aa176)     
#>  tmaptools      3.0-1      2020-05-07 [1] Github (mtennekes/tmaptools@5d326a2)
#>  units          0.6-6      2020-03-16 [1] CRAN (R 4.0.0)                      
#>  usethis        1.6.0      2020-04-09 [1] CRAN (R 4.0.0)                      
#>  viridisLite    0.3.0      2018-02-01 [1] CRAN (R 4.0.0)                      
#>  withr          2.2.0      2020-04-20 [1] CRAN (R 4.0.0)                      
#>  xfun           0.13       2020-04-13 [1] CRAN (R 4.0.0)                      
#>  XML            3.99-0.3   2020-01-20 [1] CRAN (R 4.0.0)                      
#>  xml2           1.3.2      2020-04-23 [1] CRAN (R 4.0.0)                      
#>  yaml           2.2.1      2020-02-01 [1] CRAN (R 4.0.0)                      
#> 
#> [1] /home/jn/R/x86_64-pc-linux-gnu-library/4.0
#> [2] /opt/R/4.0.0/lib64/R/library

Should work now! We may expect more of this kind, Raster objects are full with surprises!

The result, by the way, suggests that a black line around the legend color boxes would make the legend easier to read, now the third class dissolves in the cell it's placed in:
x

Thanks @edzer.
The code also gives me a strange warning, but I am not sure if this is my fault, stars, or tmap issue.

library(tmap)
library(spData)
tm_shape(grain) +
  tm_raster()
#> Warning in rep(attr(shp[[1]], "colors"), length.out = length(lvls)): 'x' is NULL
#> so the result will be NULL
#> Linking to GEOS 3.7.1, GDAL 2.3.2, PROJ 5.2.0

Created on 2020-05-11 by the reprex package (v0.3.0)

That's tmap :-) Will look into it soon.

And yes, a black box around the legend items would be better in this example, but not in general, since black lines will influence the perception of colors, especially with tiny grid cells. Normally, I would advise to draw the legend outside the map with + tm_layout(legend.outsize = TRUE).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mtennekes picture mtennekes  ·  8Comments

Si-Chong picture Si-Chong  ·  4Comments

zjslagle picture zjslagle  ·  4Comments

marionowak picture marionowak  ·  9Comments

Robinlovelace picture Robinlovelace  ·  4Comments