I am creating a series of species range maps. In some cases, there will be overlapping features for species that occur in different seasons. So far, the best way I have found to represent this is with solid fill mixed with pattern fill for different seasons so that overlapping features can be distinguishable. I'm having trouble configuring the legend in this case. I've tried adding aesthetics and scale_fill_manual, but those have screwed up the formatting of the map.
Can anyone provide advice on configuring a legend for the map below? Or does anyone have suggestions for a better way I could be visually representing overlapping features?
ggplot() +
geom_sf(data=county_sf, fill=NA) +
geom_sf_pattern(data=b, pattern_fill='black', pattern_colour='black', pattern_size=0.01) +
geom_sf(data=m, fill='black', alpha=0.5) +
scale_fill_manual(values=c("black"))+
theme_void() +
theme(legend.position="bottom") +
theme(legend.title=element_blank())
Here is what the map looks like:
If you want to have a legend then you have to map on aesthetics, i.e. instead of setting the fill
and pattern_fill
as parameters move them inside aes()
. Afterwards you can set your desired colors using scale_fill_manual
and scale_pattern_fill_manual
.
Using a minimal reproducible example based on the default example from ?geom_sf
:
library(ggplot2)
library(ggpattern)
county_sf <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
b <- county_sf[c(1, 3, 5, 7), ]
m <- county_sf[c(1, 2, 3), ]
ggplot() +
geom_sf(data = county_sf, fill = NA) +
geom_sf_pattern(
data = b,
aes(pattern_fill = "b"),
pattern_colour = "black", pattern_size = 0.01
) +
geom_sf(
data = m,
aes(fill = "m"),
alpha = 0.5
) +
scale_fill_manual(
values = c("black"),
name = NULL
) +
scale_pattern_fill_manual(
values = c("red"),
name = NULL
) +
theme_void() +
theme(
legend.position = "bottom"
)