Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mtennekes committed Dec 10, 2024
1 parent 625beea commit 848f168
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions R/tmapScale_.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ tmapUsrCls = function(x) {
format_aes_results = function(values, ord = NULL, legend, chart) {
legnr = vector(mode = "integer", length = length(values))
legnr[1] = legend_save(legend)

crtnr = vector(mode = "integer", length = length(values))
crtnr[1] = chart_save(chart)

if (is.null(ord)) {
list(values = values,
legnr = legnr,
crtnr = crtnr)
crtnr = crtnr)
} else {
list(values = values,
ord = ord,
Expand Down Expand Up @@ -110,17 +110,17 @@ data_class = function(x, check_for_color_class = FALSE) {
if (check_for_color_class) {
w = which(!is.na(x))
if (length(w) && all(valid_colors(head(x[w], 100)))) {
c("asis", "color")
c("asis", "color")
} else {
subclass = if (is.ordered(x)) "ord" else "unord"
c("fact", subclass)
c("fact", subclass)
}
} else {
subclass = if (is.ordered(x)) "ord" else "unord"
c("fact", subclass)
}
}

attr(cls, "units") = if (inherits(x, "units")) {
paste0(" [", units(x), "]")
} else ""
Expand All @@ -133,6 +133,7 @@ data_class = function(x, check_for_color_class = FALSE) {
#' @keywords internal
#' @rdname tmap_internal
tmapScale = function(aes, value, scale, legend, chart, free) {
if (is.null(legend)) legend = tm_legend_hide()
structure(list(aes = aes, value = tmapVV(value), scale = scale, legend = legend, chart = chart, free = free), class = c("tmapScale", "list"))
}

Expand All @@ -145,31 +146,31 @@ tmapScaleAuto = function(x1, scale, legend, chart, o, aes, layer, layer_args, so
if (length(args)) {
names(args) = paste0("x", 2L:(length(args)+1L))
}

cls = data_class(x1, check_for_color_class = aes %in% c("col", "fill"))

#if (cls[1] == "na")
sc_opt = getAesOption("scales.var", o, aes, layer, cls = cls)
#if (aes == "fill") browser()
#if (aes == "fill") browser()
if (k == 2) {
sc = "bivariate"
} else if (k > 2) {
stop("No default scale for multivariate variables", call. = FALSE)
#sc = "composition"
} else if (cls[1] == "asis") {
sc = "asis"
sc = "asis"
} else if (attr(cls, "unique") && !(sc_opt == "asis")) {
if ("num" %in% cls) {
sc = "ordinal"
message("The visual variable \"", aes, "\" of the layer \"", layer, "\" contains a unique value. Therefore a discrete scale is applied (tm_scale_discrete).")
message("The visual variable \"", aes, "\" of the layer \"", layer, "\" contains a unique value. Therefore a discrete scale is applied (tm_scale_discrete).")
} else {
sc = "categorical"
sc = "categorical"
message("The visual variable \"", aes, "\" of the layer \"", layer, "\" contains a unique value. Therefore a categorical scale is applied (tm_scale_categorical).")
}
} else {

sc_pref = scale$fun_pref

if (!is.null(sc_pref)) {
if (sc_pref %in% c("categorical", "continuous", "continuous_log", "rank")) {
sc = sc_pref
Expand All @@ -180,23 +181,23 @@ tmapScaleAuto = function(x1, scale, legend, chart, o, aes, layer, layer_args, so
sc = sc_opt
}
}


tm_scalefun = paste0("tm_scale_", sc)

scale = scale[names(scale) %in% names(formals(tm_scalefun))]

scale_new = do.call(tm_scalefun, args = scale)

FUN = scale_new$FUN
scale_new$FUN = NULL

if (sc == "bivariate") {
do.call(FUN, list(x1 = x1, x2 = args[[1]], scale = scale_new, legend = legend, chart = chart, o = o, aes = aes, layer = layer, layer_args = layer_args, sortRev = sortRev, bypass_ord = bypass_ord, submit_legend = submit_legend))
} else if (sc == "multi_continuous") {
do.call(FUN, c(list(x1 = x1), args, list(scale = scale_new, legend = legend, chart = chart, o = o, aes = aes, layer = layer, layer_args = layer_args, sortRev = sortRev, bypass_ord = bypass_ord, submit_legend = submit_legend)))
} else {
do.call(FUN, list(x1 = x1, scale = scale_new, legend = legend, chart = chart, o = o, aes = aes, layer = layer, layer_args = layer_args, sortRev, bypass_ord, submit_legend))
}

}

0 comments on commit 848f168

Please sign in to comment.