【问题标题】:Fill in continuous colors in geom_sf在 geom_sf 中填充连续颜色
【发布时间】:2021-09-03 08:04:15
【问题描述】:

我正在运行代码以在 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)))

我得到一个像这样的图:

但是,我想改变图例以及用连续颜色填充地图的颜色(因为特征“eap”是一个连续变量),不像这样,看起来像一个离散变量。例如,像这样:

我已经试过了

scale_fill_viridis_c()

scale_colour_gradient2()

两者都不起作用。

如果有人可以帮助我,将不胜感激 非常感谢。

【问题讨论】:

    标签: r ggplot2 geom viridis


    【解决方案1】:

    您可以将scale_fill_gradient2() 与手动midpointguide_colorbar() 一起使用以获得所需的效果:

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

    情节:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多