Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mtennekes committed Jul 21, 2024
1 parent ca86e1d commit 4fc4632
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 24 deletions.
24 changes: 14 additions & 10 deletions R/step1_helper_facets.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions R/tm_scale_.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions R/tmapScale_.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
13 changes: 12 additions & 1 deletion examples/tm_rgb.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))

}
}
13 changes: 12 additions & 1 deletion man/tm_rgb.Rd

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

8 changes: 5 additions & 3 deletions man/tm_shape_vars.Rd

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

0 comments on commit 4fc4632

Please sign in to comment.