使用 geom_sf 将 latitude/longitude 点转换为地图
Convert latitude/longitude points to map with geom_sf
我有一个 latitude/longitude 点的数据集,这些点试图转换为 R 中的简单特征 (sf)。
我的目标是使用从 urbnmapr 库中检索到的多边形在美国地图上绘制这些位置。
如代码所示,使用我们的地理参考绘图会导致显示所有点。
当使用 geom_sf() 绘制这些点时,它们最终位于南达科他州。 latitude/longitude 点似乎没有被转换成正确的坐标参考系,尽管我认为 st_as_sf() 函数的使用是正确的。
需要对此代码进行哪些更正才能在美国地图上正确显示风力涡轮机位置的分布?
# Map the locations of US Wind Turbines
library(urbnmapr)
library(ggplot2)
library(readr)
library(dplyr)
library(sf)
# This file is available from https://eerscmap.usgs.gov/uswtdb/assets/data/uswtdbCSV.zip
turbine <- read_csv("C:\mydir\uswtdb_v3_1_20200717.csv")
# Convert lat/long to a sf
turbine_sf <- turbine %>%
st_as_sf(coords = c("xlong", "ylat"), crs=2163)
# obtain state geometries
states_sf <- get_urbn_map(map = "states", sf = TRUE)
# Remove AK, HI from state and PR and GU from turbines as well
states_sf <- states_sf[!(states_sf$state_abbv %in% c("HI","AK")),]
turbine <- turbine [!(turbine$t_state %in% c('HI','AK','PR','GU')),]
# simple plot shows all locations
ggplot(turbine, aes(x=xlong, y=ylat)) + geom_point()
#plot locations over map
ggplot() +
geom_sf(data = turbine_sf) +
geom_sf(data = states_sf, fill = NA, color = "black", size = 0.15, alpha = 0) +
coord_sf(datum = st_crs(2163)) +
labs(fill = "",
title = "",
caption='') +
theme_bw()
通过执行 st_as_sf(coords = c("xlong", "ylat"), crs=2163)
你是说你的 turbine
table 的 原始 经纬度基于 2163 的 CRS .我想你想把它们设置为4326,这是WGS84下的经纬度。
设置初始 CRS 后,使用 st_transform()
将您形状的 CRS 转换为新的 CRS,例如turbine_sf <- st_transform(turbine_sf, crs=2163)
您的涡轮机数据集包含以度数表示的“xlong”和“ylat”,即具有 WGS84 基准的地理坐标系(EPSG 代码:4326)。所以,首先,使它成为crs = 4326
,然后使用st_transform(turbine_sf, crs=2163)
与states_sf
建立相同的坐标系。您可以使用以下代码
# Map the locations of US Wind Turbines
library(urbnmapr)
library(ggplot2)
library(readr)
library(dplyr)
library(sf)
# This file is available from https://eerscmap.usgs.gov/uswtdb/assets/data/uswtdbCSV.zip
turbine <- read_csv("uswtdb_v3_1_20200717.csv")
# Convert lat/long to a sf
turbine_sf <- turbine %>%
st_as_sf(coords = c("xlong", "ylat"), crs=4326)
turbine_sf_t <- st_transform(turbine_sf, crs=2163)
# obtain state geometries
states_sf <- get_urbn_map(map = "states", sf = TRUE)
st_crs(states_sf)
# Remove AK, HI from state and PR and GU from turbines as well
states_sf <- states_sf[!(states_sf$state_abbv %in% c("HI","AK")),]
turbine <- turbine [!(turbine$t_state %in% c('HI','AK','PR','GU')),]
# simple plot shows all locations
ggplot(turbine, aes(x=xlong, y=ylat)) + geom_point()
#plot locations over map
ggplot() +
geom_sf(data = turbine_sf_t) +
geom_sf(data = states_sf, fill = NA, color = "black", size = 0.15, alpha = 0) +
coord_sf(datum = st_crs(2163)) +
labs(fill = "",
title = "",
caption='') +
theme_bw()
我有一个 latitude/longitude 点的数据集,这些点试图转换为 R 中的简单特征 (sf)。 我的目标是使用从 urbnmapr 库中检索到的多边形在美国地图上绘制这些位置。
如代码所示,使用我们的地理参考绘图会导致显示所有点。
当使用 geom_sf() 绘制这些点时,它们最终位于南达科他州。 latitude/longitude 点似乎没有被转换成正确的坐标参考系,尽管我认为 st_as_sf() 函数的使用是正确的。
需要对此代码进行哪些更正才能在美国地图上正确显示风力涡轮机位置的分布?
# Map the locations of US Wind Turbines
library(urbnmapr)
library(ggplot2)
library(readr)
library(dplyr)
library(sf)
# This file is available from https://eerscmap.usgs.gov/uswtdb/assets/data/uswtdbCSV.zip
turbine <- read_csv("C:\mydir\uswtdb_v3_1_20200717.csv")
# Convert lat/long to a sf
turbine_sf <- turbine %>%
st_as_sf(coords = c("xlong", "ylat"), crs=2163)
# obtain state geometries
states_sf <- get_urbn_map(map = "states", sf = TRUE)
# Remove AK, HI from state and PR and GU from turbines as well
states_sf <- states_sf[!(states_sf$state_abbv %in% c("HI","AK")),]
turbine <- turbine [!(turbine$t_state %in% c('HI','AK','PR','GU')),]
# simple plot shows all locations
ggplot(turbine, aes(x=xlong, y=ylat)) + geom_point()
#plot locations over map
ggplot() +
geom_sf(data = turbine_sf) +
geom_sf(data = states_sf, fill = NA, color = "black", size = 0.15, alpha = 0) +
coord_sf(datum = st_crs(2163)) +
labs(fill = "",
title = "",
caption='') +
theme_bw()
通过执行 st_as_sf(coords = c("xlong", "ylat"), crs=2163)
你是说你的 turbine
table 的 原始 经纬度基于 2163 的 CRS .我想你想把它们设置为4326,这是WGS84下的经纬度。
设置初始 CRS 后,使用 st_transform()
将您形状的 CRS 转换为新的 CRS,例如turbine_sf <- st_transform(turbine_sf, crs=2163)
您的涡轮机数据集包含以度数表示的“xlong”和“ylat”,即具有 WGS84 基准的地理坐标系(EPSG 代码:4326)。所以,首先,使它成为crs = 4326
,然后使用st_transform(turbine_sf, crs=2163)
与states_sf
建立相同的坐标系。您可以使用以下代码
# Map the locations of US Wind Turbines
library(urbnmapr)
library(ggplot2)
library(readr)
library(dplyr)
library(sf)
# This file is available from https://eerscmap.usgs.gov/uswtdb/assets/data/uswtdbCSV.zip
turbine <- read_csv("uswtdb_v3_1_20200717.csv")
# Convert lat/long to a sf
turbine_sf <- turbine %>%
st_as_sf(coords = c("xlong", "ylat"), crs=4326)
turbine_sf_t <- st_transform(turbine_sf, crs=2163)
# obtain state geometries
states_sf <- get_urbn_map(map = "states", sf = TRUE)
st_crs(states_sf)
# Remove AK, HI from state and PR and GU from turbines as well
states_sf <- states_sf[!(states_sf$state_abbv %in% c("HI","AK")),]
turbine <- turbine [!(turbine$t_state %in% c('HI','AK','PR','GU')),]
# simple plot shows all locations
ggplot(turbine, aes(x=xlong, y=ylat)) + geom_point()
#plot locations over map
ggplot() +
geom_sf(data = turbine_sf_t) +
geom_sf(data = states_sf, fill = NA, color = "black", size = 0.15, alpha = 0) +
coord_sf(datum = st_crs(2163)) +
labs(fill = "",
title = "",
caption='') +
theme_bw()