If we just take a standard leaflet map like the one below:
library(leaflet)
library(maps)
mapStates = map("state", fill = TRUE, plot = FALSE)
leaflet(data = mapStates) %>% addTiles() %>%
addPolygons(fillColor = topo.colors(10, alpha = NULL), stroke = FALSE)
Is there a way to add a button that adds and removes certain polygons? For example, if the map above contained several layers (i.e. uninsured rates & average age), I would want a button or toggle switch that would allow a user to add and remove all states that begin with "a" (silly example). So instead of having 4 layers (two with those statistics across all states and two with those statistics across the states that do not begin with "a"), I would only have two layers and a button.
My caveat is that it needs to be exportable in .html format, which means I cannot deploy a shiny solution.
Perhaps there's an addEasyButton solution?
You will need to add a unique identifier in the group parameter of the addPolygons() function, then pipe to addLayersControl(), per https://rstudio.github.io/leaflet/showhide.html
I am not able to fully provide code, since I don't have your mapStates data, but here's an attempt
library(leaflet)
library(maps)
mapStates = map("state", fill = TRUE, plot = FALSE)
leaflet(data = mapStates) %>% addTiles() %>%
addPolygons(fillColor = topo.colors(10, alpha = NULL), stroke = FALSE, group = "stateShape") %>%
addLayersControl(overlayGroups = c("stateShape"))
I have been working with plotting GPX data from hiking trails. I can download and extract all of the route data, but when I plot them as polylines, it only plots a small segment. I have confirmed that the file is complete by running a function that breaks about the GPX file, creates a data frame of lat and lon, and plots them as markers or circlemarkers. This is very slow for the files that I am working with.
The code is as follows:
library(rgdal)
library(maps)
library(htmltools)
library(devtools)
library(leaflet)
library(sp)
library(htmlwidgets)
library(plotKML)
library(maptools)
library(XML)
url <- "http://hiking.waymarkedtrails.org/en/routebrowser/1225378/gpx"
download.file(url, destfile = "pct.gpx", method = "wininet")
pct <- readOGR("pct.gpx", layer = "tracks")
# Import list with shapefiles of the three states the PCT is crossing
mapStates <- map("state", fill = TRUE,
plot = FALSE,
region = c('california', 'oregon', 'washington:main'))
your.map <- leaflet(pct) %>%
# Add layer
addTiles(urlTemplate = "http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png") %>%
addPolylines(color="red", popup="PCT") %>%
addMarkers(-116.4697, 32.60758, popup = "Campo") %>%
addMarkers(-120.7816, 49.06465, popup = "Manning Park, Canada") %>%
addPolygons(data=mapStates, fillColor = heat.colors(3, alpha = NULL), stroke = FALSE) %>%
# Add legend
addLegend(position = 'topright', colors = "red", labels = "PCT", opacity = 0.4,
title = 'Legend')
your.map
This code works, you get a map, shaded properly and the markers are in the right place.
However, only one small line segment is plotted. There are 12 lines in the GPX file which I can see by viewing the pct object, but it appears to be only plotting one. No matter which GPX file I download, it's the same problem.
This is probably related to a bug in leaflet. There is already an issue filed at github, see here.
In mapview we have fixed this and MultiLines are drawn correctly. See the issue conversation here.
As a reproducible example (taken from the mapview issue):
library(trajectories)
data(storms)
x = as(storms, "SpatialLinesDataFrame")
plot(x)
library(mapview)
mapview(x)
leaflet() %>% addTiles() %>% addPolylines(data = x)
or for your example, simply
your.map <- mapview(pct, map.types = "CartoDB.Positron")#map %>%
addMarkers(-116.4697, 32.60758, popup = "Campo") %>%
addMarkers(-120.7816, 49.06465, popup = "Manning Park, Canada") %>%
addPolygons(data=mapStates, fillColor = heat.colors(3, alpha = NULL), stroke = FALSE) %>%
# Add legend
addLegend(position = 'topright', colors = "red", labels = "PCT", opacity = 0.4,
title = 'Legend')
your.map
NOTE: This is currently only fixed in the development version of mapview which can be installed with devtools::install_github("environmentalinformatics-marburg/mapview", ref = "develop")
I am learning how to use R and Leaflet. I am almost done making a map using California counties, but I don't like that I can see other states in the map. I would like to white out the map around my polygon (the counties). I saw a similar problem resolved elsewhere but I don't know enough to apply what was said there to my code. Could someone check this code out and suggest what I need to add?
If you scroll to the bottom of this link you'll see what I am trying to do. http://rpubs.com/stefanya/127436
The code I am using is:
#loading shapefile
counties <- readOGR("./shapefiles", layer="cb_2014_us_county_20m")
#filtering for only california
counties <- subset(counties, counties#data$STATEFP=="06")
#making a leaflet map of california counties
leaflet() %>% addTiles() %>% addPolygons(data=counties)
#merging the data into this shapefile
counties#data = data.frame(counties#data, sumByCounty[match(counties#data[,"NAME"], sumByCounty[,"NAME"]),])
#set color palette
colorRamp <- colorRamp(c("#2c7fb8","#7fcdbb","#edf8b1"), interpolate = "spline")
palette <- colorNumeric(colorRamp, counties#data$progress)
leaflet() %>% addProviderTiles("Stamen.TonerLite") %>%
addPolygons(
weight= 2,
stroke = TRUE,
fillOpacity = .65,
data=counties,
color = ~palette(progress),
popup = ~paste("<strong>County:</strong>",NAME,
"<br>",
"<strong>Total Responses:</strong>",sumByCounty,
"<br>",
"<strong>Complete:</strong>",progress,"<strong>%</strong>")
) %>% addLegend(title = "Response <br> Goal Met", pal = palette, values = counties#data$progress, bins=5, opacity = 1, position="topright", labFormat = labelFormat(suffix = '%'))
Create a polygon with with two rings, the first with geometry of the entire earth (or your view's bounds), the second with the geometry of California:
[
// World
[[90,-180], [90,180], [-90,180], [-90,-180]],
// California
[[42.006186,-123.233256],[42.011663,-122.378853],[41.995232,-121.037003],[41.995232,-120.001861],[40.264519,-119.996384],[38.999346,-120.001861],[38.101128,-118.71478],[37.21934,-117.498899],[36.501861,-116.540435],[35.970598,-115.85034],[35.00118,-114.634459],[34.87521,-114.634459],[34.710902,-114.470151],[34.448009,-114.333228],[34.305608,-114.136058],[34.174162,-114.256551],[34.108438,-114.415382],[33.933176,-114.535874],[33.697668,-114.497536],[33.54979,-114.524921],[33.40739,-114.727567],[33.034958,-114.661844],[33.029481,-114.524921],[32.843265,-114.470151],[32.755634,-114.524921],[32.717295,-114.72209],[32.624187,-116.04751],[32.536556,-117.126467],[32.668003,-117.24696],[32.876127,-117.252437],[33.122589,-117.329114],[33.297851,-117.471515],[33.538836,-117.7837],[33.763391,-118.183517],[33.703145,-118.260194],[33.741483,-118.413548],[33.840068,-118.391641],[34.042715,-118.566903],[33.998899,-118.802411],[34.146777,-119.218659],[34.26727,-119.278905],[34.415147,-119.558229],[34.40967,-119.875891],[34.475393,-120.138784],[34.448009,-120.472878],[34.579455,-120.64814],[34.858779,-120.609801],[34.902595,-120.670048],[35.099764,-120.631709],[35.247642,-120.894602],[35.450289,-120.905556],[35.461243,-121.004141],[35.636505,-121.168449],[35.674843,-121.283465],[35.784382,-121.332757],[36.195153,-121.716143],[36.315645,-121.896882],[36.638785,-121.935221],[36.6114,-121.858544],[36.803093,-121.787344],[36.978355,-121.929744],[36.956447,-122.105006],[37.115279,-122.335038],[37.241248,-122.417192],[37.361741,-122.400761],[37.520572,-122.515777],[37.783465,-122.515777],[37.783465,-122.329561],[38.15042,-122.406238],[38.112082,-122.488392],[37.931343,-122.504823],[37.893004,-122.701993],[38.029928,-122.937501],[38.265436,-122.97584],[38.451652,-123.129194],[38.566668,-123.331841],[38.698114,-123.44138],[38.95553,-123.737134],[39.032208,-123.687842],[39.366301,-123.824765],[39.552517,-123.764519],[39.831841,-123.85215],[40.105688,-124.109566],[40.259042,-124.361506],[40.439781,-124.410798],[40.877937,-124.158859],[41.025814,-124.109566],[41.14083,-124.158859],[41.442061,-124.065751],[41.715908,-124.147905],[41.781632,-124.257444],[42.000709,-124.213628],[42.006186,-123.233256]]
]
Example on Plunker: http://embed.plnkr.co/ZWzuxz/preview