diff --git a/dev/tag-message b/dev/tag-message index 54af56c9412adf40c628eb6d913b9994e73160dc..a05446c51d7364433a162f68bf1c544b97d9a188 100644 --- a/dev/tag-message +++ b/dev/tag-message @@ -1,16 +1,13 @@ -so.ii Version: 1.0.27.0 +so.ii Version: 1.0.28.0 0 errors ✔ | 0 warnings ✔ | 0 notes ✔ **Note de version** - * ajout de aggregate_wide - * ajout de add_margin + * ajout de add_bar **Détails** - * aggregate_wide permet de renvoyer directement une matrice quand un - aggregate est réalisé sur 2 dimensions. - * add_margin permet d'utiliser addmargins directement sur un data.frame + * add_bar permet d'ajouter une barre d'échelle à des cartes. -# git tag -a v1.0.27.0 -F dev/tag-message +# git tag -a v1.0.28.0 -F dev/tag-message # git push --tags diff --git a/so.ii/DESCRIPTION b/so.ii/DESCRIPTION index cf42619b4b9e1cf3a6d226794b5b12a6fc80d348..1dc34ba3b7accdecd1f68958919d9d2b014012d6 100644 --- a/so.ii/DESCRIPTION +++ b/so.ii/DESCRIPTION @@ -1,6 +1,6 @@ Package: so.ii Title: Utilities very useful to share within so_ii team -Version: 1.0.27.0 +Version: 1.0.28.0 Authors@R: c( person(given = "Frédéric", diff --git a/so.ii/NAMESPACE b/so.ii/NAMESPACE index 437c8b8b3c3f92f0fe5372e1f5558a03c431c0c0..979ca07e06c1d8db296cc2129d43a0c3402dfb9b 100644 --- a/so.ii/NAMESPACE +++ b/so.ii/NAMESPACE @@ -1,5 +1,6 @@ # Generated by roxygen2: do not edit by hand +export(add_bar) export(add_inset) export(add_margin) export(aggregate_wide) diff --git a/so.ii/R/adjust_terra_scale.R b/so.ii/R/adjust_terra_scale.R index 7bcbe79130db2ad524aa11e714a7afee8c9e36cb..2fdcfc8c092bd5d5e267289fa59c89a506f171e7 100644 --- a/so.ii/R/adjust_terra_scale.R +++ b/so.ii/R/adjust_terra_scale.R @@ -1,3 +1,61 @@ +#' @title Add a bar to a map +#' +#' @details +#' Correct some things in sbar from terra to add scale bar to plots. +#' +#' @param d integer, size of the bar in km. +#' @param xy either a character or a vector giving the position of the bar in +#' the plot. +#' @param adj numeric of length 2, adjustment for text placement. +#' @param lon numeric of length 1, may be adjusted for some plots with +#' trial-and-error strategy. +#' +#' @return NULL. +#' +#' @export +#' +#' @encoding UTF-8 +#' @author Frédéric Grelot +#' +#' @examples +#' +#' \dontrun{ +#' library(sf) +#' plot(so_ii_collectivity[0]) +#' add_bar(d = 10, xy = c(3.55, 43.47), adj = c(0.5, -1)) +#' } + + +add_bar = function( + d = NULL, + xy = NULL, + adj = NULL, + lon = 0.4788987 +) { + d = adjust_terra_scale(d) + xy = calculate_terra_xy(xy) + if (is.null(adj)) { + # Magical adjustment when scope vary to have text readable + # 0.4788987 should correspond to longitude range for so.ii scope + adj = diff(graphics::par()$usr[3:4]) + adj = c( + 0.5, + -1 * exp(lon / adj - 1)^.024 + ) + } + + terra::sbar( + d = d[1], + xy = xy, + type = "bar", + below = "km", + label = c(0, d[2]/2, d[2]), + lonlat = FALSE, + adj = adj, + cex = .8 + ) +} + adjust_terra_scale = function(d = NULL, lonlat = TRUE) { destpoint = function (p, d, b = 90, r = 6378137) { toRad = pi/180 diff --git a/so.ii/R/map_so_ii.R b/so.ii/R/map_so_ii.R index 54ff95b2a9ae31abcdff7cd8f53ae826421b7e47..4e5783b2ccbff22290a57a202f130bffc521325c 100644 --- a/so.ii/R/map_so_ii.R +++ b/so.ii/R/map_so_ii.R @@ -289,33 +289,16 @@ map_so_ii = function( ## Plot bar if (bar == TRUE) { if (identical(scope, so.ii::so_ii_limit)) { - # d = 10 - d = adjust_terra_scale(10) - xy = c(3.55, 43.47) - adj = c(0.5, -1) - } else { - # area = sf::st_area(sf::st_as_sfc(sf::st_bbox(scope))) - # d = max(pretty(units::set_units(sqrt(area) / 5, "km"))) - d = adjust_terra_scale() - xy = calculate_terra_xy("bottomleft") - # Magical adjustment when scope vary to have text readable - # 0.4788987 should correspond to longitude range for so.ii scope - adj = diff(graphics::par()$usr[3:4]) - adj = c( - 0.5, - -1 * exp(0.4788987 / adj - 1)^.024 + # Parameters fitted for so_ii scope + add_bar( + d = 10, + xy = c(3.55, 43.47), + adj = c(0.5, -1), + lon = 0.4788987 ) + } else { + add_bar() } - terra::sbar( - d = d[1], - xy = xy, - type = "bar", - below = "km", - label = c(0, d[2]/2, d[2]), - lonlat = FALSE, - adj = adj, - cex = .8 - ) } ## Plot inset diff --git a/so.ii/man/add_bar.Rd b/so.ii/man/add_bar.Rd new file mode 100644 index 0000000000000000000000000000000000000000..d8abd09847f9da499d4e2fb5ac1ac23dcd27b38c --- /dev/null +++ b/so.ii/man/add_bar.Rd @@ -0,0 +1,40 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/adjust_terra_scale.R +\encoding{UTF-8} +\name{add_bar} +\alias{add_bar} +\title{Add a bar to a map} +\usage{ +add_bar(d = NULL, xy = NULL, adj = NULL, lon = 0.4788987) +} +\arguments{ +\item{d}{integer, size of the bar in km.} + +\item{xy}{either a character or a vector giving the position of the bar in +the plot.} + +\item{adj}{numeric of length 2, adjustment for text placement.} + +\item{lon}{numeric of length 1, may be adjusted for some plots with +trial-and-error strategy.} +} +\value{ +NULL. +} +\description{ +Add a bar to a map +} +\details{ +Correct some things in sbar from terra to add scale bar to plots. +} +\examples{ + +\dontrun{ +library(sf) +plot(so_ii_collectivity[0]) +add_bar(d = 10, xy = c(3.55, 43.47), adj = c(0.5, -1)) +} +} +\author{ +Frédéric Grelot +}