diff --git a/DESCRIPTION b/DESCRIPTION index eba88e01..bdc4fc26 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -27,7 +27,7 @@ Imports: Rcpp (>= 1.0.10), shiny, sfheaders (>= 0.4.4) -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.1 LinkingTo: BH, colourvalues (>= 0.3.9), diff --git a/NAMESPACE b/NAMESPACE index 5a99766d..13929861 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -58,6 +58,7 @@ export(add_h3) export(add_heatmap) export(add_hexagon) export(add_i3s) +export(add_legend) export(add_line) export(add_mesh) export(add_path) diff --git a/NEWS.md b/NEWS.md index f1d1335a..c0ca1489 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # v0.3.6 +* `add_legend` and `clear_legend()` for adding custom legends to the map [issue 390](https://github.com/SymbolixAU/mapdeck/issues/390) * `add_text()` gets `elevation` argument for when using a `data.frame` * `map_click` event added to shiny - e.g. `observeEvent({input$map_click})` diff --git a/R/map_legend.R b/R/map_legend.R new file mode 100644 index 00000000..794f65a0 --- /dev/null +++ b/R/map_legend.R @@ -0,0 +1,83 @@ + +#' Add Legend +#' +#' Add a custom legend to the map +#' +#' @inheritParams add_scatterplot +#' +#' @examples +#' +#' \donttest{ +#' sf <- spatialwidget::widget_melbourne +#' sf$my_colour <- ifelse( substr(sf$SA2_NAME, 1, 1) == "A", "#00FF00FF", "#FF0000FF") +#' +#' l1 <- legend_element( +#' variables = c("Begins with A", "Doesn't begin with A") +#' , colours = c("#00FF00FF", "#FF0000FF") +#' , colour_type = "fill" +#' , variable_type = "category" +#' ) +#' js <- mapdeck_legend(l1) +#' +#' set_token(secret::get_secret("MAPBOX")) +#' mapdeck() %>% +#' add_legend(legend = js, layer_id = "my_layer") +#' +#' +#'mapdeck() %>% +#' add_legend(legend = js, layer_id = "my_layer") %>% +#' clear_legend(layer_id = "my_layer") +#' +#' } +#' +#' @export +add_legend <- function(map, legend, layer_id) { + + legend_format <- "hex" + + invoke_method( + map, "md_add_legend", map_type(map), layer_id, legend, legend_format + ) +} + + +#' Clear Legend +#' +#' Removes a legend from the map +#' +#' @inheritParams add_legend +#' +#' @examples +#' \donttest{ +#' +#' sf <- spatialwidget::widget_melbourne +#' sf$my_colour <- ifelse( substr(sf$SA2_NAME, 1, 1) == "A", "#00FF00FF", "#FF0000FF") +#' +#' l1 <- legend_element( +#' variables = c("Begins with A", "Doesn't begin with A") +#' , colours = c("#00FF00FF", "#FF0000FF") +#' , colour_type = "fill" +#' , variable_type = "category" +#' ) +#' js <- mapdeck_legend(l1) +#' +#' set_token(secret::get_secret("MAPBOX")) +#' +#' ## Add a legend +#' mapdeck() %>% +#' add_legend(legend = js, layer_id = "my_layer") +#' +#' ## Calling `clear_legend` should immediately remove it +#' mapdeck() %>% +#' add_legend(legend = js, layer_id = "my_layer") %>% +#' clear_legend(layer_id = "my_layer") +#' +#' } +#' +#' @export +clear_legend <- function(map, layer_id) { + + invoke_method( + map, "md_clear_legend", map_type(map), layer_id + ) +} diff --git a/man/add_legend.Rd b/man/add_legend.Rd new file mode 100644 index 00000000..38f2aaed --- /dev/null +++ b/man/add_legend.Rd @@ -0,0 +1,47 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/map_legend.R +\name{add_legend} +\alias{add_legend} +\title{Add Legend} +\usage{ +add_legend(map, legend, layer_id) +} +\arguments{ +\item{map}{a mapdeck map object} + +\item{legend}{either a logical indiciating if the legend(s) should be displayed, or +a named list indicating which colour attributes should be included in the legend.} + +\item{layer_id}{single value specifying an id for the layer. Use this value to +distinguish between shape layers of the same type. Layers with the same id are likely +to conflict and not plot correctly} +} +\description{ +Add a custom legend to the map +} +\examples{ + +\donttest{ +sf <- spatialwidget::widget_melbourne +sf$my_colour <- ifelse( substr(sf$SA2_NAME, 1, 1) == "A", "#00FF00FF", "#FF0000FF") + +l1 <- legend_element( + variables = c("Begins with A", "Doesn't begin with A") + , colours = c("#00FF00FF", "#FF0000FF") + , colour_type = "fill" + , variable_type = "category" +) +js <- mapdeck_legend(l1) + +set_token(secret::get_secret("MAPBOX")) +mapdeck() \%>\% + add_legend(legend = js, layer_id = "my_layer") + + +mapdeck() \%>\% + add_legend(legend = js, layer_id = "my_layer") \%>\% + clear_legend(layer_id = "my_layer") + +} + +} diff --git a/man/add_text.Rd b/man/add_text.Rd index 5f5ef5f8..4d3dfc9b 100644 --- a/man/add_text.Rd +++ b/man/add_text.Rd @@ -10,6 +10,7 @@ add_text( text, lon = NULL, lat = NULL, + elevation = NULL, polyline = NULL, fill_colour = NULL, fill_opacity = NULL, diff --git a/man/clear_legend.Rd b/man/clear_legend.Rd index 438e438b..1fb42b26 100644 --- a/man/clear_legend.Rd +++ b/man/clear_legend.Rd @@ -1,9 +1,11 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/legend.R +% Please edit documentation in R/legend.R, R/map_legend.R \name{clear_legend} \alias{clear_legend} \title{Clear Legend} \usage{ +clear_legend(map, layer_id) + clear_legend(map, layer_id) } \arguments{ @@ -15,4 +17,34 @@ to conflict and not plot correctly} } \description{ Clears the legend for a given layer_id + +Removes a legend from the map +} +\examples{ +\donttest{ + +sf <- spatialwidget::widget_melbourne +sf$my_colour <- ifelse( substr(sf$SA2_NAME, 1, 1) == "A", "#00FF00FF", "#FF0000FF") + +l1 <- legend_element( + variables = c("Begins with A", "Doesn't begin with A") + , colours = c("#00FF00FF", "#FF0000FF") + , colour_type = "fill" + , variable_type = "category" +) +js <- mapdeck_legend(l1) + +set_token(secret::get_secret("MAPBOX")) + +## Add a legend +mapdeck() \%>\% + add_legend(legend = js, layer_id = "my_layer") + +## Calling `clear_legend` should immediately remove it +mapdeck() \%>\% + add_legend(legend = js, layer_id = "my_layer") \%>\% + clear_legend(layer_id = "my_layer") + +} + }