ggplot:是否可以重叠 2 个地块?

问题描述 投票:0回答:1

我正在使用 shapefile 绘制我所在国家/地区的两张地图:一张用于实际工资低于 700 的地区,另一张用于实际工资高于 700 的地区。这是我的代码

plot 1 <-right_join(prov2022, dataset, by = "COD_PROV") %>% 
         ggplot(aes(fill = `Real Wage` < 700))+
         geom_sf() +
         theme_void() +
         theme(legend.position = "none", legend.title=element_blank())+
         scale_fill_manual(values = c('white', 'orange'))
  

plot 2<- right_join(prov2022, dataset, by = "COD_PROV") %>% 
         ggplot(aes(fill = `Real Wage` > 800))+
         geom_sf() +
         theme_void() +
         theme(legend.position = "none", legend.title=element_blank())+
         scale_fill_manual(values = c('white', 'red'))

完美无缺。

[![地图][1]][1] [1]: https://i.stack.imgur.com/oADaw.jpg

有没有办法将第二个图重叠在第一个图上?更准确地说,我需要将第二个情节的填充区域放入第一个情节

r ggplot2 plot colors fill
1个回答
0
投票

您可以创建一张地图并使用例如

dplyr::case_when
创建要映射到
fill
上的间隔或使用
cut
.

使用

ggplot2::geom_sf
中的默认示例:

library(ggplot2)
library(sf)
#> Linking to GEOS 3.10.2, GDAL 3.4.2, PROJ 8.2.1; sf_use_s2() is TRUE

nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)

ggplot(nc) +
  geom_sf(aes(fill = dplyr::case_when(
    AREA < .1 ~ "a",
    AREA > .15 ~ "c",
    .default = "b"
  ))) +
  scale_fill_manual(
    values = c("red", "white", "orange"),
    labels = c(a = "< .1", b = ".1 <= x <= .15", c = "> .15")
  ) +
  labs(fill = NULL)

创建于 2023-02-21 与 reprex v2.0.2

© www.soinside.com 2019 - 2024. All rights reserved.