I have a world map where countries are colored and some colors also have stripes (so that it is ready when printed in black and white).
I managed to add the stripes using the package ggpattern
, but I haven’t succeeded in merging the legend of ggplot2
and ggpattern
: how can I include the stripes in the legend for colors, and remove the legend for patterns?
Bonus if you manage to make another coordinate system work with ggpattern
(I’d like to add this line: “).
Here is a minimum reproducible example with only two colors (which takes a lot of time to run, for some reason).
library(tidyverse)
library(ggplot2)
library(ggpattern)
breaks <- c(-Inf, 0, Inf)
labels <- c("negative", "positive")
world_map <- map_data(map = "world")
data <- data.frame(country_map = unique(world_map$region))
data$mean <- -1
data$mean[data$country_map == "Russia"] <- 1
data$group <- cut(data$mean, breaks = breaks, labels = labels)
data$pattern <- ifelse(data$mean < 0, "stripe", NA)
ggplot(data) + geom_map(aes(map_id = country_map, fill = fct_rev(group)), map = world_map) + #coord_proj("+proj=robin", xlim = c(-135, 178.5), ylim = c(-56, 84)) +
geom_polygon(data = world_map, aes(x = long, y = lat, group = group), colour = 'grey', size = 0, fill = NA) + expand_limits(x = world_map$long, y = world_map$lat) + theme_void() +
geom_map_pattern(data = data, map = world_map, aes(map_id = country_map, pattern = pattern), pattern_fill = "black", fill = NA, pattern_density = 0.1, pattern_angle = 30, pattern_spacing = 0.02) #+
scale_fill_manual(name = "Group", drop = FALSE, values = c("red", "blue"), labels = labels)