Change line width of leaflet's stroke in leaflet - r

Is it possible, to make the black line thinner?
Here's a reproducible example, see also here:
library(leaflet)
leaflet() %>%
addTiles() %>%
addProviderTiles(providers$OpenStreetMap, group = "OSM") %>%
addProviderTiles(providers$Stamen.TonerLite, group = "Toner Lite") %>%
addLayersControl(baseGroups = c("OSM", "Toner Lite")) %>%
leaflet::addCircleMarkers(lat = 0,
lng = 0,
color = "black",
fillColor = "red",
stroke = TRUE,
popup = "hello",
radius = 10,
fillOpacity = 0.7)

You can specifiy the stroke weight with the weight argument:
library(leaflet)
leaflet() %>%
addTiles() %>%
addProviderTiles(providers$OpenStreetMap, group = "OSM") %>%
addCircleMarkers(lat = 0,
lng = 0,
stroke = TRUE,
weight = 1)
leaflet() %>%
addTiles() %>%
addProviderTiles(providers$OpenStreetMap, group = "OSM") %>%
addCircleMarkers(lat = 0,
lng = 0,
stroke = TRUE,
weight = 100)

Related

Add a pop-up label in a leaflet heatmap

Is it possible to add a pop-label when the user mouse over a certain point in a leaflet heatmap? For example to see depth and stations from the quakes dataset.
library(leaflet)
leaflet(quakes) %>%
addProviderTiles(providers$CartoDB.DarkMatter) %>%
setView( 178, -20, 5 ) %>%
addHeatmap(
lng = ~long, lat = ~lat, intensity = ~mag,
blur = 20, max = 0.05, radius = 15
)
## for more examples see
# browseURL(system.file("examples/heatmaps.R", package = "leaflet.extras"))
kml <- readr::read_file(
system.file("examples/data/kml/crimes.kml.zip", package = "leaflet.extras")
)
leaflet() %>%
setView(-77.0369, 38.9072, 12) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addKMLHeatmap(kml, radius = 7) %>%
addKML(
kml,
markerType = "circleMarker",
stroke = FALSE, fillColor = "black", fillOpacity = 1,
markerOptions = markerOptions(radius = 1))
I'm not sure this is what you want but you can add marker popups in the usual way:
library(leaflet)
leaflet(quakes) %>%
addProviderTiles(providers$CartoDB.DarkMatter) %>%
setView( 178, -20, 5 ) %>%
addHeatmap(
lng = ~long, lat = ~lat, intensity = ~mag,
blur = 20, max = 0.05, radius = 15
) %>%
addMarkers(lng = quakes$long, lat = quakes$lat,
popup = paste("Depth", quakes$depth, "<br>",
"Stations:", quakes$stations))
if you dont want the dominating markers visible you could add circle markers but set the fillOpacity to zero:
leaflet(quakes) %>%
addProviderTiles(providers$CartoDB.DarkMatter) %>%
setView( 178, -20, 5 ) %>%
addHeatmap(
lng = ~long, lat = ~lat, intensity = ~mag,
blur = 20, max = 0.05, radius = 15
) %>%
addCircleMarkers(lng = quakes$long, lat = quakes$lat,
fillOpacity = 0, weight = 0,
popup = paste("Depth:", quakes$depth, "<br>",
"Stations:", quakes$stations),
labelOptions = labelOptions(noHide = TRUE))

showGroup not working as expected in R with Leaflet

I have the following code:
# Get unique list of groups needed
lsl <- unique(origAddress$LIHN_Line)
# create initial leaflet
mt <- leaflet() %>%
setView(lng = sv_lng, lat = sv_lat, zoom = sv_zoom) %>%
addTiles(group = "OSM (default)") %>%
addProviderTiles(providers$Stamen.Toner, group = "Toner") %>%
addProviderTiles(providers$Stamen.TonerLite, group = "Toner Lite")
# for loop to cycle through adding layers
for(i in 1:length(lsl)){
#l <- lsl[i]
mt <- mt %>%
addCircles(
data = subset(origAddress, origAddress$LIHN_Line == lsl[i])
, group = lsl[i]
, radius = 3
, fillOpacity = 0.6
)
}
# add layercontrol
mt <- mt %>%
addLayersControl(
baseGroups = c("OSM (default)", "Toner", "Toner Lite"),
overlayGroups = lsl,
options = layersControlOptions(collapsed = TRUE
, position = "bottomright")
) %>%
showGroup("Medical")
# print map
mt
The issue is that the showGroup() function is not working as I expected. I checked the element of the map and got the following:
<span> Medical</span>
So I am not understanding why only that group isn't showing.

