2

这是我的程序:

library(sp)
library(RColorBrewer)

#get spatial data for Spain on region level
con <- url("http://gadm.org/data/rda/ESP_adm1.RData")
print(load(con))
close(con)
# plot Spain with colors
col = c("blue","blue","blue","blue","blue","blue","blue","blue","blue",
        "blue","blue","blue","blue","blue","blue","blue","red","red")
spplot(gadm, "NAME_1", col.regions=col, main="Regiones de España",
       colorkey=F, lwd=.4, col="black")

我跑步时得到的是我正在寻找的东西,但我想改变西南部的岛屿(加那利群岛),使其更接近西班牙国家。我不知道该怎么做才能改变岛屿的坐标。我只知道他们在这里:

gadm[14,]

例如,我尝试在此处进行更改:

gadm[14,]@polygons[[1]]@Polygons[[1]]@coords

但我收到消息:no method for coercing this S4 class to a vector

4

1 回答 1

0

也可以试试这个sf包 - CRAN 链接在这里

GADM 数据 URL 现在似乎已弃用,但可以从http://gadm.org/手动下载包含西班牙 SpatialPolygonsDataFrame 的 RDS 文件。

library(sf)
spain <- readRDS(file = "ESP_adm1.rds") # downloaded from http://gadm.org/

# convert from sp to sf object
spain_sf <- st_as_sf(spain)

# Change the coordinates of Canary Islands - the 14th geometry of spain_sf,
# itself with 20 polygons, check str(st_geometry(spain_sf)[[14]])
# Will shift them closer to mainland Spain.
# The added values are degrees because the CRS is unprojected
# (e.g. add 10 to longitudes and 6 to latitudes)
for (i in 1:length(st_geometry(spain_sf)[[14]])) {
  st_geometry(spain_sf)[[14]][[i]][[1]][, 1] <- st_geometry(spain_sf)[[14]][[i]][[1]][, 1] + 10
  st_geometry(spain_sf)[[14]][[i]][[1]][, 2] <- st_geometry(spain_sf)[[14]][[i]][[1]][, 2] + 6
}

# plot to check
par(omi=c(0,0,0,2)) # make some space to the right for the legend
plot(spain_sf["NAME_1"], 
     axes = TRUE, 
     graticule = st_crs(spain_sf))
# dev.off() # resets par()

在此处输入图像描述

于 2018-03-12T22:26:41.673 回答