如何根据自己的空间位置select取一个值?

How to select a value according to their spatial position?

我想在输出地图上每个国家的中心显示国家名称。这可能直接来自 word 变量吗?

library(sp)

names(world)

saudi = world[world$name_long == "Saudi Arabia", ]
plot(st_geometry(saudi), expandBB = c(0, 0.2, 0.1, 1), col = "gray", lwd = 2)
plot(world[0], add = TRUE)

我就喜欢这个案例:

library(tidyverse)

data <-
  map_data("world") %>%
  as_tibble() %>%
  filter(region %in% c("United Arab Emirates", "Saudi Arabia"))
data
#> # A tibble: 619 x 6
#>     long   lat group order region               subregion   
#>    <dbl> <dbl> <dbl> <int> <chr>                <chr>       
#>  1  53.9  24.2    11   959 United Arab Emirates Abu al Abyad
#>  2  53.9  24.1    11   960 United Arab Emirates Abu al Abyad
#>  3  53.8  24.2    11   961 United Arab Emirates Abu al Abyad
#>  4  53.8  24.1    11   962 United Arab Emirates Abu al Abyad
#>  5  53.7  24.1    11   963 United Arab Emirates Abu al Abyad
#>  6  53.6  24.2    11   964 United Arab Emirates Abu al Abyad
#>  7  53.7  24.2    11   965 United Arab Emirates Abu al Abyad
#>  8  53.8  24.3    11   966 United Arab Emirates Abu al Abyad
#>  9  53.9  24.2    11   967 United Arab Emirates Abu al Abyad
#> 10  53.9  24.2    11   968 United Arab Emirates Abu al Abyad
#> # … with 609 more rows

region_names <-
  data %>%
  group_by(region) %>%
  summarise(long = mean(long), lat = mean(lat))
region_names
#> # A tibble: 2 x 3
#>   region                long   lat
#>   <chr>                <dbl> <dbl>
#> 1 Saudi Arabia          44.9  23.2
#> 2 United Arab Emirates  54.4  24.3

data %>%
  ggplot(aes(long, lat)) +
  geom_polygon(aes(color = region), fill = "white") +
  geom_text(
    data = region_names,
    mapping = aes(label = region)
  )

reprex package (v2.0.1)

于 2021-10-18 创建

另一个版本可以是使用 geom_sf_text

注意:我正在使用 tmap 包来获取 World 数据集

library(sf)
library(tmap)

data(World)

saudi <- World[World$name == "Saudi Arabia", ]
saudiBbox <- st_bbox(saudi)

ggplot(saudi) + 
  geom_sf(data = World, fill = 'white') + 
  geom_sf() +
  coord_sf(
    xlim = c(saudiBbox['xmin'], saudiBbox['xmax']), 
    ylim = c(saudiBbox['ymin'], saudiBbox['ymax'])
  ) + 
  geom_sf_text(aes(label = name), size = 3, family = "sans") +
  theme_void()

给出:

一个非常简单的解决方案,使用 tmap。请在下面找到 reprex,当然,您可以随意调整参数以获得您想要的精确渲染。

Reprex

library(sf)
library(tmap)

data(World)
Saudi <-  World[World$name == "Saudi Arabia", ]

tm_shape(Saudi, bbox = st_as_sfc(st_bbox(c(st_bbox(Saudi)[1:2]*0.8,st_bbox(Saudi)[3:4]*1.2))))+
  tm_fill("grey")+
  tm_borders(lty = "solid", lwd = 4, col = "black")+
  tm_text("name", size = 1.3)+
  tm_shape(World)+
  tm_borders(col = "black")

reprex package (v2.0.1)

于 2021-10-18 创建

如果您真的不想要框架(我还调整了与第一个示例不同的缩放级别以向您展示它是如何工作的):

tm_shape(Saudi, bbox = st_as_sfc(st_bbox(c(st_bbox(Saudi)[1:2]*0.6,st_bbox(Saudi)[3:4]*1.3)))) +
  tm_fill("grey") +
  tm_borders(lty = "solid", lwd = 3, col = "black") +
  tm_text("name", size = 1.3) +
  tm_shape(World) +
  tm_borders(col = "black") +
  tm_layout(frame = FALSE)

reprex package (v2.0.1)

于 2021-10-18 创建

最后一张带有周边国家名称(这里是 iso 代码)的渲染图:

World2 <- World[!(World$iso_a3 == "SAU"), ]
tm_shape(Saudi, bbox = st_as_sfc(st_bbox(c(st_bbox(Saudi)[1:2]*0.6,st_bbox(Saudi)[3:4]*1.3)))) +
  tm_fill("grey") +
  tm_borders(lty = "solid", lwd = 3, col = "black") +
  tm_text("name", size = 1.3) +
  tm_shape(World2) +
  tm_borders(col = "black") +
  tm_text("iso_a3", size = 0.7, size.lowerbound = TRUE, print.tiny = TRUE, remove.overlap = TRUE) +
  tm_layout(frame = FALSE)

reprex package (v2.0.1)

于 2021-10-18 创建