sylvia
sylvia

Reputation: 217

Fill in continuous colors in geom_sf

I am running codes to create maps in R:

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)))

and I acquire a figure like this:

enter image description here

However, I would like to change the legend as well as the color filled in the map with continuous colors (since the feature "eap" is a continuous variable), not like this, looks like a discrete variable. for example, like this:

enter image description here

I have already tried

scale_fill_viridis_c()

and

scale_colour_gradient2()

both do not work.

will appreciate if someone can help me on this Thank you very much.

Upvotes: 6

Views: 4477

Answers (1)

ktiu
ktiu

Reputation: 2636

You can use scale_fill_gradient2() with a manual midpoint and guide_colorbar() for the desired effect:

de %>% 
  filter(year %in% c(2000)) %>%
  ggplot() +
  geom_sf(aes(fill = eap), color = NA) +
  scale_fill_gradient2(midpoint = 275) +
  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_colorbar(title = "Unit: 1000",
                             title.position = "bottom",
                             title.theme = element_text(size = 10,
                                                        face = "bold",
                                                        colour = "gray70",
                                                        angle = 0)))

Plots:

plot with gradient

Upvotes: 5

Related Questions