0

I am plotting a map using the world dataset from spData, which looks as follows:

Simple feature collection with 6 features and 10 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -180 ymin: -18.28799 xmax: 180 ymax: 83.23324
Geodetic CRS:  WGS 84
# A tibble: 6 x 11
  iso_a2 name_long   continent   region_un subregion    type       area_km2     pop lifeExp gdpPercap                                             geom
  <chr>  <chr>       <chr>       <chr>     <chr>        <chr>         <dbl>   <dbl>   <dbl>     <dbl>                               <MULTIPOLYGON [°]>
1 FJ     Fiji        Oceania     Oceania   Melanesia    Sovereign…   1.93e4  8.86e5    70.0     8222. (((180 -16.06713, 180 -16.55522, 179.3641 -16.8…
2 TZ     Tanzania    Africa      Africa    Eastern Afr… Sovereign…   9.33e5  5.22e7    64.2     2402. (((33.90371 -0.95, 34.07262 -1.05982, 37.69869 …
3 EH     Western Sa… Africa      Africa    Northern Af… Indetermi…   9.63e4 NA         NA         NA  (((-8.66559 27.65643, -8.665124 27.58948, -8.68…
4 CA     Canada      North Amer… Americas  Northern Am… Sovereign…   1.00e7  3.55e7    82.0    43079. (((-122.84 49, -122.9742 49.00254, -124.9102 49…
5 US     United Sta… North Amer… Americas  Northern Am… Country      9.51e6  3.19e8    78.8    51922. (((-122.84 49, -120 49, -117.0312 49, -116.0482…
6 KZ     Kazakhstan  Asia        Asia      Central Asia Sovereign…   2.73e6  1.73e7    71.6    23587. (((87.35997 49.21498, 86.59878 48.54918, 85.768…

I have plotted the the outlines of each country using:

spData::world %>% ggplot() + geom_sf(aes(geometry=geom)) 

I would also like the plot a label for each country, preferably at the Lat/Long midpoint of each country spData::world data.frame. I presume this is done from the geom field, but I don't know how to obtain it.

user438383
  • 5,716
  • 8
  • 28
  • 43
  • https://stackoverflow.com/questions/42225987/geom-text-finding-centroids-and-adding-text-in-a-polygon-using-ggplot2 – crestor Apr 13 '21 at 12:15

1 Answers1

1

A possible approach without expressly determining centroids; albeit the map is a bit crowded.

library(spData)
library(ggplot2)
library(sf)

ggplot(world) + 
  geom_sf(aes(geometry = geom))+
  geom_sf_text(aes(label = name_long), size = 1.5)

Created on 2021-04-13 by the reprex package (v2.0.0)

Peter
  • 11,500
  • 5
  • 21
  • 31