I'm wondering if anyone else has experienced similar issues when plotting a large number of markers and polygons using leaflet package in R. This is what it normally should look like:
However, when I zoom in/out of the map, the polygons and markers are clearly out of place (or you can say the base map does not adjust properly). An example is included below:
I would not have this issue when I plotted a smaller area or few markers. I'm wondering if there is a way to improve the performance. Many thanks in advance for your help!
A sample of my code is included below:
map1 <- leaflet() %>%
addProviderTiles("CartoDB.Positron") %>%
addPolygons(data = data_merged, group="Default",
fillColor = ~pal(minority_population), color = "orange",
fillOpacity = 0.7,weight = 1, smoothFactor = 0.2, popup = popup) %>%
addMarkers(data = branches_temp, ~long, ~lat,
popup=~name_branch, group="Branch Locations",
icon=icons(iconUrl = "./data/bank_blue_marker.png",iconWidth=20, iconHeight=20))
Related
For example, I made a simple leaflet based map using tmap package. The result is an html with interactive map, showing some polygons of buildings (find data, code and the map itself here).
I want polygons to be highlighted someway when selected by html user by mouse click, like it is in QGIS (screenshot adjusted). Is there a way to do such thing in R?
You may consider switching over to library(leaflet)
map <- tm_shape(buildings) +
tm_polygons(col = "#ff00bf")
leaflet(data = buildings) %>%
addPolygons(highlightOptions = highlightOptions(color = "yellow",
weight = 2,
fillColor = 'yellow',
bringToFront = TRUE))
I am making some maps in Leaflet in R, and the maps consist of a polygon layer as well as a layer of markers. The polygon usually covers a larger area than the markers, but I want to zoom specifically to the marker area.
I know I can manually specify lat/longs and zoom level, but I don't want to do that because this code will generate a lot of different maps covering different subsets of my dataset (or on different datasets).
Here's a simplified version of the code that generates the map. I don't think this necessarily requires a full reprex to answer the question but can do so if necessary.
addProviderTiles(providers$CartoDB.Positron, group = "Basemap") %>%
addPolygons(data = seat_boundary, fillColor = "white", fillOpacity = 0.0, weight = 1.5, opacity = 0.6, color = "red", group = "Seat boundary") %>%
addCircleMarkers(data = formalvotes_by_booth_subarea, fillColor = ~subarea_pal(sub_areas), lng=~longitude, lat=~latitude, radius = ~circle_size, fillOpacity = 1, color = 'white', opacity = 1, weight = 1.2, popup = ~popup) %>%
addLegend(pal = subarea_pal, values = formalvotes_by_booth_subarea$sub_areas, opacity = 1)```
I also think I might be able to half-solve this problem by averaging the max and min latitude and max and min longitude in the markers dataset to come up with a central point to focus the map on, but it wouldn't ensure the zoom would be correct (indeed I'm not sure if you can set the lat/long and not set the zoom).
Thanks!
Have you tried fitBounds?
Starting from what you have posted, I would first get the bbox coordinates of your markers:
library(sf)
bounds <- markers %>%
st_bbox() %>%
as.character()
Then passing them into the fitBounds function:
library(leaflet)
leaflet %>%
addPolygons(data = polygons) %>%
addCircleMarkers(data = markers) %>%
fitBounds(bounds[1], bounds[2], bounds[3], bounds[4])
I'm new to R. I've just made a basic interactive choropleth map using the leaflet package in R:
leaflet() %>%
addProviderTiles("CartoDB.Positron") %>%
setView(-2, 55, zoom = 5) %>%
addPolygons(data = shapes_funding_merge ,
fillColor = ~pal(shapes_funding_merge$PerPerson),
fillOpacity = 0.7,
weight = 0.2,
smoothFactor = 0.2,
popup = ~popup2) %>%
addLegend(pal = pal,
values = shapes_funding_merge$PerPerson,
position = "bottomright",
title = "Per-head funding (£)")
I want to make this embeddable on a web page. Can I create an embed code that I could send to someone else to embed the leaflet map on their web page?
I hope this isn't a silly question. I've read some other threads but can't seem to find what I think I need.
Any help would be very much appreciated.
Thanks
output$countPlot <- renderLeaflet({
leaflet(cleanData) %>%
addTiles() %>%
addCircleMarkers(lng =~longitude+rnorm(1,0,0.0001), lat = ~latitude+rnorm(1, 0, 0.0001), clusterOptions = markerClusterOptions(), label = ~id)})
My code now displays a leaflet map in R shiny with markers clustered and shows how many data points in each marker. I want to hide the numbers on the clustered markers but still show the color and size proportional to the data points in the cluster.
Please find the map below:
Is there anyway to change the color of leaflet line base on the value of some variable? I google it, and found this link.
However, I was wondering if there is a simple way to do it with leaflet in R. I tried something like:
data <- data.frame(long = runif(40,-10,10), lat = runif(40,50,60), speed = runif(40,0,100))
leaflet(data) %>% addTiles() %>% addPolylines(lng = ~long, lat = ~lat, color = ~speed)
but, it shows only one color.