首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用geom_sf个性化地图

如何使用geom_sf个性化地图
EN

Stack Overflow用户
提问于 2021-06-24 08:46:27
回答 2查看 59关注 0票数 1

我试着用下面的意图来个性化一张地图:我想用较暗的色调绘制处理过的区域,用浅色调绘制控制区域。

现在,我有以下代码和各自的输出:

代码语言:javascript
复制
mapas_regiones %>%
  ggplot() + 
  geom_sf(aes(geometry = geometry, fill = treatment), col = "#ffffff")  +
  labs(title = "Regiones tratadas",
       caption = "Fuente: Elaboración propia en base de reportes de Carabineros, y chilemapas") +
  coord_sf()   +
  theme_void()

从该代码中,我们得到以下输出:

我希望处理区域为深蓝色,而控制区为浅蓝色(我的意思是,与我现在的情况相反)。与图例中的颜色比例不同,我希望只有两个带有文本的框:“浅蓝色”用于控制,“深蓝色”用于处理。

非常感谢您的时间和帮助!:)

EN

回答 2

Stack Overflow用户

发布于 2021-06-24 09:21:32

因为我没有你的数据,所以我只是使用你的代码以澳大利亚地图为例。treatment变量只有两个值,您可能希望将其作为因子。您可能还需要根据您的目的调整颜色代码和顺序:

代码语言:javascript
复制
mapas_regiones %>% 
  ggplot() + 
  geom_sf(aes(geometry = geometry, fill = as.factor(treatment)))  +
  labs(title = "Regiones tratadas",  
       caption = "Fuente: Elaboración propia en base de reportes de Carabineros, y chilemapas") +
  coord_sf()   +
  theme_void() + 
  scale_fill_manual(values=c("lightblue", "darkblue")) +
  guides(fill=guide_legend(title="Treatment"))

票数 2
EN

Stack Overflow用户

发布于 2021-06-24 09:21:10

您只需将填充设置为因子(fill = as.factor(treatment)),然后使用scale_fill_manual手动设置每个级别的颜色。

代码语言:javascript
复制
ggplot() + 
  # Change fill as factor
  geom_sf(aes(geometry = geometry, fill = as.factor(treatment), col = "#ffffff"))  +
  labs(title = "Regiones tratadas",
       caption = "Fuente: Elaboración propia en base de reportes de Carabineros, y chilemapas") +
  coord_sf()   +
  theme_void() +
  # Set the fill colors manually
  scale_fill_manual(values = c("lightblue", "darkblue")) +
  theme_void()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68108382

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档