From 346bb9179d874a90fba004d57c3feafcb77669d3 Mon Sep 17 00:00:00 2001 From: mtennekes Date: Thu, 7 Mar 2024 17:54:16 +0100 Subject: [PATCH] fixed #383 and added backward-comp with legend.hist --- R/tm_layers_lines.R | 4 ++++ R/tm_layers_polygons.R | 9 +++++++-- R/tm_layers_raster.R | 5 +++++ R/tm_layers_symbols.R | 9 ++++++++- R/tm_layers_text.R | 33 +++++++++++++++++++++++++-------- R/tmapScaleRGB.R | 8 ++++---- man/tm_text.Rd | 21 ++++++++++++++------- 7 files changed, 67 insertions(+), 22 deletions(-) diff --git a/R/tm_layers_lines.R b/R/tm_layers_lines.R index 8b244c3d..6906ffa5 100644 --- a/R/tm_layers_lines.R +++ b/R/tm_layers_lines.R @@ -176,6 +176,10 @@ tm_lines = function(col = tm_const(), lwd.legend = do.call("tm_legend", lwd.legend.args) + if ("legend.hist" %in% names(args) && args$legend.hist) { + col.chart = tm_chart_histogram() + # to do: histogram title + } } diff --git a/R/tm_layers_polygons.R b/R/tm_layers_polygons.R index 9347a9fd..873916b5 100644 --- a/R/tm_layers_polygons.R +++ b/R/tm_layers_polygons.R @@ -78,7 +78,6 @@ tm_polygons = function(fill = tm_const(), fill.legend = tm_legend(), fill.chart = tm_chart_none(), fill.free = NA, - #fill.chart = tm_chart_none(), col = tm_const(), col.scale = tm_scale(), col.legend = tm_legend(), @@ -180,7 +179,7 @@ tm_polygons = function(fill = tm_const(), fill.scale = do.call("tm_scale", args = fill.scale.args) - if ("col" %in% names(args_called) && (args_called$called_from != "fill")) { + if ("col" %in% names(args_called) && (is.null(args_called$called_from) || (args_called$called_from != "fill"))) { fill = col col = tm_const() } @@ -203,6 +202,12 @@ tm_polygons = function(fill = tm_const(), reverse = imp("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() + # to do: histogram title + } + } diff --git a/R/tm_layers_raster.R b/R/tm_layers_raster.R index 6d857562..7b709384 100644 --- a/R/tm_layers_raster.R +++ b/R/tm_layers_raster.R @@ -138,6 +138,11 @@ tm_raster = function(col = tm_shape_vars(), reverse = imp("legend.reverse", FALSE)) col.legend = do.call("tm_legend", col.legend.args) + + if ("legend.hist" %in% names(args) && args$legend.hist) { + col.chart = tm_chart_histogram() + # to do: histogram title + } } # needed for color maps without categories (then tm_scale_categorical is used without legend, unless called) diff --git a/R/tm_layers_symbols.R b/R/tm_layers_symbols.R index 97ce6f02..87201ba7 100644 --- a/R/tm_layers_symbols.R +++ b/R/tm_layers_symbols.R @@ -143,6 +143,7 @@ tm_symbols = function(size = tm_const(), size.legend = res$size.legend shape.scale = res$shape.scale shape.legend = res$shape.legend + fill.chart = res$fill.chart } @@ -388,6 +389,10 @@ v3_symbols = function(args, args_called) { shape.legend = do.call("tm_legend", shape.legend.args) + if ("legend.hist" %in% names(args) && args$legend.hist) { + fill.chart = tm_chart_histogram() + # to do: histogram title + } list(fill = fill, col = col, @@ -397,7 +402,8 @@ v3_symbols = function(args, args_called) { size.scale = size.scale, size.legend = size.legend, shape.scale = shape.scale, - shape.legend = shape.legend) + shape.legend = shape.legend, + fill.chart = fill.chart) } else { NULL } @@ -454,6 +460,7 @@ tm_bubbles = function(size = tm_const(), size.legend = res$size.legend shape.scale = res$shape.scale shape.legend = res$shape.legend + fill.chart = res$fill.chart } tm_element_list(tm_element( diff --git a/R/tm_layers_text.R b/R/tm_layers_text.R index c7040799..d9ee3f5f 100644 --- a/R/tm_layers_text.R +++ b/R/tm_layers_text.R @@ -28,15 +28,15 @@ #' own legend. For facet wraps and stacks ([tm_facets_wrap()] and [tm_facets_stack()]) #' there is only one facet dimension, so the `.free` argument requires only one logical value. #' -#' @param text,text.scale,text.legend,text.free Visual variable that determines +#' @param text,text.scale,text.legend,text.chart,text.free Visual variable that determines #' the text. See details. -#' @param size,size.scale,size.legend,size.free Visual variable that determines +#' @param size,size.scale,size.legend,size.chart,size.free Visual variable that determines #' the font size. See details. -#' @param col,col.scale,col.legend,col.free Visual variable that determines +#' @param col,col.scale,col.legend,col.chart,col.free Visual variable that determines #' the col color. See details. -#' @param col_alpha,col_alpha.scale,col_alpha.legend,col_alpha.free Visual variable that determines +#' @param col_alpha,col_alpha.scale,col_alpha.legend,col_alpha.chart,col_alpha.free Visual variable that determines #' the border color alpha transparency. See Details. -#' @param fontface,fontface.scale,fontface.legend,fontface.free Visual variable that determines +#' @param fontface,fontface.scale,fontface.legend,fontface.chart,fontface.free Visual variable that determines #' the font face. See Details. #' @param fontfamily The font family. See [gpar()][grid::gpar()] for details. #' @param shadow Shadow behind the text. Logical or color. @@ -53,9 +53,9 @@ #' groups can be switched on and off. Options: `"radio"` for radio buttons #' (meaning only one group can be shown), `"check"` for check boxes (so multiple groups can be shown), #' and `"none"` for no control (the group cannot be (de)selected). -#' @param bgcol,bgcol.scale,bgcol.legend,bgcol.free Visual variable that determines +#' @param bgcol,bgcol.scale,bgcol.legend,bgcol.chart,bgcol.free Visual variable that determines #' the background color. See Details. -#' @param bgcol_alpha,bgcol_alpha.scale,bgcol_alpha.legend,bgcol_alpha.free Visual variable that determines +#' @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 ... to catch deprecated arguments from version < 4.0 #' @example ./examples/tm_text.R @@ -63,31 +63,38 @@ tm_text = function(text = tm_const(), text.scale = tm_scale(), text.legend = tm_legend(), + text.chart = tm_chart_none(), text.free = NA, size = tm_const(), size.scale = tm_scale(), size.legend = tm_legend(), + size.chart = tm_chart_none(), size.free = NA, col = tm_const(), col.scale = tm_scale(), col.legend = tm_legend(), + col.chart = tm_chart_none(), col.free = NA, col_alpha = tm_const(), col_alpha.scale = tm_scale(), col_alpha.legend = tm_legend(), + col_alpha.chart = tm_chart_none(), col_alpha.free = NA, fontface = tm_const(), fontface.scale = tm_scale(), fontface.legend = tm_legend(), + fontface.chart = tm_chart_none(), fontface.free = NA, fontfamily = "", bgcol = tm_const(), bgcol.scale = tm_scale(), bgcol.legend = tm_legend(), + bgcol.chart = tm_chart_none(), bgcol.free = NA, bgcol_alpha = tm_const(), bgcol_alpha.scale = tm_scale(), bgcol_alpha.legend = tm_legend(), + bgcol_alpha.chart = tm_chart_none(), bgcol_alpha.free = NA, shadow = FALSE, plot.order = tm_plot_order("AREA", reverse = FALSE, na.order = "bottom"), @@ -192,7 +199,10 @@ tm_text = function(text = tm_const(), orientation = ifelse(imp("legend.size.is.portrait", TRUE), "portrait", "landscape"), reverse = imp("legend.size.reverse", FALSE)) - + if ("legend.hist" %in% names(args) && args$legend.hist) { + col.chart = tm_chart_histogram() + # to do: histogram title + } #} } @@ -208,36 +218,43 @@ tm_text = function(text = tm_const(), value = text, scale = text.scale, legend = text.legend, + chart = text.chart, free = text.free), size = tmapScale(aes = "size", value = size, scale = size.scale, legend = size.legend, + chart = size.chart, free = size.free), col = tmapScale(aes = "col", value = col, scale = col.scale, legend = col.legend, + chart = col.chart, free = col.free), col_alpha = tmapScale(aes = "col_alpha", value = col_alpha, scale = col_alpha.scale, legend = col_alpha.legend, + chart = col_alpha.chart, free = col_alpha.free), bgcol = tmapScale(aes = "bgcol", value = bgcol, scale = bgcol.scale, legend = bgcol.legend, + chart = bgcol.chart, free = bgcol.free), bgcol_alpha = tmapScale(aes = "bgcol_alpha", value = bgcol_alpha, scale = bgcol_alpha.scale, legend = bgcol_alpha.legend, + chart = bgcol_alpha.chart, free = bgcol_alpha.free), fontface = tmapScale(aes = "fontface", value = fontface, scale = fontface.scale, legend = fontface.legend, + chart = fontface.chart, free = fontface.free)), gpar = tmapGpar(fill = NA, diff --git a/R/tmapScaleRGB.R b/R/tmapScaleRGB.R index fb55e7c9..5a64387b 100644 --- a/R/tmapScaleRGB.R +++ b/R/tmapScaleRGB.R @@ -48,7 +48,7 @@ tmapScaleRGB = function(x1, x2, x3, scale, legend, chart, o, aes, layer, layer_a } -tmapScaleAsIs = function(x1, scale, legend, o, aes, layer, layer_args, sortRev, bypass_ord, submit_legend = TRUE) { +tmapScaleAsIs = function(x1, scale, legend, chart, o, aes, layer, layer_args, sortRev, bypass_ord, submit_legend = TRUE) { legend = list(title = NA, nitems = 0, labels = NA, @@ -67,11 +67,11 @@ tmapScaleAsIs = function(x1, scale, legend, o, aes, layer, layer_args, sortRev, if (submit_legend) { if (bypass_ord) { - format_aes_results(values, legend = legend) + format_aes_results(values, legend = legend, chart = chart) } else { - format_aes_results(values, ord = 1L, legend = legend) + format_aes_results(values, ord = 1L, legend = legend, chart = chart) } } else { - list(vals = values, ids = 1L, legend = legend, bypass_ord = bypass_ord) + list(vals = values, ids = 1L, legend = legend, chart = chart, bypass_ord = bypass_ord) } } \ No newline at end of file diff --git a/man/tm_text.Rd b/man/tm_text.Rd index 184c4dd2..17f1b35c 100644 --- a/man/tm_text.Rd +++ b/man/tm_text.Rd @@ -8,31 +8,38 @@ tm_text( text = tm_const(), text.scale = tm_scale(), text.legend = tm_legend(), + text.chart = tm_chart_none(), text.free = NA, size = tm_const(), size.scale = tm_scale(), size.legend = tm_legend(), + size.chart = tm_chart_none(), size.free = NA, col = tm_const(), col.scale = tm_scale(), col.legend = tm_legend(), + col.chart = tm_chart_none(), col.free = NA, col_alpha = tm_const(), col_alpha.scale = tm_scale(), col_alpha.legend = tm_legend(), + col_alpha.chart = tm_chart_none(), col_alpha.free = NA, fontface = tm_const(), fontface.scale = tm_scale(), fontface.legend = tm_legend(), + fontface.chart = tm_chart_none(), fontface.free = NA, fontfamily = "", bgcol = tm_const(), bgcol.scale = tm_scale(), bgcol.legend = tm_legend(), + bgcol.chart = tm_chart_none(), bgcol.free = NA, bgcol_alpha = tm_const(), bgcol_alpha.scale = tm_scale(), bgcol_alpha.legend = tm_legend(), + bgcol_alpha.chart = tm_chart_none(), bgcol_alpha.free = NA, shadow = FALSE, plot.order = tm_plot_order("AREA", reverse = FALSE, na.order = "bottom"), @@ -45,27 +52,27 @@ tm_text( ) } \arguments{ -\item{text, text.scale, text.legend, text.free}{Visual variable that determines +\item{text, text.scale, text.legend, text.chart, text.free}{Visual variable that determines the text. See details.} -\item{size, size.scale, size.legend, size.free}{Visual variable that determines +\item{size, size.scale, size.legend, size.chart, size.free}{Visual variable that determines the font size. See details.} -\item{col, col.scale, col.legend, col.free}{Visual variable that determines +\item{col, col.scale, col.legend, col.chart, col.free}{Visual variable that determines the col color. See details.} -\item{col_alpha, col_alpha.scale, col_alpha.legend, col_alpha.free}{Visual variable that determines +\item{col_alpha, col_alpha.scale, col_alpha.legend, col_alpha.chart, col_alpha.free}{Visual variable that determines the border color alpha transparency. See Details.} -\item{fontface, fontface.scale, fontface.legend, fontface.free}{Visual variable that determines +\item{fontface, fontface.scale, fontface.legend, fontface.chart, fontface.free}{Visual variable that determines the font face. See Details.} \item{fontfamily}{The font family. See \link[grid:gpar]{gpar()} for details.} -\item{bgcol, bgcol.scale, bgcol.legend, bgcol.free}{Visual variable that determines +\item{bgcol, bgcol.scale, bgcol.legend, bgcol.chart, bgcol.free}{Visual variable that determines the background color. See Details.} -\item{bgcol_alpha, bgcol_alpha.scale, bgcol_alpha.legend, bgcol_alpha.free}{Visual variable that determines +\item{bgcol_alpha, bgcol_alpha.scale, bgcol_alpha.legend, bgcol_alpha.chart, bgcol_alpha.free}{Visual variable that determines the background color transparency. See Details.} \item{shadow}{Shadow behind the text. Logical or color.}