如何在使用geom_sf和ggplot2制作的地图上对两个边界点之间的线进行着色?

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

How to color the line different between 2 points on borders in maps made with geom_sf and ggplot2

问题

我试图制作一张地图,在这张地图上我想用不同的颜色来标记两点之间的线。这听起来是一个非常基本的任务,但我在网上找不到答案,所以我不得不在这里发表一个问题。让我用一个基本的具体例子来展示我想要实现的目标;

library(rnaturalearth)
library(rnaturalearthdata)

turkey_provinces <- ne_states(country = "turkey", returnclass = "sf") # 土耳其城市的数据
ggplot(turkey_provinces) +
  geom_sf() +
  ggthemes::theme_map()

这段代码产生了这个图像

如何在使用geom_sf和ggplot2制作的地图上对两个边界点之间的线进行着色?

我想要做的是,在西部边界上选择两个随机点,并用不同的颜色标记它们之间的线。我尝试了一种方法,我制作了一条线,并试图找到与实际边界的交点。但这不成功,因为实际边界的形状更为复杂,使用 st_intersect 寻找与直线的交点没有奏效。所以,如果你有任何建议,我会非常感激。先谢谢你的关注。

英文:

I am trying to make a map, where I would like to color the line between 2 points differently. This sounds a very basic task but I could not find an answer on the web, so I had to post a question here. Let me show what I am trying to achieve with a basic concrete example;

library(sf)
library(rnaturalearth)
library(rnaturalearthdata)



turkey_provinces <- ne_states(country = "turkey", returnclass = "sf") # data for turkish cities
ggplot(turkey_provinces) +
  geom_sf() +
  ggthemes::theme_map()

which produces this

如何在使用geom_sf和ggplot2制作的地图上对两个边界点之间的线进行着色?

What I want to do is, choosing 2 random points on the western border and color the line between them differently. I have tried an approach, where I made a line string and tried to find the intersection with the real border. It was not successful since the shape of the real border is more intricate and finding an intersection with a straight line using st_intersect did not work out. So, if you have any suggestion, I am more than grateful for that. Thank you for your attention beforehand.

答案1

得分: 0

我怀疑有更加优雅的解决方案,但您可以定义一个垂直于您想要的经度边界的矩形,并且水平地使其与西边界重叠但不与东边界重叠。然后,取矩形与边界的交集(使用 st_union 并进行多线字符串类型的转换来获取外边界)。

library(sf)
library(ggplot2)

nc <- st_read(system.file("shape/nc.shp", package="sf"))

rect <- list(matrix(
    c(-85, 35.5, 
      -80, 35.5, 
      -80, 36.5, 
      -85, 36.5, 
      -85, 35.5),
    byrow = TRUE, 
    ncol = 2
  )) %>%
  st_polygon() %>%
  st_sfc(crs = st_crs(nc))

border_line <- st_intersection(
  rect, 
  st_cast(st_union(nc), "MULTILINESTRING")
)

ggplot(nc) +
  geom_sf() +
  geom_sf(data = border_line, color = "red")

如何在使用geom_sf和ggplot2制作的地图上对两个边界点之间的线进行着色?


<details>
<summary>英文:</summary>

I suspect there’s a more elegant solution, but you could define a rectangle bounded vertically by the longitudes you want, and horizontally so that it overlaps the western but not eastern border. Then take the intersection of the rectangle with the border. (Get the outer border using `st_union` and casting to MULTILINESTRING.)
```r
library(sf)
library(ggplot2)

nc &lt;- st_read(system.file(&quot;shape/nc.shp&quot;, package=&quot;sf&quot;))

rect &lt;- list(matrix(
    c(-85, 35.5, 
      -80, 35.5, 
      -80, 36.5, 
      -85, 36.5, 
      -85, 35.5),
    byrow = TRUE, 
    ncol = 2
  )) |&gt;
  st_polygon() |&gt;
  st_sfc(crs = st_crs(nc))

border_line &lt;- st_intersection(
  rect, 
  st_cast(st_union(nc), &quot;MULTILINESTRING&quot;)
)

ggplot(nc) +
  geom_sf() +
  geom_sf(data = border_line, color = &quot;red&quot;)

如何在使用geom_sf和ggplot2制作的地图上对两个边界点之间的线进行着色?

huangapple
  • 本文由 发表于 2023年6月9日 08:41:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76436518.html
匿名

发表评论

匿名网友

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

确定