英文:
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
参数。除此之外,对于 sf
和 sp
,除了不同的 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()
英文:
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()
<!-- -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论