ropensci / rnaturalearth

An R package to hold and facilitate interaction with natural earth map data :earth_africa:
http://ropensci.github.io/rnaturalearth/
Other
217 stars 23 forks source link

is there a way to retrieve a country 'middle point'? #55

Closed sghignone closed 1 year ago

sghignone commented 2 years ago

In a ggplot plot based on whole world map, I'd like to place a geom_point (with size proportional to a variable) at a coordinate representing the 'center' of a selected list of countries. Is there a way to get the country middle point easily? Thanks

mps9506 commented 2 years ago

Use the st_point_on_surface() function in sf.

library(rnaturalearth)
library(sf)
library(purrr)
library(dplyr)
library(ggplot2)

countries <- ne_countries(returnclass = "sf")

countries_center <- st_point_on_surface(countries)

ggplot() +
  geom_sf(data = countries) +
  geom_sf(data = countries_center,aes(size = pop_est), color = "steelblue")

image

PMassicotte commented 1 year ago

Closing this since it is not related to rnaturalearth but rather on sf and general geo-computational manipulations.