Learn R Programming

simplevis (version 1.5.0)

leaflet_sf_col: Map of simple features in leaflet that is coloured.

Description

Map of simple features in leaflet that is coloured.

Usage

leaflet_sf_col(data, col_var, label_var = NULL, col_method = NULL,
  bin_cuts = NULL, quantile_cuts = c(0, 0.25, 0.5, 0.75, 1),
  pal = NULL, rev_pal = FALSE, col_scale_drop = FALSE,
  popup = leafpop::popupTable(sentence_spaced_colnames(data)),
  radius = 1, weight = 2, opacity = 0.9, stroke = TRUE,
  title = "[Title]", legend_digits = 1, legend_labels = NULL,
  basemap = "light", shiny = FALSE, map_id = "map")

Arguments

data

An sf object of geometry type point/multipoint, linestring/multilinestring or polygon/multipolygon geometry type. Required input.

col_var

Unquoted variable to colour the features by. Required input.

label_var

Unquoted variable to label the features by. If NULL, defaults to using the colour variable.

col_method

The method of colouring features, either "bin", "quantile" or "category." if categorical colour variable, NULL results in "category". If numeric variable, defaults to "quantile". Note all numeric variables are cut to be inclusive of the min in the range, and exclusive of the max in the range (except for the final bucket which includes the highest value).

bin_cuts

A vector of bin cuts applicable where col_method of "bin" is selected. The first number in the vector should be either -Inf or 0, and the final number Inf. If NULL, 'pretty' breaks are used. Only applicable where col_method equals "bin".

quantile_cuts

A vector of probability cuts applicable where col_method of "quantile" is selected. The first number in the vector should 0 and the final number 1. Defaults to quartiles. Only applicable where col_method equals "quantile".

pal

Character vector of hex codes. Defaults to NULL, which selects the colorbrewer Set1 or viridis.

rev_pal

Reverses the palette. Defaults to FALSE.

col_scale_drop

TRUE or FALSE of whether to drop unused levels from the legend. Defaults to FALSE.

popup

HTML strings for use in popup. Defaults to making a leafpop::popupTable of all attribute columns in the sf object.

radius

Radius of points. Defaults to 1.

weight

Stroke border size. Defaults to 2.

opacity

The opacity of polygons. Defaults to 0.9.

stroke

TRUE or FALSE of whether to draw a border around the features. Defaults to TRUE.

title

A title string that will be wrapped into the legend. Defaults to "Title".

legend_digits

Select the appropriate number of decimal places for numeric variable auto legend labels. Defaults to 1.

legend_labels

A vector of manual legend label values. Defaults to NULL, which results in automatic labels.

basemap

The underlying basemap. Either "light", "dark", "satellite", "street", or "ocean". Defaults to "light". Only applicable where shiny equals FALSE.

shiny

TRUE or FALSE for whether the map is being run within a shiny app. Defaults to FALSE.

map_id

The shiny map id for a leaflet map within a shiny app. For standard single-map apps, id "map" should be used. For dual-map apps, "map1" and "map2" should be used. Defaults to "map".

Value

A leaflet object.

Examples

Run this code
# NOT RUN {
leaflet_sf_col(example_sf_nz_livestock, dairydens,
     col_method = "quantile", quantile_cuts = c(0, 0.25, 0.5, 0.75, 0.95, 1),
     title = "Dairy density in count per km\u00b2, 2017")

leaflet_sf_col(example_sf_nz_livestock, dairydens,
     col_method = "bin", bin_cuts = c(0, 10, 50, 100, 150, 200, Inf), legend_digits = 0,
     title = "Dairy density in count per km\u00b2, 2017")

map_data <- example_sf_nz_river_wq %>%
  dplyr::filter(period == "1998-2017", indicator == "Nitrate-nitrogen")

pal <- c("#4575B4", "#D3D3D3", "#D73027")

leaflet_sf_col(map_data, trend_category, pal = pal, col_method = "category",
   title = "Monitored river nitrate-nitrogen trends, 2008\u201317")
# }

Run the code above in your browser using DataLab