Jacky
Jacky

Reputation: 750

Change legend bin size in leaflet

I created a density map but R has chosen bin sizes that are too wide. I want to create my own bin sizes. https://i.sstatic.net/p49qc.jpg

The image shows bin sizes at a full integer and it does not produce anything meaningful. I also cannot get the Esri.WorldGrayCanvas base map to show up but that's less important.

library(tmap)
library(tmaptools)
library(leaflet)
library(tidyverse)


us_geo = read_shape("taxi_zones_sp.shp",as.sf = T,stringsAsFactors = F)
popmap = append_data(us_geo
                     ,dropoffs
                     ,key.shp = "Taxi_zone"
                     ,key.data = "pu_taxi_zone")
#turn na's into zero.
popmap = popmap %>%
  mutate_all(funs(replace(., is.na(.), 0)))


my_map =
  tm_shape(popmap) +
  tm_fill("perc", palette = "Oranges"
          ,title = "pickup density (%)", id = "Taxi_zone") +
  tm_borders(alpha=.4)

leaflet() %>%
  setView(lng = -74.058913, lat = 40.689852, zoom = 10) %>%
  addProviderTiles("Esri.WorldGrayCanvas") + my_map

I would like to have 0 show up as white and the rest show up in .5 increments. and if possible, the Esri.WorldGrayCanvas to show up in my map. I also want to keep the map in a static format. So icons/controls like zooming and panning are not ideal.

Upvotes: 0

Views: 2183

Answers (1)

Stedy
Stedy

Reputation: 7469

I don't have access to your data set, so I just downloaded similar looking data from NYU. In order to properly set the width of your bins you need to use the breaks parameter in the tm_fill function:

library(tmap)
library(tmaptools)
library(leaflet)
library(tidyverse)

nyc_geo = read_shape("nyu_2451_36743.shp",as.sf = T,stringsAsFactors = F)

nyc_zone <- nyc_geo[4]

#I just made up some sample data for the purposes of this question
nyc_zone$rate = sample(seq(0,4,0.5), size=nrow(nyc_zone), replace=T)

breaks = seq(0,4,0.5)

my_map =
  tm_shape(nyc_zone) +
  tm_fill("rate", palette = "Oranges", breaks= breaks
          ,title = "pickup density (%)", id = "Taxi_zone") +
  tm_borders(alpha=.4)

leaflet() %>%
  setView(lng = -74.058913, lat = 40.689852, zoom = 10) %>%
  addProviderTiles("Esri.WorldGrayCanvas") + my_map

Which produces the following plot:

enter image description here

For the second part of your question, you want to give Leaflet a SpatialPolygonsDataFrame instead of the tmap element you are using now. Here is a basic workup of reading in the shapefile and then adding the tiles of interest:

library(leaflet)
library(sp)
nyc_map <- rgdal::readOGR(dsn = "nyu_2451_36743.shp", layer = "nyu_2451_36743")
nyc_map$rate = sample(seq(0,4,0.5), size=nrow(nyc_map), replace=T)

pal <- colorBin(palette = "Oranges", domain = nyc_map$rate, bins = breaks)

leaflet(nyc_map) %>%
  addPolygons(fillColor = ~pal(rate),
              weight = 2,
              opacity = 1,
              color = "white",
              fillOpacity = 0.7) %>%
  addProviderTiles("Esri.WorldGrayCanvas") %>%
  addLegend(pal = pal, values = ~rate, opacity = 0.7, title = NULL,
            position = "bottomright")

Here is a static image of that map:

enter image description here

Upvotes: 3

Related Questions