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


Here i'm using random variable for fill and
{geobr}package to fetch municipality shapes.For static border colors you can adjust
colorattribute outside ofaes()and for border width there'slinewidth. 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 firstFor 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 adjustingscalinginagg_png().This is how 6000x4000px render with specified scaling and line widths from this example looks like in native resolution:
Created on 2023-07-02 with reprex v2.0.2