Skip to content

Commit

Permalink
fixed terra level issue (triggered by #822
Browse files Browse the repository at this point in the history
  • Loading branch information
mtennekes committed Feb 9, 2024
1 parent d528d77 commit 1dc0fe0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
4 changes: 2 additions & 2 deletions R/tmapShape.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions man/tm_text.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 36 additions & 2 deletions sandbox/issues.R
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ unique(landr)

tm_shape(lc) + tm_raster()

landr = rast(land)


class(landr)
str(landr)
Expand Down Expand Up @@ -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)+
Expand All @@ -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)+
Expand All @@ -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)

0 comments on commit 1dc0fe0

Please sign in to comment.