Skip to content

Commit

Permalink
fixed #819
Browse files Browse the repository at this point in the history
  • Loading branch information
mtennekes committed Jul 21, 2024
1 parent b1b2dc2 commit ca86e1d
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 41 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ export(tm_minimap)
export(tm_mouse_coordinates)
export(tm_mv)
export(tm_mv_dim)
export(tm_mv_shape_vars)
export(tm_options)
export(tm_place_legends_bottom)
export(tm_place_legends_inside)
Expand Down
27 changes: 24 additions & 3 deletions R/step1_helper_facets.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,15 @@ step1_rearrange_facets = function(tmo, o) {
precheck_aes = function(a, layer, shpvars, args) {
within(a, {

if (inherits(value, "tmapDimVars")) {
if (!(value$x %in% smeta$dims)) stop("Unknown dimension in tm_dim_vars", call. = FALSE)
if (inherits(value, "tmapDimVars") || (inherits(value, "tmapMVShpVars") && length(shpvars) == 1L && value$n >= 1)) {
if (inherits(value, "tmapDimVars")) {
if (!(value$x %in% smeta$dims)) stop("Unknown dimension in tm_dim_vars", call. = FALSE)
} else {
value = list(x = smeta$dims[1], values = {
if (is.na(value$n)) smeta$dims_vals[[1]] else smeta$dims_vals[[1]][1L:value$n]
})
}

split_stars_dim = value$x
if (!all(value$values %in% smeta$dims_vals[[split_stars_dim]])) stop("Unknown values in tm_dim_vars", call. = FALSE)

Expand All @@ -110,7 +117,21 @@ 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")) {
value = as.list(shpvars)
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)
}
value = as.list(shpvars[1L:value$n])
}
} else if (inherits(value, "tmapMVShpVars")) {
if (is.na(value$n)) {
value = list(shpvars)
} else {
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_orig = value
#value = lapply(value_orig, make.names)
Expand Down
15 changes: 0 additions & 15 deletions R/step2_helper_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -159,21 +159,6 @@ getdts = function(aes, unm, p, q, o, dt, shpvars, layer, mfun, args, plot.order)
aes$data_vars = FALSE
}

#
# List of 10
# $ scale :List of 1
# ..- attr(*, "class")= chr [1:3] "tm_scale_auto" "tm_scale" "list"
# $ legend :List of 65
# ..- attr(*, "class")= chr [1:2] "tm_legend_standard_portrait" "list"
# $ chart :List of 36
# ..- attr(*, "class")= chr [1:4] "tm_chart_none" "tm_chart" "tm_component" "list"
# $ o :List of 343
# $ aes : chr "fill"
# $ layer : chr "polygons"
# $ layer_args : list()
# $ sortRev : logi NA
# $ bypass_ord : logi FALSE
# $ submit_legend: logi TRUE
if (!aes$data_vars && !aes$geo_vars) {
#cat("step2_grp_lyr_aes_const", unm," \n")
# constant values (take first value (of possible tm_mv per facet)
Expand Down
6 changes: 3 additions & 3 deletions R/tm_layers_rgb.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ opt_tm_rgb = function(interpolate = FALSE) {
#' @param col,col.scale,col.legend,col.chart,col.free Visual variable that determines
#' the col color. `col` is a multivariate variable, with 3 (`tm_rgb`) or 4 (`tm_rgba`) numeric data variables. These can be specified via [tm_mv()] or [tm_mv_dim()]
#' @param options options passed on to the corresponding `opt_<layer_function>` function

#' @example ./examples/tm_rgb.R
#' @export
tm_rgb = function(col = tm_mv(1:3),
tm_rgb = function(col = tm_mv_shape_vars(3),
col.scale = tm_scale_rgb(),
col.legend = tm_legend(),
col.chart = tm_chart_none(),
Expand All @@ -26,7 +26,7 @@ tm_rgb = function(col = tm_mv(1:3),

#' @rdname tm_rgb
#' @export
tm_rgba = function(col = tm_mv(1:4),
tm_rgba = function(col = tm_mv_shape_vars(4),
col.scale = tm_scale_rgba(),
col.legend = tm_legend(),
col.chart = tm_chart_none(),
Expand Down
13 changes: 11 additions & 2 deletions R/tm_scale_.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +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
#' @export
tm_shape_vars = function() {
structure(list(), class = c("tm_shape_vars", "list"))
tm_shape_vars = function(n = NA) {
structure(list(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"))
}

#' Scales: automatic scale
Expand Down
3 changes: 2 additions & 1 deletion R/tmapScale_.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +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(), class = "tmapShpVars"))
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, "tmapDimVars")) return(x)

cls = if (inherits(x, "AsIs")) "tmapAsIs" else if (inherits(x, "tmapUsrCls")) "tmapUsrCls" else "tmapVars"
Expand Down
28 changes: 14 additions & 14 deletions examples/tm_rgb.R
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
require(stars)
file = system.file("tif/L7_ETMs.tif", package = "stars")

L7 = stars::read_stars(file)

# not working yet #819
tm_shape(L7) +
tm_rgb()

\dontrun{
# the previous example was a shortcut of this call
tm_shape(L7) +
tm_rgb(col = tm_mv_dim("band", 1:3))

tm_shape(L7) +
tm_rgba(col = tm_mv_dim("band", 1:4))

library(stars)
# alternative format: using a stars dimension instead of attributes
L7_alt = split(L7, "band")

# not working yet #819
tm_shape(L7_alt) +
tm_rgb()

tm_shape(L7_alt) +
tm_rgb(col = tm_mv("X1", "X2", "X3"))

tm_shape(L7_alt) +
tm_rgb(col = tm_mv(1:3))

L7_terra = terra::rast(file)

tm_shape(L7_terra) +
tm_rgb(col = do.call(tm_mv, as.list(names(L7_terra)[1:3])))
if (requireNamespace("terra")) {
L7_terra = terra::rast(file)

tm_shape(L7_terra) +
tm_rgb()

tm_shape(L7_terra) +
tm_rgb(tm_mv(names(L7_terra)[1:3]))
}
}
37 changes: 35 additions & 2 deletions man/tm_rgb.Rd

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

8 changes: 7 additions & 1 deletion 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 ca86e1d

Please sign in to comment.