Skip to content

Instantly share code, notes, and snippets.

@Sandy4321
Forked from abresler/leafletMapWithLegend.R
Created December 18, 2015 15:35
Show Gist options
  • Save Sandy4321/ba08b2aca03f27fcaf75 to your computer and use it in GitHub Desktop.
Save Sandy4321/ba08b2aca03f27fcaf75 to your computer and use it in GitHub Desktop.
#devtools::install_github("rstudio/leaflet", ref="feature/color-legend")
library(leaflet)
library(RColorBrewer)
set.seed(100)
pdf <- data.frame(Latitude = runif(100, -90,90), Longitude = runif(100, -180,180))
#make a property with colors
pdf$Study <- rep(1:10,10)
#need to create a pal using colorbin
pal = colorBin('RdYlBu', as.numeric(pdf$Study), 5)
#the map with a legend
leaflet(pdf) %>% addTiles() %>%
addCircleMarkers(lat = ~ Latitude, lng = ~ Longitude, color= pal(pdf$Study)) %>%
addLegend(pal = pal, values = pdf$Study, title="Study")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment