Skip to content

Commit

Permalink
done #811
Browse files Browse the repository at this point in the history
  • Loading branch information
mtennekes committed Feb 7, 2024
1 parent cbdad3f commit 3169b63
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
4 changes: 2 additions & 2 deletions R/tm_layers_raster.R
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ tm_raster = function(col = tm_shape_vars(),
col.legend = do.call("tm_legend", col.legend.args)
}



# needed for color maps without categories (then tm_scale_categorical is used without legend, unless called)
col.legend$called = "col.legend" %in% names(args_called)


tm_element_list(tm_element(
Expand Down
20 changes: 16 additions & 4 deletions R/tmapScaleCategorical.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
tmapScaleCategorical = function(x1, scale, legend, o, aes, layer, layer_args, sortRev, bypass_ord, submit_legend = TRUE) {
cls = if (inherits(scale, "tm_scale_categorical")) c("fact", "unord") else c("fact", "ord")

if (is.factor(x1) && grepl("=<>=", levels(x1)[1], fixed = TRUE)) {
res = strsplit(levels(x1), "=<>=", fixed = TRUE)
levels(x1) = vapply(res, "[", 1, FUN.VALUE = character(1))
ct = vapply(res, "[", 2, FUN.VALUE = character(1))
if (is.factor(x1)) {
defcols_cats = grepl("=<>=", levels(x1)[1], fixed = TRUE)
defcols_nocats = grepl("=><=", levels(x1)[1], fixed = TRUE)

if (defcols_cats || defcols_nocats) {
res = strsplit(levels(x1), {if (defcols_cats) "=<>=" else "=><="}, fixed = TRUE)
levels(x1) = vapply(res, "[", 1, FUN.VALUE = character(1))
ct = vapply(res, "[", 2, FUN.VALUE = character(1))

if (defcols_nocats && !legend$called) {
legend$show = FALSE
}
} else {
ct = NULL
}
} else {
ct = NULL
}


scale = get_scale_defaults(scale, o, aes, layer, cls, ct)

Expand Down
4 changes: 3 additions & 1 deletion R/tmapShape.R
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ tmapShape.SpatRaster = function(shp, is.main, crs, bbox, unit, filter, shp_name,

for (nm in dtcols) {
if (!is.null(ctabs[[nm]])) {
# in case of predefined colors: if categories are associated (cats), use tm_scale_categorical, otherwise tm_

ct = ctabs[[nm]]
lt = cats[[nm]]
if (is.factor(dt[[nm]])) {
Expand All @@ -158,7 +160,7 @@ tmapShape.SpatRaster = function(shp, is.main, crs, bbox, unit, filter, shp_name,
levels(dt[[nm]]) = paste(levels(dt[[nm]]), cls, sep = "=<>=")
} else if ("values" %in% names(ct)) {
cls = rgb(ct$red, ct$green, ct$blue, ct$alpha, maxColorValue = 255)
dt[[nm]] = factor(dt[[nm]], levels = ct$values, labels = paste(ct$values, cls, sep = "=<>="))
dt[[nm]] = factor(dt[[nm]], levels = ct$values, labels = paste(ct$values, cls, sep = "=><="))
}

}
Expand Down
30 changes: 18 additions & 12 deletions sandbox/issues.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ my_map |>
leaflet::addTiles(attributions = "Last update in October 2023")

###########################################################
# was not working when asp = portrait
# scalebar
# https://github.com/r-tmap/tmap/issues/809
# changed width back to number of lines
###########################################################
Expand All @@ -38,13 +38,6 @@ tm_shape(World) +
tm_scalebar(position = tm_pos_out())



###########################################################
###########################################################


# scale bar outside

tm_shape(NLD_prov) +
tm_fill() +
tm_grid() +
Expand All @@ -61,7 +54,7 @@ tm_shape(NLD_prov) +
tm_fill() +
tm_grid(labels.inside.frame = F) +
tm_layout(meta.margins = c(0, 0, 0, .6)) +
tm_scalebar(width = 1,
tm_scalebar(width = 10,
position = tm_pos_out())

tm_shape(NLD_prov) +
Expand Down Expand Up @@ -111,7 +104,10 @@ tm_shape(tanzania) +
tm_polygons(lwd = 2) +
tm_scalebar(c(0, 200, 400), position = c("left", "bottom"))

# 811

###########################################################
# https://github.com/r-tmap/tmap/issues/811
###########################################################

library(terra)

Expand All @@ -132,12 +128,14 @@ tm_shape(r) +
tm_shape(r) +
tm_raster(col.legend = tm_legend_hide())


str(r)
# not working
tm_shape(r) +
tm_rgb()


cat_raster = rast(system.file("raster/nlcd.tif", package = "spDataLarge"))


tm_shape(cat_raster) +
tm_raster(col.scale = tm_scale_categorical(levels.drop = TRUE),
col.legend = tm_legend("Land cover"))
Expand Down Expand Up @@ -169,6 +167,14 @@ plot(lc_cameroon) # raster with color table
tm_shape(lc_cameroon) +
tm_raster()

tm_shape(lc_cameroon) +
tm_raster("land_cover")


tm_shape(lc_cameroon) +
tm_raster("land_cover", col.legend = tm_legend())


tm_shape(lc) +
tm_raster()

Expand Down

0 comments on commit 3169b63

Please sign in to comment.