Skip to content

Commit

Permalink
add and clear legend
Browse files Browse the repository at this point in the history
  • Loading branch information
dcooley committed Dec 17, 2024
1 parent 72c7985 commit ad936c9
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 2 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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})`

Expand Down
83 changes: 83 additions & 0 deletions R/map_legend.R
Original file line number Diff line number Diff line change
@@ -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
)
}
47 changes: 47 additions & 0 deletions man/add_legend.Rd

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

1 change: 1 addition & 0 deletions man/add_text.Rd

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

34 changes: 33 additions & 1 deletion man/clear_legend.Rd

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

0 comments on commit ad936c9

Please sign in to comment.