R语言:如何使用开放限制绘制shapefiles?

huangapple go评论73阅读模式
英文:

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 &lt;- extent(-76.5, -60.0, -3.0, 3.5)

paises &lt;- c(&quot;BRA&quot;, &quot;COL&quot;, &quot;ECU&quot;, &quot;VEN&quot;)
for(i in 1:length(paises)){
     if(i == 1){
          shps0 &lt;- gadm(paises[i], level=0, path = tempdir(), version=&quot;latest&quot;, resolution=1)
     } else {
          temp &lt;- gadm(paises[i], level=0, path = tempdir(), version=&quot;latest&quot;, resolution=1)
          shps0 &lt;- rbind(temp, shps0)
     }
}
shps0 &lt;- crop(shps0,e)

This yields the following map (without the colored arrows) of the region among Colombia, Brazil, Venezuela Ecuador, and Peru:
R语言:如何使用开放限制绘制shapefiles?

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:

R语言:如何使用开放限制绘制shapefiles?

答案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)

R语言:如何使用开放限制绘制shapefiles?

创建于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)
#&gt; Loading required package: terra
#&gt; terra 1.7.23
library(terra)

shps0_poly &lt;- gadm(c(&quot;BRA&quot;, &quot;COL&quot;, &quot;ECU&quot;, &quot;VEN&quot;), level = 0, path = tempdir())
shps0_poly
#&gt;  class       : SpatVector 
#&gt;  geometry    : polygons 
#&gt;  dimensions  : 4, 2  (geometries, attributes)
#&gt;  extent      : -92.00854, -28.84764, -33.74632, 15.91248  (xmin, xmax, ymin, ymax)
#&gt;  coord. ref. : lon/lat WGS 84 (EPSG:4326) 
#&gt;  names       : GID_0  COUNTRY
#&gt;  type        : &lt;chr&gt;    &lt;chr&gt;
#&gt;  values      :   BRA   Brazil
#&gt;                  COL Colombia
#&gt;                  ECU  Ecuador
as.data.frame(shps0_poly)
#&gt;   GID_0   COUNTRY
#&gt; 1   BRA    Brazil
#&gt; 2   COL  Colombia
#&gt; 3   ECU   Ecuador
#&gt; 4   VEN Venezuela

shps0_lines &lt;- as.lines(shps0_poly) |&gt; 
  crop(ext(-76.5, -60.0, -3.0, 3.5))
shps0_lines
#&gt;  class       : SpatVector 
#&gt;  geometry    : lines 
#&gt;  dimensions  : 4, 2  (geometries, attributes)
#&gt;  extent      : -76.5, -63.37228, -3, 3.5  (xmin, xmax, ymin, ymax)
#&gt;  coord. ref. : lon/lat WGS 84 (EPSG:4326) 
#&gt;  names       : GID_0  COUNTRY
#&gt;  type        : &lt;chr&gt;    &lt;chr&gt;
#&gt;  values      :   BRA   Brazil
#&gt;                  COL Colombia
#&gt;                  ECU  Ecuador
as.data.frame(shps0_lines)
#&gt;   GID_0   COUNTRY
#&gt; 1   BRA    Brazil
#&gt; 2   COL  Colombia
#&gt; 3   ECU   Ecuador
#&gt; 4   VEN Venezuela

plot(shps0_lines)

R语言:如何使用开放限制绘制shapefiles?<!-- -->

<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 &lt;- gadm(c(&quot;BRA&quot;, &quot;COL&quot;, &quot;ECU&quot;, &quot;VEN&quot;), level = 0, path = tempdir())
e &lt;- 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 &lt;- 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

huangapple
  • 本文由 发表于 2023年4月19日 15:05:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76051615.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定