I have asked this question before here.
Initially the focus was on a point map, which the answer fixed. However looking at clustered and heatmaps, the issue persists.
Point generates fine (fixed via previous question)
Clustered do not appear on map
Heatmap doesn't show at all (whitespace)
Minimal code example:
library(tidyverse)
library(leaflet)
library(leaflet.extras)
leaflet()
long <- as.numeric(c("0.005638", "0.005648", "0.005658"))
lat <- as.numeric(c("51.62879", "51.62889", "51.62879"))
data1 <- data.frame(long, lat)
filtered_list <- 1:3
cat("## Tabs {.tabset .tabset-fade .tabset-pills}", "\n")
for (estates in filtered_list){
cat("###", estates, "\n")
cat("\n\n\n")
cat("This is where the map will go ")
cat("1 ")
# generate leaflet plot
page <- htmltools::tagList(
leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=data1$long, lat=data1$lat)
)
cat(as.character(page))
cat("2 ")
page1 <- htmltools::tagList(
leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=data1$long, lat=data1$lat, clusterOptions = markerClusterOptions())
)
cat(as.character(page1))
cat("3 ")
page2 <- htmltools::tagList(
leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=data1$long, lat=data1$lat) %>%
addHeatmap(
lng = data1$lat, lat = data1$long,
blur = 20, max = 5, radius = 40
)
)
cat(as.character(page2))
}
The maps render fine in R Studio, e.g.
Related
I followed the tutorial here (https://rstudio.github.io/leaflet/) and made this map:
library(leaflet)
m <- leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R") %>% setView(174.768, -36.852, zoom = 12)
When I run this code, the map comes out like this:
However, I would like the map to have the "zoom out" option disabled (I would like to keep the "zoom in" option, as well as the side-to-side scroll option).
Is there some way that prevents the user from being abled to "zoom out" from the specified zoom level, and only allows the user to "zoom in"?
I tried:
# source: https://stackoverflow.com/questions/36365897/r-leaflet-zoomcontrol-option
m <- leaflet((options = leafletOptions(zoomControl = FALSE,
minZoom = 3, maxZoom = 3,
dragging = FALSE)) %>%
addTiles() %>%
addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")) %>% setView(174.768, -36.852, zoom = 12)
But this gives me an error: Error in dispatch(map, method, leaflet = { : Invalid map parameter
Thank you!
I'm running into a problem with leaflet where it is drawing a giant rectangle instead of the shapes. I'm certain there is some issue with the format of the shapefile, but I can't determine what's going wrong. Plotting the file works fine.
file: https://upload.cat/8c8ade09a3489b47
original file source: http://sites.psu.edu/psucz/data/ (at bottom of page)
require(tidyverse)
require(leaflet)
require(rgdal)
ers_shp <- readOGR("ERS10.shp")
#Doesn't work, produces rectangle:
leaflet() %>% addProviderTiles("CartoDB.Positron") %>% addPolygons(data = ers_shp)
#Works, indicating the data is there.
plot(ers_shp, col="#f2f2f2", fill=TRUE, bg="skyblue", lwd=0.25, mar=rep(0,4), border=0 )
This is because you need to convert the polygons to lat/long before passing them to leaflet:
library(sf)
inv <- sf::st_read("ERS10.rep.shp") %>%
sf::st_transform(4326)
leaflet() %>% addProviderTiles("CartoDB.Positron") %>% addPolygons(data = inv)
OR
library(sp)
inv <- rgdal::readOGR("ERS10.rep.shp") %>%
spTransform(CRS("+proj=longlat +datum=WGS84"))
leaflet() %>% addProviderTiles("CartoDB.Positron") %>% addPolygons(data = inv)
I am trying tu use David Rumsey' Georeferencer tiles in R leaflet package without success (Only the OSM basemap is displayed).
According to the package vignette I tried :
library(leaflet)
# XYZ direct link
leaflet() %>%
setView(0.65, 0, zoom = 5) %>%
addTiles() %>%
addTiles("https://maps.georeferencer.com/georeferences/700526190853/2017-12-30T11:48:27.589686Z/map.json?key=D7AwmpRP1H6pUic6DIK3")
and
library(leaflet)
# WMS tiles
leaflet() %>%
addTiles() %>% setView(0.65, 0, zoom = 5) %>%
addWMSTiles(
"https://maps.georeferencer.com/georeferences/700526190853/2017-12-30T11:48:27.589686Z/wmts?key=D7AwmpRP1H6pUic6DIK3&SERVICE=WMTS&REQUEST=GetCapabilities",
layers = "1", # I assume
options = WMSTileOptions(format = "image/png", transparent = FALSE),
attribution = "")
Notes :
- The access at the map links need free registration.
- I used the 2.1 package release.
- The vignette's addWMSTiles example is working on my computer.
I solve my problem using the addLayersControl function that can allow me to choose the layer to display.
library(leaflet)
m <- leaflet() %>%
addProviderTiles(providers$CartoDB.DarkMatterNoLabels, group = "DarkMatter") %>%
addTiles("https://maps.georeferencer.com/georeferences/700526190853/2017-12-30T11:48:27.589686Z/map/{z}/{x}/{y}.png?key=D7AwmpRP1H6pUic6DIK3", group ="H") %>%
addLayersControl(
baseGroups = c("DarkMatter", "H"), position ="topleft")
m
I am trying to map San Francisco's crimes in a map. The below code intends to map every crime (lat, lng) and when the marker is clicked, show the "category" column of the dataset.
Right now the below code shows a blank text box when I click the marker.
Can anybody help?
sf <- read.csv("https://raw.githubusercontent.com/uwescience/datasci_course_materials/master/assignment6/sanfrancisco_incidents_summer_2014.csv")
crime <- data.frame(lat = c(sf$Y),
lng = c(sf$X))
cat <- c(sf$Category)
library(leaflet)
crime %>%
leaflet() %>%
addTiles() %>%
addMarkers(popup = paste(sf$Category), clusterOptions = markerClusterOptions())
Try the following:
sf <- read.csv("https://raw.githubusercontent.com/uwescience/datasci_course_materials/master/assignment6/sanfrancisco_incidents_summer_2014.csv")
library(leaflet)
sf %>%
leaflet() %>%
addTiles() %>%
addMarkers(lat = ~Y, lng = ~X, popup = ~Category, clusterOptions = markerClusterOptions())
I'm not sure what your issue is, but using the formula syntax allows leaflet to build the list of pop-up labels on its own and does not require calling paste explicitly or subsetting the original dataframe.
I have a shapefile of neighborhood areas in NYC (https://nycopendata.socrata.com/City-Government/Neighborhood-Tabulation-Areas/cpf4-rkhq).
When I use use leaflet to overlay these polygons the background tiles are not shown.
This is the code I am using:
library(rgdal)
library(leaflet)
nyc = readOGR("geo_export_da30afd0-e475-44e2-90d2-aca31344ef5e.shp",
layer="geo_export_da30afd0-e475-44e2-90d2-aca31344ef5e")
m = leaflet() %>% fitBounds(lng1 = -74.458921, lat1 = 40.550302,
lng2 = -73.683035, lat2=40.89225)
m %>% addTiles()
m %>% addPolygons(data=nyc)
This is the output:
m %>% addTiles() %>% addPolygons(data = nyc)
should work. %>% does not assign but only pipes contents, therefore needs to be a complete chain.