How to personalize maps using geom_sf

I'm trying to personalize a map with the following intention: I want to paint in a darker tone the regions that were treated and in a light tone the control regions.

For now I have the following code and the respective output:

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

From that code we get this output: enter image description here

I wish to have the treatment regions in dark blue and the control regions in light blue (I mean, the opposite as what I have for now). Instead of having a scale of colors in the legend, I would like to have just two boxes with a text: "light blue" for control and "dark blue" for treatment.

Thank you a lot for your time and help! :)

Upvotes: 1

Views: 993

Answers (2)

Zhiqiang Wang
Zhiqiang Wang

Reputation: 6769

Since I do not have your data, I just use Australian map as an example using your code. The treatment variable has only two values, you may want to take it as factor instead. You may also need to adjust color codes and order for your purpose:

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

enter image description here

Upvotes: 2

You just need to set the fill as factor (fill = as.factor(treatment)) and then manually set the colors for each level using 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()

Upvotes: 1

Related Questions