英文:
R language: how to plot shapefiles with open limits?
问题
以下是您提供的代码部分的翻译:
一个虚拟问题,我在尝试自己修复并谷歌搜索了一段时间后提出的。我需要绘制几个地图,其中包括添加国家的边界。到目前为止,我已经使用了以下脚本:
rm(list = ls(all.names = TRUE))
library(raster)
library(geodata)
e <- extent(-76.5, -60.0, -3.0, 3.5)
paises <- c("BRA", "COL", "ECU", "VEN")
for(i in 1:length(paises)){
if(i == 1){
shps0 <- gadm(paises[i], level=0, path = tempdir(), version="latest", resolution=1)
} else {
temp <- gadm(paises[i], level=0, path = tempdir(), version="latest", resolution=1)
shps0 <- rbind(temp, shps0)
}
}
shps0 <- crop(shps0,e)
请注意,我只翻译了代码部分,没有包括您的问题或其他内容。
英文:
A dummy question I'm asking after trying to fix it by myself and googleing for a while. I need to plot several maps, which involve adding countries' limits. So far, I have used the following script:
rm(list = ls(all.names = TRUE))
library(raster)
library(geodata)
e <- extent(-76.5, -60.0, -3.0, 3.5)
paises <- c("BRA", "COL", "ECU", "VEN")
for(i in 1:length(paises)){
if(i == 1){
shps0 <- gadm(paises[i], level=0, path = tempdir(), version="latest", resolution=1)
} else {
temp <- gadm(paises[i], level=0, path = tempdir(), version="latest", resolution=1)
shps0 <- rbind(temp, shps0)
}
}
shps0 <- crop(shps0,e)
This yields the following map (without the colored arrows) of the region among Colombia, Brazil, Venezuela Ecuador, and Peru:
However, I would like to remove the artificial limits (black lines indicated by the red arrows) from the plot and display a continuous representation of the data, including the portions where the countries farther "continue" (indicated by the green arrows), something like this:
答案1
得分: 1
mask()
函数将gadm()
返回的SpatVector
转换为栅格。如果你只需将多边形转换为线段,就可以保留矢量形状。使用terra
,加载了geodata
后,可能如下所示:
library(geodata)
library(terra)
shps0_poly <- gadm(c("BRA", "COL", "ECU", "VEN"), level = 0, path = tempdir())
as.data.frame(shps0_poly)
shps0_lines <- as.lines(shps0_poly) |>
crop(ext(-76.5, -60.0, -3.0, 3.5))
as.data.frame(shps0_lines)
plot(shps0_lines)
创建于2023年04月19日,使用reprex v2.0.2
英文:
For just plotting and for this particular case it probably makes no difference, but mask()
transforms SpatVector
returned by gadm()
to raster. Though you can keep vector shapes if you just transform polygons to lines first. With terra
, which is loaded by geodata
anyway, it might look something like this:
library(geodata)
#> Loading required package: terra
#> terra 1.7.23
library(terra)
shps0_poly <- gadm(c("BRA", "COL", "ECU", "VEN"), level = 0, path = tempdir())
shps0_poly
#> class : SpatVector
#> geometry : polygons
#> dimensions : 4, 2 (geometries, attributes)
#> extent : -92.00854, -28.84764, -33.74632, 15.91248 (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326)
#> names : GID_0 COUNTRY
#> type : <chr> <chr>
#> values : BRA Brazil
#> COL Colombia
#> ECU Ecuador
as.data.frame(shps0_poly)
#> GID_0 COUNTRY
#> 1 BRA Brazil
#> 2 COL Colombia
#> 3 ECU Ecuador
#> 4 VEN Venezuela
shps0_lines <- as.lines(shps0_poly) |>
crop(ext(-76.5, -60.0, -3.0, 3.5))
shps0_lines
#> class : SpatVector
#> geometry : lines
#> dimensions : 4, 2 (geometries, attributes)
#> extent : -76.5, -63.37228, -3, 3.5 (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326)
#> names : GID_0 COUNTRY
#> type : <chr> <chr>
#> values : BRA Brazil
#> COL Colombia
#> ECU Ecuador
as.data.frame(shps0_lines)
#> GID_0 COUNTRY
#> 1 BRA Brazil
#> 2 COL Colombia
#> 3 ECU Ecuador
#> 4 VEN Venezuela
plot(shps0_lines)
<!-- -->
<sup>Created on 2023-04-19 with reprex v2.0.2</sup>
答案2
得分: 0
你没有使用当前版本的 terra。所以首先要做的是更新该包。你遇到的问题是绘图的坐标轴跟画布大小而不是地图大小一致。在当前版本的 "terra" 中不会出现这种情况。
library(geodata)
library(terra)
pols <- gadm(c("BRA", "COL", "ECU", "VEN"), level = 0, path = tempdir())
e <- ext(-76.5, -60.0, -3.0, 3.5)
如果你只想将这些多边形添加为现有绘图(地图)的线条,比如下面我创建的 x
,你可以这样做:
x <- rast(e, vals=1)
plot(x)
lines(pols)
如果你想首先绘制线条,可以这样做:
plot(e, border=NA)
lines(pols)
你也可以这样做:
plot(pols, xlim=c(-76.5, -60.0), ylim=c(-3.0, 3.5), axes=FALSE)
plot(pols, ext=c(-76.5, -60.0, -3.0, 3.5), axes=FALSE)
也就是说,你不需要裁剪这些多边形。如果你需要,@margusl 提到的使用 as.lines(pols)
的解决方案是可以的,但通常不是必要的。
英文:
You are not using the current version of terra. So the first thing to do is to update that package. The issue you are having that the axes of the plot follow the canvas size, not the map size. In the current version of "terra" that is not the case.
library(geodata)
library(terra)
pols <- gadm(c("BRA", "COL", "ECU", "VEN"), level = 0, path = tempdir())
e <- ext(-76.5, -60.0, -3.0, 3.5)
If you just wanted to add these polygons as lines on top of an existing plot (map), say or raster x
that I create below, you can do
x <- rast(e, vals=1)
plot(x)
lines(pols)
If you want to start by plotting lines you could do
plot(e, border=NA)
lines(pols)
You can also do
plot(pols, xlim=c(-76.5, -60.0), ylim=c(-3.0, 3.5), axes=FALSE)
plot(pols, ext=c(-76.5, -60.0, -3.0, 3.5), axes=FALSE)
That is, you do not need to crop the polygons. If you do, @margusl solution to use as.lines(pols)
is fine, but it should not be necessary.
答案3
得分: -2
问题已解决:我必须使用“mask”而不是“crop”...无论如何谢谢
英文:
Well, problem fixed: I must use mask
instead of crop
... thanks anyway
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论