Home > Enterprise >  Add region name on the map in geom_sf
Add region name on the map in geom_sf

Time:10-02

I have ever asked an old question here for geom_sf.

I post the code again here:

library(tidyverse)
library(ggplot2)
library(eurostat)
library(janitor)
library(sf)

eugd <- eurostat_geodata_60_2016 %>% clean_names()
eugdtr <- eugd %>% st_transform(crs = 3035)
gd_de <- eugdtr %>% filter(cntr_code == "DE", levl_code == 2) 

# download the dataset 
# Economically active population (unit: 1000)
df_d <- get_eurostat("lfst_r_lfp2act")

df_de <- df_d %>% 
  filter(
    geo %>% str_sub(1,2) == "DE", # only Italy
    geo %>% paste %>% nchar == 4, # only NUTS-2 
    age %in% c("Y15-24")# my guess is that most of our problems were because of the Russian Doll (Matreshka) effect of the way spatial data is organized
  ) %>% 
  transmute(
    id = geo %>% paste,
    year = time %>% lubridate::year(),
    eap = values,
    sex = sex
  ) %>% 
  group_by(id,year) %>% 
  summarise(eap= sum(eap)) %>% ungroup()

de <- left_join(gd_de, df_de, "id")

library(viridis)
library(cowplot)

# choose year=2000
de %>% 
  filter(year %in% c(2000)) %>%
  ggplot() 
  geom_sf(aes(fill = eap), color = NA) 
  scale_fill_viridis_b() 
  coord_sf(datum = NA) 
  theme_map() 
  theme(legend.position="right",
        plot.title = element_text(hjust = 0.5,color = "Gray40", size = 16, face = "bold"),
        plot.subtitle = element_text(color = "blue"),
        plot.caption = element_text(color = "Gray60"))  
  guides(fill = guide_legend(title = "Unit: 1000", title.position = "bottom", title.theme =element_text(size = 10, face = "bold",colour = "gray70",angle = 0)))

Today, I encountered a question that I have to add the label name of areas (variable: nuts_name) on each area on the map. I would appreciate if there is a hint about how to do it and also how to adjust the style of labels (e.g. font, size, color, position...)

CodePudding user response:

just found a hint from this page and add geom_sf_text() in the end

p<-de %>% 
  filter(year %in% c(2000)) %>%
  ggplot() 
  geom_sf(aes(fill = eap), color = NA) 
  scale_fill_viridis_b() 
  coord_sf(datum = NA) 
  theme_map() 
  theme(legend.position="right",
        plot.title = element_text(hjust = 0.5,color = "Gray40", size = 16, face = "bold"),
        plot.subtitle = element_text(color = "blue"),
        plot.caption = element_text(color = "Gray60"))  
  guides(fill = guide_legend(title = "Unit: 1000", title.position = "bottom", title.theme =element_text(size = 10, face = "bold",colour = "gray70",angle = 0)))



p geom_sf_text(aes(label = nuts_name), colour = "white")

  • Related