如何在使用rnaturalearth包时显示一个国家地图的州?

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

How to show states of a country map while using rnaturalearth package?

问题

如果使用sp包,sp::plot(ne_states(country = "spain"))会显示一个西班牙地图,其中包含该国的各个州。然而,如果不使用sp包,例如:

ne_countries(scale = 10, returnclass = 'sf') %>%
  filter(geounit == "Spain") %>%
  ggplot() +
  geom_sf() +
  theme_void()

如何获得相同的结果呢?如果我执行以下代码:

ne_states(scale = 10, returnclass = 'sf') %>%
  filter(geounit == "Spain") %>%
  ggplot() +
  geom_sf() +
  theme_void()

将会抛出以下错误:

Error in ne_states(scale = 10, returnclass = "sf") : 
  unused argument (scale = 10)
英文:

If one uses the sp package, sp::plot(ne_states(country = "spain")) shows a Spain map in which states of the country are rendered. However, if the sp package is not used, say,

ne_countries(scale = 10, returnclass = 'sf') %>%
  filter(geounit == "Spain") %>%
  ggplot() +
  geom_sf() +
  theme_void()

how can one get the same result? If I do

ne_states(scale = 10, returnclass = 'sf') %>%
  filter(geounit == "Spain") %>%
  ggplot() +
  geom_sf() +
  theme_void()

the following error is thrown:

Error in ne_states(scale = 10, returnclass = "sf") : 
  unused argument (scale = 10)

答案1

得分: 1

问题在于 ne_states 没有 scale 参数。除此之外,对于 sfsp,除了不同的 returnclass,基本上是一样的:

library(ggplot2)
library(rnaturalearth)

# 对于 {rnaturalearth},将不再支持 Spatial 对象 (`sp`),并且将在未来版本中删除该包的支持。请改用 {rnaturalearth} 中的 `sf` 对象。例如:`ne_download(returnclass = 'sf')`

ne_states(country = "Spain", returnclass = "sf") |>
  ggplot() +
  geom_sf() +
  theme_void()

如何在使用rnaturalearth包时显示一个国家地图的州?

英文:

The issue is that ne_states has no scale argument. Besides that it's basically the same for sf as for sp except for the different returnclass:

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.2.3
library(rnaturalearth)
#> Warning: package 'rnaturalearth' was built under R version 4.2.3
#> Support for Spatial objects (`sp`) will be deprecated in {rnaturalearth} and will be removed in a future release of the package. Please use `sf` objects with {rnaturalearth}. For example: `ne_download(returnclass = 'sf')`

ne_states(country = "Spain", returnclass = "sf") |> 
  ggplot() +
  geom_sf() +
  theme_void()

如何在使用rnaturalearth包时显示一个国家地图的州?<!-- -->

huangapple
  • 本文由 发表于 2023年7月20日 15:01:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76727397.html
匿名

发表评论

匿名网友

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

确定