From 1dc0fe0b3cf6d0658275880887597961e49b341d Mon Sep 17 00:00:00 2001 From: mtennekes Date: Fri, 9 Feb 2024 11:50:03 +0100 Subject: [PATCH] fixed terra level issue (triggered by #822 --- R/tmapShape.R | 4 ++-- man/tm_text.Rd | 4 ++++ sandbox/issues.R | 38 ++++++++++++++++++++++++++++++++++++-- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/R/tmapShape.R b/R/tmapShape.R index e054ca84..433437de 100644 --- a/R/tmapShape.R +++ b/R/tmapShape.R @@ -98,7 +98,7 @@ tmapShape.SpatRaster = function(shp, is.main, crs, bbox, unit, filter, shp_name, ctabs = terra::coltab(shp) - cats = terra::cats(shp) + cats = terra::levels(shp) dt = data.table::setDT(terra::as.data.frame(shp, na.rm=FALSE)) @@ -148,7 +148,7 @@ tmapShape.SpatRaster = function(shp, is.main, crs, bbox, unit, filter, shp_name, if (is.factor(dt[[nm]])) { #levels(dt[[nm]]) - ids = match(lt$value[match(levels(dt[[nm]]), lt$levels)], ct$value) + ids = match(lt[match(levels(dt[[nm]]), lt[,2]), 1], ct$value) cti = ct[ids,] cls = rgb(cti$red, cti$green, cti$blue, cti$alpha, maxColorValue = 255) diff --git a/man/tm_text.Rd b/man/tm_text.Rd index 89506a4a..184c4dd2 100644 --- a/man/tm_text.Rd +++ b/man/tm_text.Rd @@ -121,6 +121,10 @@ own legend. For facet wraps and stacks (\code{\link[=tm_facets_wrap]{tm_facets_w there is only one facet dimension, so the \code{.free} argument requires only one logical value. } \examples{ +# Need to be updated. The following is just to illustrate bgcol(_alpha) +tm_shape(World) + tm_polygons() + + tm_text("name", bgcol = "economy", bgcol_alpha = .5, shadow = TRUE) + tm_shape(World) + tm_text(text = "name", size = .4, diff --git a/sandbox/issues.R b/sandbox/issues.R index 7c8c1e30..a8313c39 100644 --- a/sandbox/issues.R +++ b/sandbox/issues.R @@ -440,7 +440,7 @@ unique(landr) tm_shape(lc) + tm_raster() -landr = rast(land) + class(landr) str(landr) @@ -560,6 +560,14 @@ library(spData) packageVersion("tmap") #> [1] '3.99.9000' +tm_shape(nz)+ + tm_fill(fill = "Island", + fill.scale = tm_scale_categorical(values=c("red", "#FFEBBE")), + fill.legend = tm_legend(position = tm_pos_out("right", "top")))+ + tm_title("a)")+ + tm_layout(legend.position = tm_pos_out("left", "top"), + title.position = tm_pos_out("center", "top")) + # Left/Top and Center/Top overlap # Legend/Title are inside rather than outside even though tm_pos_out is used tm_shape(nz)+ @@ -570,7 +578,7 @@ tm_shape(nz)+ tm_shape(nz)+ tm_fill(fill = "Island", fill.scale = tm_scale_categorical(values=c("red", "#FFEBBE")), - fill.legend = tm_legend(position = tm_pos_out("left", "top")))+ + fill.legend = tm_legend(position = tm_pos_out("left", "center")))+ tm_title("a)", position = tm_pos_out("center", "top")) tm_shape(nz)+ @@ -587,3 +595,29 @@ tm_shape(World) + tm_shape(World) + tm_polygons("HPI", fill.legend = tm_legend(position = tm_pos_out("left", "top"))) + + +### 822 + +library(motif) +landcover = rast(system.file("raster/landcover2015.tif", package = "motif")) + +cats(landcover) +terra::coltab(landcover) + +paleta = c("darkgreen", "orange", "yellow", "magenta", "red", "grey", "blue", "#0096A0", "#00CF75") +tm_shape(landcover) + + tm_raster(style = "cat", + palette = paleta) + +tm_shape(landcover) + + tm_raster("category") + +set.seed(0) +r <- rast(nrows=10, ncols=10) +values(r) <- sample(3, ncell(r), replace=TRUE) +is.factor(r) + +cls <- data.frame(id=1:3, cover=c("forest", "water", "urban")) +levels(r) <- cls +is.factor(r)