user1290547
user1290547

Reputation: 43

I would like to add the borders of municipalities to map of Brazil. How can I achieve this using the ggplot, sf and brazilmaps packages?

I am currently working on a map visualization in R using the ggplot. brazilmaps and the sf packages. I have successfully plotted the municipalities of a certain region, filling them with the color "purple2" based on a variable called trab_resgatados. However, I am facing difficulties in changing the color of the municipality borders.

Could you kindly assist me by providing some suggestions?

Here is the code I am using:

muni_plot <- ggplot(data = muni_map) +
  geom_sf(aes(fill = trab_resgatados), size = 0.27, color = "purple2") +
  labs(fill = "Slaves") +
  scale_fill_gradient(low = 'white', high = 'purple2', na.value = "green") +
  theme(panel.grid = element_line(colour = "transparent"),
        panel.background = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank())

enter image description here

I have tried various approaches to add the municipality borders to the plot, but none of them have worked. Interestingly, when I use the following code to add the state borders, it works perfectly fine:

geom_sf(data = get_brmap("State"),
        fill = "transparent",
        colour = "black", size = 0.5)

enter image description here

However, when I try to use a similar approach to add the municipality borders using the get_brmap("City") function, which provides the municipality number codes (equivalent to FIPS in the US), it doesn't work.

I would greatly appreciate any guidance or suggestions on how to change the color of the municipality borders in my ggplot map using the sf package. Thank you in advance for your assistance!

Upvotes: 0

Views: 373

Answers (1)

margusl
margusl

Reputation: 17594

Here i'm using random variable for fill and {geobr} package to fetch municipality shapes.

For static border colors you can adjust color attribute outside of aes() and for border width there's linewidth. What you probably need to consider here is output image resolution, also combination of resolution and line width, otherwise borders will just cover the whole fill area for smaller polygons or are too thin to be visible.

Sample bellow is rendered with AGG graphics device, default device, at least in Windows, would produce something bit different. To change it in RStudio: Tools > Global Options General > Graphics > Backend: AGG , {ragg} package should be installed first

For a usable result, output resolution should probably be increased (a lot), this in turn can start to affect text sizes and line widths. When using {ragg}, it can be addressed by adjusting scaling in agg_png().

This is how 6000x4000px render with specified scaling and line widths from this example looks like in native resolution:

crop

library(sf)
library(dplyr)
library(geobr)
library(ggplot2)

# use ragg to save output file
library(ragg)

set.seed(123)
muni_map <- read_municipality(year = 2020) %>% 
  # add random variable for fill, set 50 values to NA
  mutate(random_geom_var = rgeom(n(), .01) %>% replace(sample(n(),50),NA))
#> Using year 2020

# color sets polygon border colour;
# reduce line width so borders would not mask fill colour
p1 <- ggplot(muni_map) +
  geom_sf(aes(fill = random_geom_var), linewidth = 0.01, color = "black") +
  scale_fill_gradient(low = 'white', high = 'purple2', na.value = "green") +
  theme_void()

p1


# use ragg to save 6000x4000px png, adjust scaling for lines and text
agg_png("out.png", 6000, 4000, scaling = 12)
p1
dev.off()

Created on 2023-07-02 with reprex v2.0.2

Upvotes: 0

Related Questions