Create layers with leaflet from categorical data in R

I have a couple of columns with categorical data. I would like to create layers from these categorical data and plot them against a base map in R with leaflet. Each of these columns has 4 or more categories.
I am completely lost on how to attack this problem. I tried to do one category from one column and I got all points on the map back and the layer controlled all points which was not the expected behavior.
Here is what I have:
lihn_map <- leaflet(origAddress) %>%
setView(lng = sv_lng, lat = sv_lat, zoom = sv_zoom) %>%
addTiles(group = "OSM (default)") %>%
addCircleMarkers(data = origAddress
, radius = 3
, fillOpacity = 1
, group = "MI"
) %>%
addProviderTiles(providers$Stamen.Toner, group = "Toner") %>%
addProviderTiles(providers$Stamen.TonerLite, group = "Toner Lite") %>%
# Overlay groups
addCircles(~lat, ~lon, group = "MI") %>%
addLayersControl(
baseGroups = c("OSM (default)", "Toner", "Toner Lite"),
overlayGroups = "MI",
options = layersControlOptions(collapsed = FALSE)
)
lihn_map
I am trying this loop, it adds the groups to the layer control but the selection does not change the map:
lsl <- unique(origAddress$LIHN_Line)
mt <- leaflet() %>%
addTiles(group = "OSM (default)") %>%
addProviderTiles(providers$Stamen.Toner, group = "Toner") %>%
addProviderTiles(providers$Stamen.TonerLite, group = "Toner Lite")
for(i in 1:length(lsl)){
l <- lsl[i]
mt <- mt %>%
addCircleMarkers(
# I have also tried
data = subset(origAddress, LIHN_Line = lsl[i])
data = origAddress
, group = lsl[i]
, radius = 3
, fillOpacity = 0.6)
}
mt <- mt %>%
addLayersControl(
baseGroups = c("OSM (default)", "Toner", "Toner Lite"),
overlayGroups = lsl,
options = layersControlOptions(collapsed = FALSE
, position = "bottomright")
)
mt
My expectation is that this layer would only control the subset of data where the group is equal to MI Maybe I have to create subset data.frames? This seems inefficient if so and I'm sure is not the answer.
I performed the following and it worked:
# for loop to cycle through adding layers
for(i in 1:length(lsl)){
l <- lsl[i]
mt <- mt %>%
addCircles(
data = subset(origAddress, origAddress$LIHN_Line == lsl[i])
#data = origAddress
, group = lsl[i]
, radius = 3
, fillOpacity = 0.6)
}
using subset worked brilliantly

R Leaflet: Zoom Control Level

