Create a gist now

Instantly share code, notes, and snippets.

Basic leaflet mapping in R
#Load the library and make a basic map
leaflet() %>% addTiles()
#Show a map with a satellite picture on it
leaflet() %>%
#Make a demo fake data set
myData <- data.frame(Latitude = runif(100, -80,80),
Longitude = runif(100, -180,180),
measurement1 = rnorm(100, -3, 2),
Year = round(runif(100, 1975,2015)))
#Plot sites on an interactive map
leaflet() %>%
addTiles() %>%
addCircles(data=myData, lat = ~Latitude, lng = ~Longitude)
#Make a label for the next map
myData$info <- paste0("Year: ", myData$Year,
"<br> Value: ", round(myData$measurement1, 3))
#make some colors!
#grab a palette
pal <- brewer.pal(11, "Spectral")
#now make it more continuous
#as a colorRamp
pal <- colorRampPalette(pal)
#now, map it to your values
palData <- classIntervals(myData$measurement1, style="quantile")
#note, we use pal(100) for a smooth palette here
#but you can play with this
myData$colors <- findColours(palData, pal(100))
#Put it all on a map!
leaflet() %>%
addTiles() %>%
addCircles(data=myData, lat = ~Latitude, lng = ~Longitude,
color = ~colors, popup = ~info)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment