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)
Related
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.
I would like to visualize municipalities in R using Leaflet.
This is my code:
library(leaflet)
library(jsonlite)
geojson <- readLines("https://cartomap.github.io/nl/wgs84/gemeente_2020.geojson", warn = FALSE) %>%
paste(collapse = "\n") %>%
fromJSON(simplifyVector = TRUE)
map <- leaflet() %>%
addTiles() %>%
addGeoJSON(geojson, weight = 1, color = "grey") %>%
setView(5.387740, 52.155499, zoom = 7)
map
Alas it is not working. I don't get an error message, but I don't get a map with municipality borders either. Could somebody please point out to me what I am doing wrong?
The addGeoJSON function is expecting a geojson object but jsolite::fromJSON returns a list. This should work:
library(leaflet)
library(sf)
library(geojsonsf)
url <- "https://cartomap.github.io/nl/wgs84/gemeente_2020.geojson"
sf <- st_read(url)
geojson <- sf_geojson(sf)
map <- leaflet() %>%
addTiles() %>%
addGeoJSON(geojson, weight = 1, color = "grey") %>%
setView(5.387740, 52.155499, zoom = 7)
map
Here is my data :
InitialLat InitialLong NewLat NewLong
62.46972 6.187194 51.4749 -0.221619
48.09750 16.310800 51.4882 -0.302621
I can connect my coordonates in pairs in leaflet with the geosphere library
(according to How Do I connect two coordinates with a line using Leaflet in R)
library(leaflet)
library(geosphere)
mydf <- data.frame(InitialLat = c(62.469722,48.0975), # initial df
InitialLong = c(6.187194, 16.3108),
NewLat = c(51.4749, 51.4882),
NewLong = c(-0.221619, -0.302621))
p1 <- as.matrix(mydf[,c(2,1)]) # it's important to list lng before lat here
p2 <- as.matrix(mydf[,c(4,3)]) # and here
gcIntermediate(p1, p2,
n=100,
addStartEnd=TRUE,
sp=TRUE) %>%
leaflet() %>%
addTiles() %>%
addPolylines()
How can I add markers to ?
I tried this without success :
library(tidyr)
markers <- mydf %>%
select(1, 2)
lines <- gcIntermediate (p1, p2,
n=100,
addStartEnd=TRUE,
sp=TRUE)
leaflet() %>%
addTiles() %>%
addPolylines(lines) %>%
addMarkers(markers, lat =~InitialLat, long =~InitialLong)
You need to specify that your arguments lines and markers are data parameter:
leaflet() %>%
addTiles() %>%
addPolylines(data = lines) %>%
addMarkers(data=markers,lat =~InitialLat, lng =~InitialLong)
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.