r - 在 R 中的地图上添加多边形,CRS 问题
问题描述
我正在尝试绘制瑞典地图,然后绘制我在其上创建的假形状。我有一个绘制瑞典自治市的脚本:
library(ggplot2)
library(sf)
library(smoothr)
library(jsonlite)
tmp <- tempfile()
download.file("http://api.thenmap.net/v2/se-7/geo/2020-06-06", destfile = tmp)
mun_name <- fromJSON("http://api.thenmap.net/v2/se-7/data/2020-06-06?language=en&data_props=name|shapeid|is_in") %>%
unnest(is_in) %>%
rename(county = is_in)
mun <- read_sf(tmp) %>%
left_join(mun_name, by = c("id" = "shapeid"))
ggplot(mun) +
geom_sf(color="grey20",fill="grey95",size=.3) +
theme_bw()
然后我有一个脚本,在其中制作多边形st_polygon
:
# make a polygon that is mostly inside sweden
sweden_polygon <-
# create list of matrices and the first point same as last point
list(
matrix(
c(14, 62,
12, 63,
14, 64,
16, 66,
20, 68,
20, 66,
19, 65,
18, 63,
14, 62),
ncol=2, byrow=T
)
)
# Create an sf polygon
sweden_polygon <- sf::st_polygon(sweden_polygon)
# smooth the polygon
smooth_sweden_polygon <- smooth(sweden_polygon, method = "chaikin")
我可以用看似相同的坐标分别绘制它们,但是当我将它们绘制在一起时,它不起作用,因为多边形没有与瑞典匹配的 CRS。
# this works:
ggplot() +
geom_sf(data=mun,color="grey20",fill="grey95",size=.3) +
theme_bw()
# this works:
ggplot() +
geom_sf(data=smooth_sweden_polygon) +
theme_bw()
# this don't work:
ggplot() +
geom_sf(data=mun,color="grey20",fill="grey95",size=.3) +
geom_sf(data=smooth_sweden_polygon) +
theme_bw()
我知道st_crs(mun)
瑞典的坐标系是 WGS 84,但我不知道如何将其分配给我的多边形。
解决方案
推荐阅读
- java - java中的数组不是连续的吗?
- javascript - 使用带有反应和状态的 setTimeout 更新对象数组
- swift - 本地化字符串未从 M1 芯片机中的自定义包中加载
- kubernetes - 即使分配了 LoadBalancer IP,也无法访问 Longhorn UI [metalLB]
- asp.net-core - Blazor WebAssembly - 如何添加不需要重新编译或重建项目的新 *.razor 页面
- react-native - 无法解析 phonepe.intentsdk.android.release
- c# - 从 DataGridView 中提取日期到 DateTimePicker
- express - 使用 API 网关设置 swagger-ui-express
- javascript - 智能感知不显示对象和功能链接
- java - intellij 找不到 oracle 驱动程序