I am trying to make an interactive Leaflet map in R. I have a shapefile that looks something like this:
# my_shapefile
Simple feature collection with 6 features and 5 fields
geometry type: MULTIPOLYGON
dimensions : xy
bbox: xmin ... y min ... xmax ... ymax...
CRS: EPSG: 4326
ID: A, B, A
Geometry : MULTIPOLYGON ((( 55.23, -71.22 ....
value : 0.12, 0.15, 0.13
I made the following map:
#taken from here: https://cengel.github.io/R-spatial/mapping.html
library(sf)
library(leaflet)
library(classInt)
library(RColorBrewer)
sf <- sf::st_read("my_shapefile.shp", options = "ENCODING-WINDOWS-1252")
#note: "value" comes from another file that I merged to the shapefile
st_trans <- st_transform(sf, 4326)
breaks_qt <- classIntervals(sf$value, n=7, style "quantile")
pal_fun <- colorQuantile("YlOrRd", NULL, n = 5)
# here is the problem I think
p_popup <- paste0("<strong> Value : </strong>", sf$value)
leaflet(st_trans) %>%
addPolygons(
stroke = FALSE,
fillColor = ~ pal_fun(value),
fillOpacity = 0.8, smoothFactor = 0.5,
popup = p_popup,
group = "my_group") %>%
addTiles(group = "OSM") %>%
addProviderTiles("CartoDB.DarkMatter", group = "Carto") %>%
addLegend("bottomright",
colors = brewer.pal(7, "YlOrRd"),
labels = paste0("up to ", format(breaks_qt$brks[-1], digits = 2)),
title = 'my legend') %>%
addLayersControl(baseGroups = c("OSM", "Carto"), overlapGroups = c("my_group"))
This map runs fine, the problem is that when I move the mouse over the map, the popup for "value" appears with many decimal points - I would like to restrict the number of decimal points to only two. The "value" variable was calculated by dividing two variables together, so I suspect that the many decimal points are coming from this.
I first tried to turn off the decimal points using the code found here (Formatting Decimal places in R), but this did not work:
#did not work
options(digits=2)
Next, I tried to manually keep only two decimal points, but this also did not work (i.e. the visualization is still displaying many decimal points):
# did not work
sf$value = substr(sf$value, 1,4)
Finally, I found this post here (R Leaflet Legend: colorBin- removing decimals in between breaks) and tried to follow this advice for restricting the number of decimal points:
#did not work
leaflet(st_trans) %>%
addPolygons(
stroke = FALSE,
fillColor = ~ pal_fun(value),
fillOpacity = 0.8, smoothFactor = 0.5,
popup = p_popup,
group = "my_group") %>%
addTiles(group = "OSM") %>%
addProviderTiles("CartoDB.DarkMatter", group = "Carto") %>%
addLegend("bottomright", labFormat = labelFormat(prefix = '', suffix = '', between = ' – ', digits = 3, big.mark = ',', transform = identity),
colors = brewer.pal(7, "YlOrRd"),
labels = paste0("up to ", format(breaks_qt$brks[-1], digits = 2)),
title = 'my legend') %>%
addLayersControl(baseGroups = c("OSM", "Carto"), overlapGroups = c("my_group"))
- Can someone please show me what I am doing wrong and what I can do to ensure that the popup in the leaflet map only contains two decimal points?
Thank you!