rnaturalearth 和 sf object

问题描述 投票:0回答:1

我想用 rnaturalearth 构建一张地图。

在该地图的顶部,我想要代表三个城市的三个点,它们用线连接。线条颜色对应于 F 的值(表示这些国家的距离有多近),以及地图的比例尺颜色(如图所示)

我有地图代码:


target <- c("Armenia", "Azerbaijan", "Turkey", "Iran", "Georgia", "Russia", "Syria", "Iraq")


countries<- ne_countries(scale = 10,returnclass = 'sf') %>%
  filter(name %in% target)

coordinates <- data.frame(
  region = c("A", "B", "C"),
  longitude = c(39.80949367, 39.65485163, 38.47802923),
  latitude = c(46.7793274, 36.99614676, 43.3863868)
)

map <- ggplot(data=countries)+geom_sf(fill = "lightgoldenrodyellow") +
  xlim(36,49) +
  ylim(36.5,42) + theme_void() +
  theme(panel.background = element_rect(fill = "lightblue2"))

print(map) 

如何绘制城市点和彩色线?

谢谢!

r dictionary ggplot2 geography
1个回答
0
投票

{ggplot} 与您相互添加的

layers
一起使用。这由
+
运算符表示。

您已经使用代码生成了底层地图。为此,您使用了来自 {rnaturalearth} 的地图数据,并使用 {sf} 来处理地理空间方面。例如,这对地图的投影有一点帮助(或者您可以利用 {sf} 包的其他功能。

library(dplyr)    # for data crunching - you use the pipe %>% or now |>
library(ggplot2)  # for plotting
library(sf)       # handling of geospatial data
library(rnaturalearth) # underlying geo map data

# ... your code here ....
map     # calling your base map

这将提供您的底图:

结合 {ggplot} 和 {sf} ...负责地理空间放置

结合 {ggplot2} 和 {sf} 有一个方面。后者确保“普通”数据框列被处理为坐标(而不是 sf 对象)。
因此,您可以在底图上“添加”点图层。

注意:我还添加了一个文本标签来显示另一个问题。

map + 
# now let's add a point geom on top of the base map, given by coordinates
  geom_point( data = coordinates
            , mapping = aes(x = longitude, y = latitude)
    # ----- make it big(ger) to see it better
           , size = 5
    ) + 

# -------- we also add a text label to show where we are
geom_text( data = coordinates
    # ----- we displace the label by adding to the longitude
    # ----- note this is for demo purposes, be mindful about 0.5 is decimal (geospatial) degree in this case ... dependent on your zoom/area this jump can differ significantly (but that is another discussion).
         , mapping = aes(x = longitude + 0.5, y = latitude, label = region)
    # ----- arbitrary setting of size to make it "big" and color
    # ----- note: you may use this to have a labelled point by removing the offset
         , size = 8, color = "red"
    )

所以我们现在在底图上叠加了(一个)点。不幸的是,您选择的区域仅显示 B。

我们现在可以使用相同的副作用来绘制城市之间的联系。 {ggplot} 允许您绘制由开始和结束位置定义的

line
段。 让我们创建一个
links
数据框:

links <- coordinates |> 
#---------- create end points, i.e. lon2, lat2
   mutate(longitude2 = lead(longitude, default = first(longitude))
         , latitude2 = lead(latitude, default = first(latitude))
#---------- we also add a label for the links and value F
         , name = c("AB", "BC", "CA")
         , F = c(3,6,9)
    ) 
 links
  region longitude latitude longitude2 latitude2 name F
1      A  39.80949 46.77933   39.65485  36.99615   AB 3
2      B  39.65485 36.99615   38.47803  43.38639   BC 6
3      C  38.47803 43.38639   39.80949  46.77933   CA 9

如果你绘制这个......不幸的是没有显示任何线条。 {ggplot} 会向您发出有关缺失(点、文本和线段)的警告。 我们之前已经看到过这一点......但很明显,您选择的区域不允许显示所有元素(因为从地理空间角度来看它们位于您的查看区域之外)

map + 
    #---- add city/ies from above
    geom_point(data = coordinates, aes(x = longitude, y = latitude), size = 5) + 
    geom_text(data = coordinates, aes(x = longitude + 0.5, y = latitude, label = region), size = 8, color = "red") +
    
    #---- add connection links we just have created
    geom_segment(data = links
               , mapping = aes(x = longitude, xend = longitude2  #note end point
                             , y = latitude,  yend = latitude2
    #-------------------- just adding a color to highlight the link value
                             , color = F)
    )

要解决“缺失”点/链接问题,请检查您的

limits
。 您选择了消除 A 和 C 的纬度值。
对于此更改
ylim
相应:

map2 <- ggplot(data=countries)+geom_sf(fill = "lightgoldenrodyellow") +
         xlim(36,49) +
    # ---------------- set upper latitude to cover all points, i.e. 47
         ylim(36.5,47) + 
         theme_void() + theme(panel.background = element_rect(fill = "lightblue2"))

map2 + 
    #---- add city/ies ---------------------
    geom_point(data = coordinates, aes(x = longitude, y = latitude), size = 5) + 
    geom_text(data = coordinates, aes(x = longitude + 0.5, y = latitude, label = region), size = 8, color = "red") +
    #---- add connection links -------------
    geom_segment(data = links, mapping = aes(x = longitude, xend = longitude2, y = latitude, yend = latitude2, color = F))

© www.soinside.com 2019 - 2024. All rights reserved.