r-tmap / tmap

R package for thematic maps
https://r-tmap.github.io/tmap
GNU General Public License v3.0
855 stars 119 forks source link

tm_mv not available? #922

Closed r-poloni closed 2 weeks ago

r-poloni commented 2 weeks ago

Hi, I have a script from last year, that simply draws maps based on a raster layer and points. Last year I struggled a bit to write it with tmap4, but I did this because it should have been a better option, since tmap3 will be discontinued soon. Since I changed my computer, I now re-installed all the packages and the script does not work any more.

The point dataframe is a simple R DF like this

      LAT LONG
[1,] 22.68 54.40
[2,] 20.04 53.84
[3,] 22.78 56.90
[4,] 17.37 54.65
[5,] 21.11 55.26
[6,] 17.57 56.94
[7,] 21.17 55.68
[8,] 17.72 54.04
#shapefile for borders
base <- read_sf("base.shp")
class(base)
#[1] "sf"         "tbl_df"     "tbl"        "data.frame"
#sets a bounding box for the map
box_base <- st_bbox(c(xmin = 51.10, xmax = 59.90, ymax = 16.14, ymin = 26.9), crs = st_crs(4326))
#points
pnts_sf <- st_as_sf(df, coords = c("LAT", "LONG"), crs = 4326)

#reads stamen raster
base_stamen.terra <- terra::rast("base_stamen_b.tif")
class(base)
#[1] "SpatRaster"
#attr(,"package")
#[1] "terra"

# OR FOR A REPRODUCIBLE EXAMPLE 
library(spDataLarge)
base_stamen = read_stars(system.file("raster/landsat.tif", package = "spDataLarge")) 

#map
tmap_mode("plot")
tm_shape(base_stamen.terra) +
    tm_rgb(tm_mv(c("band1", "band2", "band3"))) +
    tm_shape(base)+
    tm_borders()+
    tm_shape(pnts_sf, bbox=box_base)+
    tm_dots(size=0.2)+
    tm_compass(type = "arrow", position = c("right", "top"), size = 2) +
    tm_scale_bar(breaks = c(0, 100, 200), text.size = 0.5)

This produces the following error:

Error in tm_mv(c("band1", "band2", "band3")) : 
  could not find function "tm_mv"

Do you have an idea why the function tm_mv is not recognised?

olivroy commented 2 weeks ago

Hi @r-poloni , you can try tm_vars()! This is indeed a change that happened during tmap v4 development! Thanks for testing tmap v4 and let us know if something else is not working as expected!

tm_vars(c("band1", "band2", "band3"), multivariate = TRUE)

Let me know if it works as expected. See https://github.com/r-tmap/tmap/issues/819 for details.

I edited your comment slightly to make sure code was displayed. You need to use 3 back ticks ```

r-poloni commented 2 weeks ago

Hi! Sorry, I read the issue and also the news about version 4, but when I saw that they adviced the use of tm_mv I didn't read further. My bad! Thank you, the tm_vars() works perfectly indeed!