简体   繁体   中英

Adding coordinates as points to a map in R

I have a dataframe of sightings and objects with their latitudes and longitudes:

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)

I want to plot these points on a map of the world. I have created a map of the earth using the "rnaturalearth" package.

library(rnaturalearth)
library(sf)

world <- rnaturalearth::ne_countries(scale = "small", returnclass = "sf")

world %>% st_transform(crs = "+proj=moll") %>%
  ggplot() + geom_sf() + theme_minimal()

As I said before, I want to plot the coordinates on the world map.

You need to reproject these points, then you can use standard geom_point and geom_text . Your points are far too close together to see them all separately on a world map though:

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)

在此处输入图像描述

If you zoom it, it is much clearer:

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)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM