Skip to content

Commit

Permalink
tm_text, tm_polygons, tm_symbols backw comp., symbol scaling working
Browse files Browse the repository at this point in the history
  • Loading branch information
mtennekes committed Apr 24, 2024
1 parent 944fa94 commit 7a5cab5
Show file tree
Hide file tree
Showing 16 changed files with 419 additions and 216 deletions.
53 changes: 0 additions & 53 deletions R/misc_v3_comp.R

This file was deleted.

4 changes: 3 additions & 1 deletion R/step1_rearrange.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ step1_rearrange = function(tmel) {
if (any(is_opt)) for (id in which(is_opt)) {
o2 = oth[[id]]

if ("v3" %in% o2$calls) message("v3 code detected: as of tmap v4, the legend should be specified for each visual variable\n(e.g. with `fill.legend = tm_legend()` inside tm_polygons to control the legend of fill for polygons.", call. = FALSE)
cls = class(o2)[1]
if (substr(cls, nchar(cls)-2,nchar(cls)) == "_v3") cls = substr(cls, 1, nchar(cls) - 3)
if ("v3_tm_legend" %in% o2$calls) v3_tm_legend_general(fun = cls)

# special case: position, in case c("left", "top") is used
pids = grep(".position", names(o2), fixed = TRUE)
Expand Down
18 changes: 17 additions & 1 deletion R/step4_helper_legends.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,21 @@ step4_plot_collect_legends = function(tmx) {

legs2 = lapply(legs, function(legs_aes) {
legs_aes$vneutral = unlist(lapply(legs_aes$legend, function(l) l$vneutral), use.names = FALSE, recursive = FALSE)
if ("icon_scale" %in% names(legs_aes$legend[[1]])) {
legs_aes$icon_scale = legs_aes$legend[[1]]$icon_scale
} else {
legs_aes$icon_scale = 1
}
legs_aes
})

# find shared legends

icon_scales = vapply(legs2, function(l) {
as.numeric(l$icon_scale)
}, FUN.VALUE = numeric(1))
icon_scale = if (any(icon_scales != 1)) icon_scales[which(icon_scales != 1)[1]] else 1

clones = vapply(legs2, function(l) {
a = l$legend[[1]]$aes
if (!is.null(a)) a else ""
Expand Down Expand Up @@ -120,9 +130,10 @@ step4_plot_collect_legends = function(tmx) {

}


leleg = legs_aes$legend[[k]]



# get gp values from shared legends (slave)
if ("clones" %in% names(leleg)) {
clist = leleg$clones
Expand All @@ -132,6 +143,11 @@ step4_plot_collect_legends = function(tmx) {
}
}

# in case the size legend consists of symbols shapes that represented icons
if (icon_scale > 1 && legnm == "size") {
gp$size = gp$size * icon_scale
}

leleg$gp = gp
leleg$vneutral = NULL
leleg$vvalues = NULL
Expand Down
11 changes: 11 additions & 0 deletions R/tm_add_legend.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ tm_add_legend = function(...,
#args = lapply(as.list(match.call()[-1]), eval, envir = parent.frame())
if (missing(labels)) stop("tm_add_legend: labels required", call. = FALSE)
args = c(as.list(environment()), list(...))
if (type %in% c("fill", "symbol", "line")) {
v3_add_legend(type, names(args))
if ("col" %in% names(args) && !c("fill" %in% names(args))) {
args$fill = args$col
args$col = NULL
}
if ("border.col" %in% names(args)) {
args$col = args$border.col
args$border.col = NULL
}
}
tm_element_list(do.call(tm_element, c(args, list(subclass = c("tm_add_legend", "tm_component")))))
}

Expand Down
2 changes: 1 addition & 1 deletion R/tm_element.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ tm_element_list_sel = function(tml, subclass) {
if (inherits(e2, "tm_legend")) {
names(e2) = paste0("legend.", names(e2))

e2 = tm_element_list(do.call(tm_element, c(e2, list(calls = "v3", subclass = "tm_options"))))
e2 = tm_element_list(do.call(tm_element, c(e2, list(calls = "v3_tm_legend", subclass = c("tm_legend_v3", "tm_options")))))
}
structure(c(e1, e2), class = "tmap")
}
Expand Down
59 changes: 34 additions & 25 deletions R/tm_layers_polygons.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ tm_polygons = function(fill = tm_const(),
args_called = as.list(match.call()[-1]) #lapply(as.list(match.call()[-1]), eval, envir = parent.frame())

if (any(v3_only("tm_polygons") %in% names(args))) {

layer_fun = paste0("tm", {if ("called_from" %in% names(args)) {
args$called_from
} else {
"polygons"
}})

v3_start_message()
if (!("style" %in% names(args))) {
if (!"breaks" %in% names(args)) {
Expand All @@ -139,29 +146,27 @@ tm_polygons = function(fill = tm_const(),
style = args$style
}

imp = function(name, value) {
if (name %in% names(args)) args[[name]] else value
}

fill.scale.args = list(n = imp("n", 5),

fill.scale.args = list(n = v3_impute(args, "n", 5),
style = style,
style.args = imp("style.args", list()),
breaks = imp("breaks", NULL),
interval.closure = imp("interval.closure", "left"),
drop.levels = imp("drop.levels", FALSE),
midpoint = imp("midpoint", NULL),
as.count = imp("as.count", NA),
values = imp("palette", NA),
values.repeat = !imp("stretch.palette", TRUE),
values.range = imp("contrast", NA),
style.args = v3_impute(args, "style.args", list()),
breaks = v3_impute(args, "breaks", NULL),
interval.closure = v3_impute(args, "interval.closure", "left"),
drop.levels = v3_impute(args, "drop.levels", FALSE),
midpoint = v3_impute(args, "midpoint", NULL),
as.count = v3_impute(args, "as.count", NA),
values = v3_impute(args, "palette", NA, "values"),
values.repeat = !v3_impute(args, "stretch.palette", TRUE, "values.repeat"),
values.range = v3_impute(args, "contrast", NA, "values.range"),
values.scale = 1,
value.na = imp("colorNA", NA),
value.null = imp("colorNULL", NA),
value.na = v3_impute(args, "colorNA", NA, "value.na"),
value.null = v3_impute(args, "colorNULL", NA, "value.null"),
value.neutral = NA,
labels = imp("labels", NULL),
label.na = imp("textNA", NA),
labels = v3_impute(args, "labels", NULL),
label.na = v3_impute(args, "textNA", "Missing", "label.na"),
label.null = NA,
label.format = imp("legend.format", list()))
label.format = v3_impute(args, "legend.format", list(), "label.format"))

fill.scale.args$fun_pref = if (style == "cat") {
"categorical"
} else if (style %in% c("fixed", "sd", "equal", "pretty", "quantile", "kmeans", "hclust", "bclust", "fisher", "jenks", "dpih", "headtails", "log10_pretty")) {
Expand All @@ -176,6 +181,8 @@ tm_polygons = function(fill = tm_const(),
stop("unknown style")
}

if ("style" %in% names(args)) v3_tm_scale_instead_of_style(style, scale_fun = fill.scale.args$fun_pref, vv = "fill", layer_fun = layer_fun, arg_list = v3_list_get())

fill.scale = do.call("tm_scale", args = fill.scale.args)

if ("convert2density" %in% names(args) && args$convert2density) {
Expand All @@ -197,17 +204,19 @@ tm_polygons = function(fill = tm_const(),
fill_alpha = args$alpha
}

fill.legend.args = alist(title = imp("title", NA),
show = imp("legend.show", NULL),
na.show = imp("na.show", NA),
format = imp("legend.format", list()),
orientation = ifelse(imp("legend.is.portrait", TRUE), "portrait", "landscape"),
reverse = imp("legend.reverse", FALSE))
fill.legend.args = list(title = v3_impute(args, "title", NA),
show = v3_impute(args, "legend.show", NULL),
na.show = v3_impute(args, "na.show", NA),
format = v3_impute(args, "legend.format", list()),
orientation = ifelse(v3_impute(args, "legend.is.portrait", TRUE), "portrait", "landscape"),
reverse = v3_impute(args, "legend.reverse", FALSE))

fill.legend = do.call("tm_legend", fill.legend.args)

if ("legend.hist" %in% names(args) && args$legend.hist) {
fill.chart = tm_chart_histogram()
v3_tm_chart_hist(layer_fun = layer_fun, vv = "fill", arg = "legend.hist")

# to do: histogram title
}

Expand Down
Loading

0 comments on commit 7a5cab5

Please sign in to comment.