我正在尝试在 R 中创建一个范围图,该图应该显示世界上特定树种(栎树)的出现情况。为此,我使用 BIEN 数据库和函数
BIEN_ranges_species()
。我成功地创建了世界地图,但无法看到显示树木出现位置的标记。显示事件的图层可能有问题。
以下代码基于BIEN推荐的示例:
library(BIEN)
library(maps)
library(sf)
species_vector <- c("Quercus_cerris")
BIEN_ranges_species(species_vector)
BIEN_ranges_species(species_vector, match_names_only = TRUE)
temp_dir <- file.path("C:/Users/Nele/Documents/R_Karten")
BIEN_ranges_species(species = species_vector,
directory = temp_dir)
BIEN_ranges_species("Quercus_cerris")
BIEN_ranges_species("Quercus_cerris",
directory = temp_dir)
Quercus_test <- st_read(dsn = temp_dir,
layer = "Quercus_cerris")
plot(Quercus_test[1])
map('world', fill = TRUE, col = "grey")
plot(Quercus_test[1],
col = "red",
add = TRUE)
另外,当我只绘制栎树的数据时,使用这部分代码:
plot(Quercus_test[1],
col = "red",
add = TRUE)
也没有可见数据。
正如@Chris 指出的,你的数据就在那里,只是这些特征在“世界”规模上不可见。如果您遇到此问题,以下工作流程可用于排除故障/探索数据:
library(BIEN)
library(maps)
library(sf)
library(ggplot2)
# Get Quercus_test metadata, note the bounding box values
Quercus_test
# Simple feature collection with 1 feature and 2 fields
# Geometry type: MULTIPOLYGON
# Dimension: XY
# Bounding box: xmin: -123.4466 ymin: 38.80048 xmax: -72.64731 ymax: 49.07663
# Geodetic CRS: WGS 84
# species gid geometry
# 1 Quercus_cerris 75854 MULTIPOLYGON (((-77.09406 3...
# Create world map sf, change CRS to match Quercus_test CRS
world <- map("world", fill = TRUE, col = "grey", wrap = c(-180, 180)) %>%
st_as_sf() %>%
st_transform(st_crs(Quercus_test))
# Using coords_sf(), 'zoom in' to Quercus_test sf with bounding box values as a basis
# linewidth value exaggerated for illustrative purposes
ggplot() +
geom_sf(data = world, colour = "grey75") +
geom_sf(data = Quercus_test, fill = "firebrick", colour = "firebrick", linewidth = 2) +
coord_sf(xlim = c(-124, -72),
ylim = c(38, 50)) +
theme(panel.background = element_blank())