From 379d738bbe978b1891654f54c4659dff49d7a744 Mon Sep 17 00:00:00 2001 From: rhijmans Date: Thu, 9 Jan 2025 09:55:15 -0800 Subject: [PATCH] comb --- NAMESPACE | 5 +---- R/levels.R | 22 ++++++++++++++++++++++ R/panel.R | 17 +---------------- R/plot_vector.R | 2 +- man/factors.Rd | 5 +++++ 5 files changed, 30 insertions(+), 21 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index fc406631c..92b5882bf 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -21,9 +21,6 @@ S3method(str, SpatRaster) S3method(str, SpatExtent) S3method(str, SpatGraticule) -export(add_box, add_legend, add_grid, add_mtext, focalMat, extractAlong, gdal, getGDALconfig, graticule, halo, libVersion, setGDALconfig, map.pal, map_extent, north, sbar, terraOptions, tmpFiles, makeVRT, mem_info, free_RAM, same.crs, shade, gdalCache, fileBlocksize, vector_layers, vrt_tiles, names, round) - - - +export(add_box, add_legend, add_grid, add_mtext, combineLevels, focalMat, extractAlong, gdal, getGDALconfig, graticule, halo, libVersion, setGDALconfig, map.pal, map_extent, north, sbar, terraOptions, tmpFiles, makeVRT, mem_info, free_RAM, same.crs, shade, gdalCache, fileBlocksize, vector_layers, vrt_tiles, names, round) diff --git a/R/levels.R b/R/levels.R index cc3204c88..a0e8756b0 100644 --- a/R/levels.R +++ b/R/levels.R @@ -72,6 +72,28 @@ setMethod("levels<-", signature(x="SpatRaster"), ) +combineLevels <- function(x) { + lv <- levels(x) + lv <- lv[sapply(lv, is.data.frame)] + un <- unique(sapply(lv, names)) + if (length(un) > 2) { + lv <- lapply(lv, function(i) { colnames(i) <- colnames(lv[[1]]); i}) + } + lv <- try(do.call(rbind, lv), silent=TRUE) + if (inherits(lv, "try-error")) { # should not happen anymore + error("panel", "cannot use combine categories") + } + lv <- unique(lv) + if (length(unique(lv[,1])) < nrow(lv)) { + error("panel", "cannot combine conflicting categories") + } + lv <- lv[order(lv[,1]), ] + nms <- names(x) + x <- categories(x, 0, lv) + names(x) <- nms + x +} + setMethod ("set.cats" , "SpatRaster", function(x, layer=1, value, active=1) { diff --git a/R/panel.R b/R/panel.R index 1d1d40c73..1dc225efb 100644 --- a/R/panel.R +++ b/R/panel.R @@ -16,22 +16,7 @@ setMethod("panel", signature(x="SpatRaster"), # } categorical <- FALSE if (is.null(type) && any(is.factor(x))) { - lv <- levels(x) - lv <- lv[sapply(lv, is.data.frame)] - un <- unique(sapply(lv, names)) - if (length(un) > 2) { - lv <- lapply(lv, function(i) { colnames(i) <- colnames(lv[[1]]); i}) - } - lv <- try(do.call(rbind, lv), silent=TRUE) - if (inherits(lv, "try-error")) { # should not happen anymore - error("panel", "cannot use combine categories") - } - lv <- unique(lv) - if (length(unique(lv[,1])) < nrow(lv)) { - error("panel", "cannot use rasters with conflicting categories") - } - lv <- lv[order(lv[,1]), ] - x <- categories(x, 0, lv) + x <- combineLevels(x) categorical <- TRUE } diff --git a/R/plot_vector.R b/R/plot_vector.R index b8571ae3c..f068a65f0 100644 --- a/R/plot_vector.R +++ b/R/plot_vector.R @@ -483,7 +483,7 @@ setMethod("dots", signature(x="SpatVector"), stopifnot(length(ylim) == 2) out$lim[3:4] <- sort(ylim) } - } else if (!add) { + } else if ((!add) && is.null(xpd)) { xpd <- TRUE } out$ngeom <- nrow(x) diff --git a/man/factors.Rd b/man/factors.Rd index a31a36df4..8a0851db7 100644 --- a/man/factors.Rd +++ b/man/factors.Rd @@ -14,6 +14,7 @@ \alias{categories,SpatRaster-method} \alias{addCats} \alias{addCats,SpatRaster-method} +\alias{shareLevels} \title{Categorical rasters} @@ -33,6 +34,8 @@ To set categories for the first layer of a SpatRaster, you can provide \code{lev \code{droplevels} removes categories that are not used (declared but not present as values in the raster) if \code{levels=NULL}. \code{addCats} adds additional categories to a layer that already is categorical. It adds new variables, not new levels of an existing categorical variable. + +\code{combineLevels} combines the levels of all layers of \code{x} and sets them to all layers. That fails if there are labeling conflicts between layers } \usage{ @@ -47,6 +50,8 @@ To set categories for the first layer of a SpatRaster, you can provide \code{lev \S4method{droplevels}{SpatRaster}(x, level=NULL, layer=1) \S4method{addCats}{SpatRaster}(x, value, merge=FALSE, layer=1) + +combineLevels(x) }