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.
Related
I am trying to make an interactive graph of the United States using leaflet and the us.cities package in R. I was able to create the map and label put markers on all the US capitals, but I am struggling to label those capitals on the map itself.
The goal is to be able to click on a marker and have the state capital and population appear on the graph. The trouble is steaming from my last section of code that starts with the #. If anyone has any idea why that code will not run, besides the obvious fact that I put a # in front, I am open to any and all guidance!
Thank you!
library(leaflet)
library(dplyr)
library(maps)
leaflet(us.cities) %>% addTiles() %>% addCircleMarkers(data = us.cities)
us.cities <- us.cities %>% filter(capital == 2) %>% mutate(state_info = paste(name, capital == 2, pop))
us.cities$state_info
#leaflet(us.cities) %>% addTiles() %>% addCircleMarkers(data = us.cities, lat = ~lat, lng = ~long, state_info = ~state_info)
I don't know much about the leaflet package, but for the purposes of providing a quick answer...
leaflet(us.cities) %>%
addTiles() %>%
addCircleMarkers(
data = us.cities,
lat = ~lat,
lng = ~long,
popup = paste(us.cities$country.etc, us.cities$pop)
)
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'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 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.