我试着用下面的意图来个性化一张地图:我想用较暗的色调绘制处理过的区域,用浅色调绘制控制区域。
现在,我有以下代码和各自的输出:
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()从该代码中,我们得到以下输出:

我希望处理区域为深蓝色,而控制区为浅蓝色(我的意思是,与我现在的情况相反)。与图例中的颜色比例不同,我希望只有两个带有文本的框:“浅蓝色”用于控制,“深蓝色”用于处理。
非常感谢您的时间和帮助!:)
发布于 2021-06-24 09:21:32
因为我没有你的数据,所以我只是使用你的代码以澳大利亚地图为例。treatment变量只有两个值,您可能希望将其作为因子。您可能还需要根据您的目的调整颜色代码和顺序:
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"))

发布于 2021-06-24 09:21:10
您只需将填充设置为因子(fill = as.factor(treatment)),然后使用scale_fill_manual手动设置每个级别的颜色。
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()https://stackoverflow.com/questions/68108382
复制相似问题