Skip to content

Commit

Permalink
xmod ymod working #840
Browse files Browse the repository at this point in the history
  • Loading branch information
mtennekes committed Mar 13, 2024
1 parent ad3e758 commit 25469b6
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 15 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ S3method(tmapLeafletCompHeight,tm_mouse_coordinates)
S3method(tmapLeafletCompHeight,tm_scalebar)
S3method(tmapLeafletCompHeight,tm_title)
S3method(tmapLeafletCompPrepare,tm_chart)
S3method(tmapLeafletCompPrepare,tm_chart_none)
S3method(tmapLeafletCompPrepare,tm_compass)
S3method(tmapLeafletCompPrepare,tm_credits)
S3method(tmapLeafletCompPrepare,tm_legend_standard_landscape)
Expand Down
2 changes: 1 addition & 1 deletion R/step3_trans.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ step3_trans = function(tm) {
sdcols = names(transDT)

if (length(transDT)) {
y = transDT[, .(shp = do.call(do_trans, list(tdt = .SD, FUN = al$trans_fun, shpDT = shpDT, plot.order = plot.order, args = al$trans_args))), by = bycols, .SDcols = sdcols]
y = transDT[, .(shp = do.call(do_trans, list(tdt = .SD, FUN = al$trans_fun, shpDT = shpDT, plot.order = plot.order, args = al$trans_args, scale = o$scale))), by = bycols, .SDcols = sdcols]
shpDT = rbindlist(y$shp)
} else {
shpDT$shpTM = lapply(shpDT$shpTM, function(s) do.call(al$trans_fun, list(shpTM = s, plot.order = plot.order, args = al$trans_args)))
Expand Down
26 changes: 25 additions & 1 deletion R/tm_layers_text.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
#' the background color. See Details.
#' @param bgcol_alpha,bgcol_alpha.scale,bgcol_alpha.legend,bgcol_alpha.chart,bgcol_alpha.free Visual variable that determines
#' the background color transparency. See Details.
#' @param xmod,xmod.scale,xmod.legend,xmod.chart,xmod.free Transformation variable that determines the x offset. See details.
#' @param ymod,ymod.scale,ymod.legend,ymod.chart,ymod.free Transformation variable that determines the y offset. See details.
#' the text. See details.
#' @param ... to catch deprecated arguments from version < 4.0
#' @example ./examples/tm_text.R
#' @export
Expand Down Expand Up @@ -96,6 +99,16 @@ tm_text = function(text = tm_const(),
bgcol_alpha.legend = tm_legend(),
bgcol_alpha.chart = tm_chart_none(),
bgcol_alpha.free = NA,
xmod = 0,
xmod.scale = tm_scale(),
xmod.legend = tm_legend_hide(),
xmod.chart = tm_chart_none(),
xmod.free = NA,
ymod = 0,
ymod.scale = tm_scale(),
ymod.legend = tm_legend_hide(),
ymod.chart = tm_chart_none(),
ymod.free = NA,
shadow = FALSE,
plot.order = tm_plot_order("AREA", reverse = FALSE, na.order = "bottom"),
trans.args = list(points.only = "ifany"),
Expand Down Expand Up @@ -210,7 +223,18 @@ tm_text = function(text = tm_const(),
tm_element_list(tm_element(
layer = "text",
trans.fun = tmapTransCentroid,
trans.aes = list(),
trans.aes = list(xmod = tmapScale(aes = "xmod",
value = xmod,
scale = xmod.scale,
legend = xmod.legend,
chart = xmod.chart,
free = xmod.free),
ymod = tmapScale(aes = "ymod",
value = ymod,
scale = ymod.scale,
legend = ymod.legend,
chart = ymod.chart,
free = ymod.free)),
trans.args = trans.args,
trans.isglobal = FALSE,
mapping.aes = list(
Expand Down
1 change: 1 addition & 0 deletions R/tm_scale_.R
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ tm_scale_continuous = function(n = NULL,
#' @param label.na (generic scale argument) Label for missing values
#' @param label.null (generic scale argument) Label for null (out-of-scope) values
#' @param label.format (generic scale argument) Label formatting (similar to `legend.format` in tmap3)
#' @param unit unit the unit name of the values. By default `"rank"`.
#' @export
tm_scale_rank = function(n = NULL,
ticks = NULL,
Expand Down
7 changes: 7 additions & 0 deletions R/tmapLeafletComp_charts.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ tmapLeafletCompPrepare.tm_chart = function(comp, o) {
comp
}

#' @method tmapLeafletCompPrepare tm_chart_none
#' @export
tmapLeafletCompPrepare.tm_chart_none = function(comp, o) {
comp
}


#' @method tmapLeafletCompWidth tm_chart
#' @export
tmapLeafletCompWidth.tm_chart = function(comp, o) {
Expand Down
7 changes: 3 additions & 4 deletions R/tmapScale_.R
Original file line number Diff line number Diff line change
Expand Up @@ -156,23 +156,22 @@ tmapScaleAuto = function(x1, scale, legend, chart, o, aes, layer, layer_args, so
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 (!is.null(x2)) {
sc = "bivariate"
} else if (cls[1] == "asis") {
sc = "asis"
} else if (attr(cls, "unique")) {
} 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).")
} else {
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_opt = getAesOption("scales.var", o, aes, layer, cls = cls)

sc_pref = scale$fun_pref

if (!is.null(sc_pref)) {
Expand Down
30 changes: 29 additions & 1 deletion R/tmapScale_defaults.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ tmapValuesCheck_lty = function(x) {
TRUE
}

tmapValuesCheck_xmod = tmapValuesCheck_ymod = function(x) {
# to do
TRUE
}

tmapValuesCheck_col_alpha= function(x) {
tmapValuesCheck_size(x)
Expand Down Expand Up @@ -112,6 +116,11 @@ tmapValuesIsDiv_area = function(x) {
tmapValuesIsDiv_size(x)
}

tmapValuesIsDiv_xmod = tmapValuesIsDiv_ymod = function(x) {
tmapValuesIsDiv_size(x)
}


tmapValuesIsDiv_shape = function(x) {
FALSE
}
Expand Down Expand Up @@ -177,6 +186,11 @@ tmapValuesRange_area = function(x, n, isdiv) {
tmapValuesRange_size(x, n, isdiv)
}

tmapValuesRange_xmod = tmapValuesRange_ymod = function(x, n, isdiv) {
tmapValuesRange_size(x, n, isdiv)
}


tmapValuesRange_text = function(x, n, isdiv) {
c(0, 1)
}
Expand Down Expand Up @@ -342,6 +356,10 @@ tmapValuesVV_area = function(...) {
do.call(tmapValuesVV_size, args = list(...))
}

tmapValuesVV_xmod = tmapValuesVV_ymod = function(...) {
do.call(tmapValuesVV_size, args = list(...))
}

tmapValuesVV_text = function(x, value.na, isdiv, n, dvalues, are_breaks, midpoint, range, scale, rep, o) {
list(vvalues = rep(x, length.out = n), value.neutral = x[1], value.na = value.na)
}
Expand All @@ -355,6 +373,8 @@ tmapValuesSubmit_col = function(x, args) x
tmapValuesSubmit_fill = function(x, args) x
tmapValuesSubmit_bgcol = function(x, args) x
tmapValuesSubmit_size = function(x, args) x
tmapValuesSubmit_xmod = function(x, args) x
tmapValuesSubmit_ymod = function(x, args) x
tmapValuesSubmit_lwd = function(x, args) x
tmapValuesSubmit_lty = function(x, args) x
tmapValuesSubmit_shape = function(x, args) {
Expand Down Expand Up @@ -402,7 +422,7 @@ tmapValuesScale_fill_alpha = function(x, scale) x
tmapValuesScale_bgcol_alpha = function(x, scale) x
tmapValuesScale_text = function(x, scale) x
tmapValuesScale_fontface = function(x, scale) x

tmapValuesScale_xmod = tmapValuesScale_ymod = function(x, scale) x

tmapValuesColorize_col = function(x, pc) do.call(process_color, c(list(col = x), pc))
tmapValuesColorize_fill = function(x, pc) do.call(process_color, c(list(col = x), pc))
Expand All @@ -416,6 +436,7 @@ tmapValuesColorize_fill_alpha = function(x, pc) x
tmapValuesColorize_bgcol_alpha = function(x, pc) x
tmapValuesColorize_text = function(x, pc) x
tmapValuesColorize_fontface = function(x, pc) x
tmapValuesColorize_xmod = tmapValuesColorize_ymod = function(x, pc) x



Expand Down Expand Up @@ -511,6 +532,13 @@ tmapValuesCVV_area = function(x, value.na, n, range, scale, rep, o) {
tmapValuesVV_area(x = x, value.na = value.na, isdiv = FALSE, n = n, dvalues = NA, are_breaks = FALSE, midpoint = NA, range = range, scale = scale, rep = rep)
}

tmapValuesCVV_xmod = function(x, value.na, n, range, scale, rep, o) {
tmapValuesVV_xmod(x = x, value.na = value.na, isdiv = FALSE, n = n, dvalues = NA, are_breaks = FALSE, midpoint = NA, range = range, scale = scale, rep = rep)
}

tmapValuesCVV_ymod = function(x, value.na, n, range, scale, rep, o) {
tmapValuesVV_ymod(x = x, value.na = value.na, isdiv = FALSE, n = n, dvalues = NA, are_breaks = FALSE, midpoint = NA, range = range, scale = scale, rep = rep)
}

tmapValuesCVV_shape = function(x, value.na, n, range, scale, rep, o) {
tmapValuesVV_shape(x = x, value.na = value.na, isdiv = FALSE, n = n, dvalues = NA, are_breaks = FALSE, midpoint = NA, range = range, scale = scale, rep = rep)
Expand Down
30 changes: 24 additions & 6 deletions R/tmapTrans.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
do_trans = function(tdt, FUN, shpDT, plot.order, args) {
do_trans = function(tdt, FUN, shpDT, plot.order, args, scale) {
#browser()

shpDT = copy(shpDT)
Expand All @@ -23,15 +23,21 @@ do_trans = function(tdt, FUN, shpDT, plot.order, args) {

x = as.list(tdt[match(tmapID__, ids), aesvars, with = FALSE])

res = do.call(FUN, c(list(shpTM = shpX), x, list(plot.order = plot.order, args = args)))
res = do.call(FUN, c(list(shpTM = shpX), x, list(plot.order = plot.order, args = args, scale = scale)))
}
shpDT$shpTM = lapply(shpDT$shpTM, apply_trans)
list(shpDT)
}

delta_per_lineheight = function(x, n = 20, scale = 1) {
b = unname(sf::st_bbox(x))
(b[4] - b[2]) / n * scale
}


# args:
# - points.only: "yes", "no", "ifany"
tmapTransCentroid = function(shpTM, ord__, plot.order, args) {
tmapTransCentroid = function(shpTM, xmod = NULL, ymod = NULL, ord__, plot.order, args, scale) {
within(shpTM, {
is_stars = inherits(shp, "dimensions")
if (is_stars && args$points.only == "no") {
Expand Down Expand Up @@ -67,6 +73,18 @@ tmapTransCentroid = function(shpTM, ord__, plot.order, args) {
})
}
}

if (!is.null(xmod) || !is.null(ymod)) {
shp = local({
d = delta_per_lineheight(shp, scale = scale)

if (is.null(xmod)) xmod = rep(0, length(shp))
if (is.null(ymod)) ymod = rep(0, length(shp))
mod = mapply(c, xmod * d, ymod * d, SIMPLIFY = FALSE)
shp + mod
})
}

rm(geom_types)
}
})
Expand All @@ -81,7 +99,7 @@ tmapTransRaster = function(shpTM, ord__, plot.order, args) {

# args:
# - polygons.only: "yes", "no", "ifany"
tmapTransPolygons = function(shpTM, ord__, plot.order, args) {
tmapTransPolygons = function(shpTM, ord__, plot.order, args, scale) {
within(shpTM, {
is_stars = inherits(shp, "dimensions")
if (is_stars && args$polygons.only == "no") {
Expand Down Expand Up @@ -132,7 +150,7 @@ tmapTransPolygons = function(shpTM, ord__, plot.order, args) {

# args:
# - lines.only: "yes", "no", "ifany"
tmapTransLines = function(shpTM, ord__, plot.order, args) {
tmapTransLines = function(shpTM, ord__, plot.order, args, scale) {
within(shpTM, {
is_stars = inherits(shp, "dimensions")
if (is_stars) {
Expand Down Expand Up @@ -178,7 +196,7 @@ tmapTransLines = function(shpTM, ord__, plot.order, args) {
})
}

tmapTransCartogram = function(shpTM, area, ord__, plot.order, args) {
tmapTransCartogram = function(shpTM, area, ord__, plot.order, args, scale) {
s = shpTM$shp

if (sf::st_is_longlat(s)) {
Expand Down
6 changes: 5 additions & 1 deletion R/tmap_options.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@
col_alpha = c(0.25, 1),
bgcol_alpha = c(0.25, 1),
shape = 21:25,
area = c(0, 1)),
area = c(0, 1),
xmod = c(0, 1),
ymod = c(0, 1)),
values.range = list(fill = NA, col = NA, size = c(0, 1), lwd = c(0.1, 1),
lty = NA, text = NA, fontface = NA, fill_alpha = NA,
col_alpha = NA, shape = NA), # NA = automatic, NULL is not applicable
Expand Down Expand Up @@ -149,6 +151,8 @@
col_alpha = list(fact = "categorical", num = "intervals"),
bgcol_alpha = list(fact = "categorical", num = "intervals"),
area = list(fact = "categorical", num = "continuous"),
xmod = list(fact = "asis", num = "asis"),
ymod = list(fact = "asis", num = "asis"),
text = list(fact = "asis", num = "asis"),
fontface = list(fact = "categorical", num = "categorical")),

Expand Down
9 changes: 9 additions & 0 deletions examples/tm_text.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,12 @@ tm_shape(World) +
bgcol.scale = tm_scale_categorical(values = cols4all::.P$hcl$cat$set2),
bgcol_alpha = "pop_est",
bgcol_alpha.scale = tm_scale_intervals(style = "kmeans"))

## xymod
metro$initial = substr(metro$name,1,1)
metro$az = (match(metro$initial, LETTERS)-1) /25

tm_shape(metro) +
tm_dots("red") +
tm_text("initial", ymod = "az")

4 changes: 4 additions & 0 deletions man/tm_chart.Rd

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

5 changes: 4 additions & 1 deletion man/tm_scale_rank.Rd

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

24 changes: 24 additions & 0 deletions man/tm_text.Rd

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

0 comments on commit 25469b6

Please sign in to comment.