diff --git a/R/step1_helper_facets.R b/R/step1_helper_facets.R index 5121b93e..41c13235 100644 --- a/R/step1_helper_facets.R +++ b/R/step1_helper_facets.R @@ -83,7 +83,7 @@ step1_rearrange_facets = function(tmo, o) { precheck_aes = function(a, layer, shpvars, args) { within(a, { - if (inherits(value, "tmapDimVars") || (inherits(value, "tmapMVShpVars") && length(shpvars) == 1L && value$n >= 1)) { + if (inherits(value, "tmapDimVars") || (inherits(value, "tmapMVShpVars") && length(shpvars) == 1L)) { if (inherits(value, "tmapDimVars")) { if (!(value$x %in% smeta$dims)) stop("Unknown dimension in tm_dim_vars", call. = FALSE) } else { @@ -117,20 +117,24 @@ step1_rearrange_facets = function(tmo, o) { if (!is.list(value_orig)) value = list(value_orig) names(value) = sapply(value, "[", 1) } else if (inherits(value, "tmapShpVars")) { - if (is.na(value$n)) { - value = as.list(shpvars) - } else { - if (length(shpvars) < value$n) { - stop("tm_shape_vars defined for n = ", value$n, " while there are only ", length(shpvars), " variables", call. = FALSE) - } + if (!is.na(value$ids[1])) { + if (!all(value$ids %in% 1L:length(shpvars))) stop("tm_shape_vars defined for ids = ", paste(value$ids, collapse = ", "), " while there are only ", length(shpvars), " variables", call. = FALSE) + value = as.list(shpvars[value$ids]) + } else if (!is.na(value$n)) { + if (length(shpvars) < value$n) stop("tm_shape_vars defined for n = ", value$n, " while there are only ", length(shpvars), " variables", call. = FALSE) value = as.list(shpvars[1L:value$n]) + } else { + value = as.list(shpvars) } } else if (inherits(value, "tmapMVShpVars")) { - if (is.na(value$n)) { - value = list(shpvars) - } else { + if (!is.na(value$ids[1])) { + if (!all(value$ids %in% 1L:length(shpvars))) stop("tm_shape_vars defined for ids = ", paste(value$ids, collapse = ", "), " while there are only ", length(shpvars), " variables", call. = FALSE) + value = list(shpvars[value$ids]) + } else if (!is.na(value$n)) { if (length(shpvars) < value$n) stop("tm_shape_vars specified with n = ", value$n, " but there are only ", length(shpvars), " variables available", call. = FALSE) value = list(shpvars[1L:value$n]) + } else { + value = list(shpvars) } } else { value_orig = value diff --git a/R/tm_scale_.R b/R/tm_scale_.R index 2f8d5b72..41a8356d 100644 --- a/R/tm_scale_.R +++ b/R/tm_scale_.R @@ -11,18 +11,18 @@ tm_const = function() { #' #' tmap function to specify all variables in the shape object #' -#' @param n the first `n` shape variables are used -#' @rdname tm_shape_vars +#' @param n if specified, the first `n` shape variables are used +#' @param id index numbers of the used shape variables #' @export -tm_shape_vars = function(n = NA) { - structure(list(n = n), class = c("tm_shape_vars", "list")) +tm_shape_vars = function(ids = NA, n = NA) { + structure(list(ids = ids, n = n), class = c("tm_shape_vars", "list")) } + #' @rdname tm_shape_vars -#' @name tm_mv_shape_vars #' @export -tm_mv_shape_vars = function(n = NA) { - structure(list(n = n), class = c("tm_mv_shape_vars", "list")) +tm_mv_shape_vars = function(ids = NA, n = NA) { + structure(list(ids = ids, n = n), class = c("tm_mv_shape_vars", "list")) } #' Scales: automatic scale diff --git a/R/tmapScale_.R b/R/tmapScale_.R index b682e742..6cb17d97 100644 --- a/R/tmapScale_.R +++ b/R/tmapScale_.R @@ -21,8 +21,8 @@ tm_mv_dim = function(x, values) { tmapVars = function(x) { if (inherits(x, "tmapOption")) return(x) - if (inherits(x, "tm_shape_vars")) return(structure(list(n = x$n), class = "tmapShpVars")) - if (inherits(x, "tm_mv_shape_vars")) return(structure(list(n = x$n), class = "tmapMVShpVars")) + if (inherits(x, "tm_shape_vars")) return(structure(list(ids = x$ids, n = x$n), class = "tmapShpVars")) + if (inherits(x, "tm_mv_shape_vars")) return(structure(list(ids = x$ids, n = x$n), class = "tmapMVShpVars")) if (inherits(x, "tmapDimVars")) return(x) cls = if (inherits(x, "AsIs")) "tmapAsIs" else if (inherits(x, "tmapUsrCls")) "tmapUsrCls" else "tmapVars" diff --git a/examples/tm_rgb.R b/examples/tm_rgb.R index 7a380d72..0919a7b5 100644 --- a/examples/tm_rgb.R +++ b/examples/tm_rgb.R @@ -16,16 +16,27 @@ L7_alt = split(L7, "band") tm_shape(L7_alt) + tm_rgb() +# with attribute names tm_shape(L7_alt) + tm_rgb(col = tm_mv("X1", "X2", "X3")) +# with attribute indices +tm_shape(L7_alt) + + tm_rgb(col = tm_mv_shape_vars(1:3)) + if (requireNamespace("terra")) { L7_terra = terra::rast(file) tm_shape(L7_terra) + tm_rgb() - + + # with layer names tm_shape(L7_terra) + tm_rgb(tm_mv(names(L7_terra)[1:3])) + + # with layer indices + tm_shape(L7_alt) + + tm_rgb(col = tm_mv_shape_vars(1:3)) + } } diff --git a/man/tm_rgb.Rd b/man/tm_rgb.Rd index 1f7aaeb6..f317d239 100644 --- a/man/tm_rgb.Rd +++ b/man/tm_rgb.Rd @@ -56,17 +56,28 @@ L7_alt = split(L7, "band") tm_shape(L7_alt) + tm_rgb() +# with attribute names tm_shape(L7_alt) + tm_rgb(col = tm_mv("X1", "X2", "X3")) +# with attribute indices +tm_shape(L7_alt) + + tm_rgb(col = tm_mv_shape_vars(1:3)) + if (requireNamespace("terra")) { L7_terra = terra::rast(file) tm_shape(L7_terra) + tm_rgb() - + + # with layer names tm_shape(L7_terra) + tm_rgb(tm_mv(names(L7_terra)[1:3])) + + # with layer indices + tm_shape(L7_alt) + + tm_rgb(col = tm_mv_shape_vars(1:3)) + } } } diff --git a/man/tm_shape_vars.Rd b/man/tm_shape_vars.Rd index c0d08f8a..cb9bce73 100644 --- a/man/tm_shape_vars.Rd +++ b/man/tm_shape_vars.Rd @@ -5,12 +5,14 @@ \alias{tm_mv_shape_vars} \title{tmap function to specify all variables in the shape object} \usage{ -tm_shape_vars(n = NA) +tm_shape_vars(ids = NA, n = NA) -tm_mv_shape_vars(n = NA) +tm_mv_shape_vars(ids = NA, n = NA) } \arguments{ -\item{n}{the first \code{n} shape variables are used} +\item{n}{if specified, the first \code{n} shape variables are used} + +\item{id}{index numbers of the used shape variables} } \description{ tmap function to specify all variables in the shape object