(using languages but also about using with tmap and ggplot)
This vignette shows how rnaturalearth makes it easier to make maps with labels in languages other than Engish, and to make thematic maps if you have data that is referenced in languages other than Engish.
rnaturalearth is an R package to hold and facilitate interaction with natural earth vector map data.
Natural Earth is a public domain map dataset including vector country boundaries.
library(rnaturalearth)
library(sp)
library(sf)
library(ggplot2)
library(ggrepel)
library(tmap)
library(knitr)
sf_use_s2(FALSE)
data(df_layers_physical)
data(df_layers_cultural)
::kable(df_layers_physical, caption = "physical vector data available via ne_download()") knitr
layer | scale10 | scale50 | scale110 |
---|---|---|---|
antarctic_ice_shelves_lines | 1 | 1 | 0 |
antarctic_ice_shelves_polys | 1 | 1 | 0 |
coastline | 1 | 1 | 1 |
geographic_lines | 1 | 1 | 1 |
geography_marine_polys | 1 | 1 | 1 |
geography_regions_elevation_points | 1 | 1 | 1 |
geography_regions_points | 1 | 1 | 1 |
geography_regions_polys | 1 | 1 | 1 |
glaciated_areas | 1 | 1 | 1 |
lakes | 1 | 1 | 1 |
lakes_europe | 1 | 0 | 0 |
lakes_historic | 1 | 1 | 0 |
lakes_north_america | 1 | 0 | 0 |
lakes_pluvial | 1 | 0 | 0 |
land | 1 | 1 | 1 |
land_ocean_label_points | 1 | 0 | 0 |
land_ocean_seams | 1 | 0 | 0 |
land_scale_rank | 1 | 0 | 0 |
minor_islands | 1 | 0 | 0 |
minor_islands_coastline | 1 | 0 | 0 |
minor_islands_label_points | 1 | 0 | 0 |
ocean | 1 | 1 | 1 |
ocean_scale_rank | 1 | 0 | 0 |
playas | 1 | 1 | 0 |
reefs | 1 | 0 | 0 |
rivers_europe | 1 | 0 | 0 |
rivers_lake_centerlines | 1 | 1 | 1 |
rivers_lake_centerlines_scale_rank | 1 | 1 | 0 |
rivers_north_america | 1 | 0 | 0 |
::kable(df_layers_cultural, caption = "cultural vector data available via ne_download()") knitr
layer | scale10 | scale50 | scale110 |
---|---|---|---|
admin_0_antarctic_claim_limit_lines | 1 | 0 | 0 |
admin_0_antarctic_claims | 1 | 0 | 0 |
admin_0_boundary_lines_disputed_areas | 1 | 1 | 0 |
admin_0_boundary_lines_land | 1 | 1 | 1 |
admin_0_boundary_lines_map_units | 1 | 0 | 0 |
admin_0_boundary_lines_maritime_indicator | 1 | 1 | 0 |
admin_0_boundary_map_units | 0 | 1 | 0 |
admin_0_breakaway_disputed_areas | 0 | 1 | 0 |
admin_0_countries | 1 | 1 | 1 |
admin_0_countries_lakes | 1 | 1 | 1 |
admin_0_disputed_areas | 1 | 0 | 0 |
admin_0_disputed_areas_scale_rank_minor_islands | 1 | 0 | 0 |
admin_0_label_points | 1 | 0 | 0 |
admin_0_map_subunits | 1 | 1 | 0 |
admin_0_map_units | 1 | 1 | 1 |
admin_0_pacific_groupings | 1 | 1 | 1 |
admin_0_scale_rank | 1 | 1 | 1 |
admin_0_scale_rank_minor_islands | 1 | 0 | 0 |
admin_0_seams | 1 | 0 | 0 |
admin_0_sovereignty | 1 | 1 | 1 |
admin_0_tiny_countries | 0 | 1 | 1 |
admin_0_tiny_countries_scale_rank | 0 | 1 | 0 |
admin_1_label_points | 1 | 0 | 0 |
admin_1_seams | 1 | 0 | 0 |
admin_1_states_provinces | 1 | 1 | 1 |
admin_1_states_provinces_lakes | 1 | 1 | 1 |
admin_1_states_provinces_lines | 1 | 1 | 1 |
admin_1_states_provinces_scale_rank | 1 | 1 | 1 |
airports | 1 | 1 | 0 |
parks_and_protected_lands_area | 1 | 0 | 0 |
parks_and_protected_lands_line | 1 | 0 | 0 |
parks_and_protected_lands_point | 1 | 0 | 0 |
parks_and_protected_lands_scale_rank | 1 | 0 | 0 |
populated_places | 1 | 1 | 1 |
populated_places_simple | 1 | 1 | 1 |
ports | 1 | 1 | 0 |
railroads | 1 | 0 | 0 |
railroads_north_america | 1 | 0 | 0 |
roads | 1 | 0 | 0 |
roads_north_america | 1 | 0 | 0 |
time_zones | 1 | 0 | 0 |
urban_areas | 1 | 1 | 0 |
urban_areas_landscan | 1 | 0 | 0 |
# Africa
::plot(ne_countries(continent = "africa"))
terra#> Warning: The `returnclass` argument of `ne_download()` sp as of rnaturalearth 1.0.0.
#> ℹ Please use `sf` objects with {rnaturalearth}, support for Spatial objects
#> (sp) will be removed in a future release of the package.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.
<- ne_countries(continent = "africa", returnclass = "sf")
sfaf <- st_centroid(sfaf)
sfafc #> Warning: st_centroid assumes attributes are constant over geometries
#> Warning in st_centroid.sfc(st_geometry(x), of_largest_polygon =
#> of_largest_polygon): st_centroid does not give correct centroids for
#> longitude/latitude data
<- cbind(sfaf, st_coordinates(st_centroid(sfaf$geometry)))
sfaf #> Warning in st_centroid.sfc(sfaf$geometry): st_centroid does not give correct
#> centroids for longitude/latitude data
# G = cbind(G, st_coordinates(st_centroid(G$geometry)))
# this adds centroids in the middle of countries
ggplot(sfaf) +
geom_sf() +
geom_sf(data = sfafc)
# trying labels in the middle of countries, doesn't quite work needs x,y,label
# but once x & y added on with st_coordinates ... seems getting the coords might
# not be necessary for much longer https://github.com/slowkow/ggrepel/issues/111
# cool nearly there ...
ggplot(sfaf) +
geom_sf() +
geom_text_repel(aes(x = X, y = Y, label = name))
#> Warning: ggrepel: 25 unlabeled data points (too many overlaps). Consider
#> increasing max.overlaps
# getting there, labels still overlap a bit maybe make map bigger to allow space
# for labels
ggplot(sfaf) +
geom_sf() +
geom_text_repel(aes(x = X, y = Y, label = name_es))
#> Warning: ggrepel: 27 unlabeled data points (too many overlaps). Consider
#> increasing max.overlaps
# point.padding=NA allows labels to overlap the centroid
ggplot(sfaf) +
geom_sf() +
geom_text_repel(aes(x = X, y = Y, label = name_es), point.padding = NA)
#> Warning: ggrepel: 27 unlabeled data points (too many overlaps). Consider
#> increasing max.overlaps
# Africa labels just down left & right sides works pretty well I think
ggplot(sfaf) +
geom_sf() +
xlim(-28, 61) +
geom_text_repel(aes(x = X, y = Y, label = name_es),
data = subset(sfaf, X > 21),
nudge_x = 60 - subset(sfaf, X > 21)$X,
segment.size = 0.2,
segment.color = "grey50",
direction = "y",
hjust = 0
+
) geom_text_repel(aes(x = X, y = Y, label = name_es),
data = subset(sfaf, X < 21),
nudge_x = -19 - subset(sfaf, X < 21)$X,
segment.size = 0.2,
segment.color = "grey50",
direction = "y",
hjust = 1
)#> Warning: ggrepel: 18 unlabeled data points (too many overlaps). Consider
#> increasing max.overlaps
# french labels
ggplot(sfaf) +
geom_sf() +
xlim(-28, 61) +
geom_text_repel(aes(x = X, y = Y, label = name_fr),
data = subset(sfaf, X > 21),
nudge_x = 60 - subset(sfaf, X > 21)$X,
segment.size = 0.2,
segment.color = "grey50",
direction = "y",
hjust = 0
+
) geom_text_repel(aes(x = X, y = Y, label = name_fr),
data = subset(sfaf, X < 21),
nudge_x = -19 - subset(sfaf, X < 21)$X,
segment.size = 0.2,
segment.color = "grey50",
direction = "y",
hjust = 1
)#> Warning: ggrepel: 18 unlabeled data points (too many overlaps). Consider
#> increasing max.overlaps
# tmap good but labels currently overlap
# english labels
tm_shape(sfaf) +
tm_borders() +
tm_text("name")
# spanish labels
tm_shape(sfaf) +
tm_borders() +
tm_text("name_es")
# other languages de, fr, nl,
tm_shape(sfaf) +
tm_borders() +
tm_text("name_de", size = 0.5)