Last active
March 10, 2018 21:01
-
-
Save aagarw30/239eb60899d7761767a83cee5b5f2e4c to your computer and use it in GitHub Desktop.
R Leaflet and Shiny - Example - Layers, Group and Layer Control along with markers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(shiny) # load the shiny package | |
library(leaflet) # load the leaflet package | |
# quakes dataset is used for this app. It comes with base R. | |
# ?quakes in R console to read more about the dataset | |
# str(quakes) | |
# Classifying the type of earthquake based on magnitude as Light, Moderate, Strong or Major based on the magnitude range | |
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 | |
# By the end of this code, the quakes dataset will have one more column called type | |
# classification can be done in other ways as well. | |
# Color mapping using colorfactor() | |
# colorfactor() conveniently maps factor/char data values to colors according to a given palette. | |
pal = colorFactor(palette = c("yellow", "brown", "red"), domain = quakes$type) | |
# shiny server function begins here | |
shinyServer(function(input, output, session){ | |
output$mymap <- renderLeaflet({ | |
# mapping the quakes | |
mymap = leaflet(data=quakes) %>% # initialize the map ad dataset to be used | |
# add 3rd party tile | |
addProviderTiles("Esri.WorldImagery") | |
# Below code will add circle markers and groups based on types. | |
# Create groups within the addCirleMarkers. Subsetted data is used for individual groups | |
for(t in unique(quakes$type)){ # loop through each of the types we defined earlier | |
sub = quakes[quakes$type==t,] # subset the data based on each type | |
mymap=mymap %>% | |
# add Circlemarker | |
addCircleMarkers( | |
data=sub, | |
lng = ~ long, | |
lat = ~ lat, | |
radius = 5, | |
color = ~pal(type), | |
stroke = FALSE, | |
fillOpacity = 0.5, | |
label= paste( | |
"Magnitude: ", sub$mag, "Type:", sub$type, "Depth: ", sub$depth, "Stations: ", sub$stations, | |
sep = "" | |
), | |
group = t | |
) | |
} | |
# Below code to add control layers and legend to the map. | |
mymap %>% | |
addLayersControl( | |
overlayGroups = unique(quakes$type), | |
options = layersControlOptions(collapsed = FALSE) | |
) %>% | |
# add legend to the map | |
addLegend( | |
position = "bottomright", | |
pal = pal, | |
values = ~ type, | |
title = "Type based on magnitude", | |
opacity = 0.3 | |
) | |
}) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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