简体   繁体   中英

plot a map using lat and lng coordinates in R

My data set has 4 columns which are station_name, station_lat, station_lng and count.This is the example of my data set.

stations <- data.frame(station = c("StreeterDr", "MichiganAve", "WellsSt"), lat = c(41.89228, 41.90096, 41.91213), lng = c(-87.61204, -87.62378, -87.63466), count = c(2300, 5678, 3452))

I want to plot these coordinates on maps. Referring previous post on this topic I tried this code. But it's not working.

install.packages(c("leaflet", "sp")) 
library(leaflet)
library(leaflet)

lon <- stations$start_lng
lat <- stations$start_lat
df <- as.data.frame(cbind(lon,lat))

coordinates(df) <- ~lon+lat
leaflet(df) %>% addMarkers() %>% addTiles()

Stating to R which columns are xy is not sufficient. You need to convert your data.table to an sf object as well and state which geographic coordinate system you use.

#object with your data


#state which columns are lon and lat
coordinates(dt) <- ~lng+lat

#convert to sf object
dt <- st_as_sf(dt)

#set crs (WSG1984 seems to be used here)
st_crs(dt) <- 4326

#create leaflet
leaflet(dt) %>% addMarkers() %>% addTiles()

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM