Commit 1cc6557b authored by Grelot Frederic's avatar Grelot Frederic :swimmer_tone5:
Browse files

Préparation de la version 1.0.29.0 de so.ii

No related merge requests found
Showing with 112 additions and 33 deletions
+112 -33
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
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",
......
# Generated by roxygen2: do not edit by hand
export(add_bar)
export(add_inset)
export(add_margin)
export(aggregate_wide)
......
#' @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
......
......@@ -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
......
% 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
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment