[英]Adding coordinates as points to a map in R
我有一个包含经纬度的目击和物体的数据框:
object = c("yacht", "fishingboat", "whale")
long = c(-123.02676, -123.39763, -123.25103)
lat = c(38.22033, 38.05059, 38.32280)
df = cbind.data.frame(long, lat)
我想在世界地图上绘制这些点。 我使用“rnaturalearth”包创建了地球地图。
library(rnaturalearth)
library(sf)
world <- rnaturalearth::ne_countries(scale = "small", returnclass = "sf")
world %>% st_transform(crs = "+proj=moll") %>%
ggplot() + geom_sf() + theme_minimal()
正如我之前所说,我想在世界地图上绘制坐标。
您需要重新投影这些点,然后您可以使用标准geom_point
和geom_text
。 但是,您的点太近了,无法在世界地图上单独看到它们:
df <- sf::sf_project("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0",
"+proj=moll", df) %>%
as.data.frame() %>%
setNames(c("long", "lat")) %>%
cbind(object = object)
world %>%
st_transform(crs = "+proj=moll") %>%
ggplot() +
geom_sf() +
theme_minimal() +
geom_point(data = df, aes(long, lat)) +
geom_text(data = df, aes(long, lat, label = object),
vjust = c(1, 0, -1), hjust = 1)
如果放大它,它会更清晰:
world %>%
st_transform(crs = "+proj=moll") %>%
ggplot() +
geom_sf() +
theme_minimal() +
geom_point(data = df, aes(long, lat)) +
geom_text(data = df, aes(long, lat, label = object),
vjust = c(0.5, 1, -1), hjust = 1.2) +
coord_sf(ylim = c(4200000, 4700000), xlim = c(-10.75, -10.25) * 10^6)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.