我正在拼命尝试在
ggplot
中添加具有法国背景的库存数据(可在线获取,如下面的可重现示例所示),以便我可以可视化没有我的物种记录的区域。
但是,我想用六边形而不是点来做。因此,我使用了
stat_summary_hex
,这阻止了我使用 geom_polygon
将背景添加到我的 ggplot
。然后,我尝试叠加两个 ggplots,但没有成功。
以Abies alba
为例## Extract data
temp <- tempfile()
download.file("https://inventaire-forestier.ign.fr/dataifn/data/export_dataifn_2005_2022.zip",temp)
couvert <- read.csv(unz(temp, "COUVERT.csv"), sep=";")
placette <- read.csv(unz(temp, "PLACETTE.csv"), sep=";")
espar <- read.csv(unz(temp,"espar-cdref13.csv"), sep=";")
unlink(temp)
## e.g. for Abies alba
ID <- espar$X...espar[espar$lib_cdref=="Abies alba"]
couvert_sp <- couvert[couvert$ESPAR_C == ID ,]
df <- merge(placette[ , c("IDP", "XL", "YL")], couvert_sp, by="IDP", all.y=T)
# coordinates conversion (required)
crs193 <- "+proj=lcc +lat_1=49 +lat_2=44 +lat_0=46.5 +lon_0=3 +x_0=700000 +y_0=6600000 +ellps=GRS80 +units=m +no_defs"
wgs84 <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
xyl93 <- df[, c("XL", "YL", "IDP")]
require(sp)
coordinates(xyl93) <- ~ XL + YL
proj4string(xyl93) <- CRS(crs193)
xyl <- as.data.frame(spTransform(xyl93, wgs84))
require(dplyr)
df2 <- merge(xyl, df, by="IDP", all.x=T, all.y=T) %>% distinct(IDP, .keep_all = TRUE)
# graphic
require(ggplot2)
AB <- map_data("world") %>% filter(region %in% "France")
# background
bck <- ggplot() +
geom_polygon(data = AB, aes(x=long, y = lat, group = group), fill="#ECECEC") +
theme_void() + coord_map()
bck
# my hex points
p <- ggplot(df2, aes(x=coords.x1, y=coords.x2, z=TCA)) +
stat_summary_hex(fun = mean, bins = 80) +
scale_fill_gradientn(colours = paletteer_c("ggthemes::Classic Green", 30)) +
theme_void() + coord_map()
p
p + bck
有什么建议吗?预先感谢!