从 R 中的地图中删除冗余区域(shapefile)
Removing redundant areas from a map in R (shapefile)
我在地图上标出了几个欧洲国家,但有一些我不需要的异常值。我尝试使用类似问题中建议的不同方式将它们从我的空间 df 中删除,但它们不适用于这种情况。你能告诉我你关于删除它们的想法吗?我很感激。形状文件可用 here
编辑:我不仅需要从地图中删除这些区域,还需要从空间数据框中删除这些区域。
library(rgdal)
library(raster)
myCountries <- c("Austria", "Belgium", "Czech Republic", "Denmark", "Estonia", "Finland",
"France", "Germany", "Latvia", "Hungary", "Iceland", "Ireland", "Italy",
"Netherlands", "Norway", "Portugal", "Poland", "Spain", "Sweden", "Switzerland",
"Turkey", "United Kingdom")
countries <- readOGR('ne_110m_admin_0_countries.shp')
eurcountries <- countries[countries$NAME_EN %in% myCountries ,]
eurcountries2<-spTransform(eurcountries, CRS("+proj=longlat +datum=NAD83"))
plot(eurcountries2)
我发现 SF 包比使用 SP 包更好,因为它与 ggplot2 配合得很好。然后限制 canvas 很简单,并增加了为国家着色的能力。
library(rgdal)
library(ggplot2)
myCountries <- c("Austria", "Belgium", "Czech Republic", "Denmark", "Estonia", "Finland",
"France", "Germany", "Latvia", "Hungary", "Iceland", "Ireland", "Italy",
"Netherlands", "Norway", "Portugal", "Poland", "Spain", "Sweden", "Switzerland",
"Turkey", "United Kingdom")
countries <- readOGR("C:/R/projects/ne_110m_admin_0_countries/ne_110m_admin_0_countries.shp")
eurcountries <- countries[countries$NAME_EN %in% myCountries, ]
eurcountries3 <- sf::st_as_sf(eurcountries)
ggplot(eurcountries3) +
geom_sf(aes(fill = ADMIN)) +
lims(x = c(50,-40), y = c(30, 74)) +
guides(fill = "none") +
theme_void()
以下是使用 terra
(替代 raster
)的方法:
myCountries <- c("Austria", "Belgium", "Czech Republic", "Denmark", "Estonia", "Finland",
"France", "Germany", "Latvia", "Hungary", "Iceland", "Ireland", "Italy",
"Netherlands", "Norway", "Portugal", "Poland", "Spain", "Sweden", "Switzerland",
"Turkey", "United Kingdom")
library(terra)
countries <- vect('ne_110m_admin_0_countries.shp')
eur <- countries[countries$NAME_EN %in% myCountries ,]
e <- ext(c(-28, 48, 35, 76)))
x <- crop(eur, e)
plot(x, "NAME_EN")
您可以交互地找到您需要的裁剪范围
plot(eur)
e <- draw()
# now click on the map twice
或者交互子集,像这样:
d <- disagg(eur)
plot(d)
s <- sel(d) # now draw a bounding box on the plot
a <- aggregate(s, "NAME_EN")
plot(a, "NAME_EN")
并且您可以将 SpatVector 对象强制转换为 sp
或 sf
类型,如下所示:
sf <- sf::st_as_sf(x)
library(raster)
sp <- as(x, "Spatial")
反之亦然:
y <- vect(sf)
我在地图上标出了几个欧洲国家,但有一些我不需要的异常值。我尝试使用类似问题中建议的不同方式将它们从我的空间 df 中删除,但它们不适用于这种情况。你能告诉我你关于删除它们的想法吗?我很感激。形状文件可用 here
编辑:我不仅需要从地图中删除这些区域,还需要从空间数据框中删除这些区域。
library(rgdal)
library(raster)
myCountries <- c("Austria", "Belgium", "Czech Republic", "Denmark", "Estonia", "Finland",
"France", "Germany", "Latvia", "Hungary", "Iceland", "Ireland", "Italy",
"Netherlands", "Norway", "Portugal", "Poland", "Spain", "Sweden", "Switzerland",
"Turkey", "United Kingdom")
countries <- readOGR('ne_110m_admin_0_countries.shp')
eurcountries <- countries[countries$NAME_EN %in% myCountries ,]
eurcountries2<-spTransform(eurcountries, CRS("+proj=longlat +datum=NAD83"))
plot(eurcountries2)
我发现 SF 包比使用 SP 包更好,因为它与 ggplot2 配合得很好。然后限制 canvas 很简单,并增加了为国家着色的能力。
library(rgdal)
library(ggplot2)
myCountries <- c("Austria", "Belgium", "Czech Republic", "Denmark", "Estonia", "Finland",
"France", "Germany", "Latvia", "Hungary", "Iceland", "Ireland", "Italy",
"Netherlands", "Norway", "Portugal", "Poland", "Spain", "Sweden", "Switzerland",
"Turkey", "United Kingdom")
countries <- readOGR("C:/R/projects/ne_110m_admin_0_countries/ne_110m_admin_0_countries.shp")
eurcountries <- countries[countries$NAME_EN %in% myCountries, ]
eurcountries3 <- sf::st_as_sf(eurcountries)
ggplot(eurcountries3) +
geom_sf(aes(fill = ADMIN)) +
lims(x = c(50,-40), y = c(30, 74)) +
guides(fill = "none") +
theme_void()
以下是使用 terra
(替代 raster
)的方法:
myCountries <- c("Austria", "Belgium", "Czech Republic", "Denmark", "Estonia", "Finland",
"France", "Germany", "Latvia", "Hungary", "Iceland", "Ireland", "Italy",
"Netherlands", "Norway", "Portugal", "Poland", "Spain", "Sweden", "Switzerland",
"Turkey", "United Kingdom")
library(terra)
countries <- vect('ne_110m_admin_0_countries.shp')
eur <- countries[countries$NAME_EN %in% myCountries ,]
e <- ext(c(-28, 48, 35, 76)))
x <- crop(eur, e)
plot(x, "NAME_EN")
您可以交互地找到您需要的裁剪范围
plot(eur)
e <- draw()
# now click on the map twice
或者交互子集,像这样:
d <- disagg(eur)
plot(d)
s <- sel(d) # now draw a bounding box on the plot
a <- aggregate(s, "NAME_EN")
plot(a, "NAME_EN")
并且您可以将 SpatVector 对象强制转换为 sp
或 sf
类型,如下所示:
sf <- sf::st_as_sf(x)
library(raster)
sp <- as(x, "Spatial")
反之亦然:
y <- vect(sf)