I am trying to set zoom out maximum in my R Leaflet map. I follow an example of a previous question/answer in Prevent zooming out in leaflet R-Map? , but it doesn't work. The line that should be able to do this is:
options = providerTileOptions(minzoom = 1, maxzoom = 10))
Can you guys can help me to figure out why?
Here is code:
deck_lf_par_map <- leaflet(lpoints) %>%
addPolygons(data = dio, noClip=T,
weight = 4,
dashArray="5, 1",
color = "black",
fillOpacity = .01,
smoothFactor = 0) %>%
setView(lng = mean(lpoints$long), lat = mean(lpoints$lat), zoom = 09) %>%
addProviderTiles("Stamen.TonerLite",
group = "Toner",
options = providerTileOptions(minzoom = 1, maxzoom = 10)) %>%
addTiles(group = "OSM") %>%
addProviderTiles("Esri.WorldTopoMap",
group = "Topo") %>%
addProviderTiles("OpenStreetMap.Mapnik", group = "Mapnik") %>%
addProviderTiles("CartoDB.Positron", group = "CartoDB") %>%
setMaxBounds((dioc#bbox[1,1] - .3),
(dioc#bbox[2,1] - .3),
(dioc#bbox[1,2] + .3),
(dioc#bbox[2,2] + .3)) %>%
addMarkers(lpoints$long,
lpoints$lat,
popup=ppopup,
icon = tec_icon,
group="Parishes",
clusterOptions = markerClusterOptions()) %>%
addLayersControl(baseGroups = c("Toner", "OSM", "Topo", "Mapnik", "CartoDB"),
options = layersControlOptions(collapsed = TRUE))
A couple of points:
It's minZoom and maxZoom (notice the capital Z)
You need the options in each Tile function that you want to set
the zoom levels for.
library(leaflet)
## the first two tiles have a zoom level control - the others don't
leaflet() %>%
setView(lng = 144, lat = -37, zoom = 09) %>%
addProviderTiles("Stamen.TonerLite",
group = "Toner",
options = providerTileOptions(minZoom = 8, maxZoom = 10)) %>%
addTiles(group = "OSM",
options = providerTileOptions(minZoom = 8, maxZoom = 10)) %>%
addProviderTiles("Esri.WorldTopoMap",
group = "Topo") %>%
addProviderTiles("OpenStreetMap.Mapnik", group = "Mapnik") %>%
addProviderTiles("CartoDB.Positron", group = "CartoDB") %>%
addLayersControl(baseGroups = c("Toner", "OSM", "Topo", "Mapnik", "CartoDB"),
options = layersControlOptions(collapsed = TRUE))

Add different legends in different layers on leaflet map in R

library(leaflet)
library(htmltools)
library(htmlwidgets)
library(dplyr)
#
df1 <- data.frame(points=c("p1", "p2"), lat=c(49.47259, 49.48095), long=c(-103.7054, -103.6126), value=c(50.34, 100.25))
df2 <- data.frame(points=c("p1", "p2"), lat=c(49.47809, 49.66849), long=c(-103.5614, -103.0224), value=c(300.56, 505.34))
#
pal1 <- colorNumeric(
palette = "PRGn",
domain = df1$value
)
#
pal2 <- colorNumeric(
palette = "PRGn",
domain = df2$value
)
#
n <- leaflet() %>% addTiles(group="1st layer") %>% addTiles(group="2nd layer") %>%
addCircles(data=df1, lng=~long, lat=~lat, weight = 3, radius=250, color = ~pal1(value),
stroke = TRUE, fillOpacity = 0.8,group="1st layer") %>%
addCircles(data=df2, lng=~long, lat=~lat, weight = 3, radius=250, color = ~pal2(value),
stroke = TRUE, fillOpacity = 0.8,group="2nd layer") %>%
addLegend("bottomright", pal = pal1, values = df1$value, title = "legend_df1") %>%
addLegend("topright", pal = pal2, values = df2$value, title = "legend_df2") %>%
addLayersControl(baseGroups=c("1st layer","2nd layer"),
options=layersControlOptions(collapsed = F))
n
I want that when I click on "1st layer" then only "legend_df1" will appear and when I click on "2nd layer" then only "legend_df2" will appear and "legend_df1" will be vanished. Therefore, in each layer different legends will appear, not both legends together. Can anybody please help me out?
This is now possible with overlayGroups
library(leaflet)
df1 <- data.frame(points=c("p1", "p2"), lat=c(49.47259, 49.48095), long=c(-103.7054, -103.6126), value=c(50.34, 100.25))
df2 <- data.frame(points=c("p1", "p2"), lat=c(49.47809, 49.66849), long=c(-103.5614, -103.0224), value=c(300.56, 505.34))
pal1 <- colorNumeric(
palette = "inferno",
domain = df1$value
)
pal2 <- colorNumeric(
palette = "viridis",
domain = df2$value
)
leaflet() %>%
addProviderTiles(providers$CartoDB.DarkMatter) %>%
addCircleMarkers(data=df1, lng=~long, lat=~lat,
color = ~pal1(value),
group="group_1") %>%
addCircleMarkers(data=df2, lng=~long, lat=~lat,
color = ~pal2(value),
group="group_2") %>%
addLegend("bottomright", pal = pal1, title="grp1",
values = df1$value, group="group_1") %>%
addLegend("bottomright", pal = pal2, title="grp2",
values = df2$value, group="group_2") %>%
addLayersControl(overlayGroups = c("group_1","group_2"),
options = layersControlOptions(collapsed = FALSE))

Resources