Skip to content

Instantly share code, notes, and snippets.

@aagarw30
Last active March 10, 2018 14:00
Show Gist options
  • Save aagarw30/28e15f59f4221fc84abd to your computer and use it in GitHub Desktop.
Save aagarw30/28e15f59f4221fc84abd to your computer and use it in GitHub Desktop.
R Shiny and leaflet - Mapping the quakes dataset - demo app with leaflet methods and features - addCircles & addCirclemarkers
library(shiny)
library(leaflet)
shinyServer(function(input, output, session){
output$mymap <- renderLeaflet({
# Classifying the type of earthquake based on magnitude
quakes$type = ifelse((quakes$mag >= 4 & quakes$mag <= 4.9), "Light [4-4.9]",
ifelse((quakes$mag >= 5 & quakes$mag <= 5.9), "Moderate [5-5.9]",
ifelse((quakes$mag >= 6 & quakes$mag <= 6.9), "Strong [6-6.9]",
ifelse((quakes$mag >= 7 & quakes$mag <= 7.9), "Major [7-7.9]", "Great"
))))
# Above Earthquake type based on it's magnitude is taken from http://www.geo.mtu.edu/UPSeis/magnitude.html
# Color mapping using colorfactor()
# colorfactor() conveniently maps factor/char data values to colors according to a given palette.
pal = colorFactor(palette = c("green", "yellow", "red"), domain = quakes$type)
leaflet(data=quakes) %>%
addProviderTiles("CartoDB.Positron") %>%
addCircles(lng=~long, lat=~lat, radius=5, color =~pal(type), popup=paste("mag=", quakes$mag)) %>%
addLegend(position="bottomleft", pal=pal, values = ~type, title = "Type based on magnitude", opacity = 0.3)
})
})
library(shiny)
library(leaflet)
shinyUI(bootstrapPage(
h4("Mapping location of earthquakes off Fiji from quakes dataset"),
h5("The data set give the locations of 1000 seismic events of MB > 4.0. The events occurred in a cube near Fiji since 1964.
These are sample from a larger dataset of 5000 observations."),
leafletOutput("mymap", height = "600")
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment