rggplot2ggpatterngeom-map

Merge the legend from ggpattern and ggplot2


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?

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)

Result


Solution

  • You could turn off the fill guide and override the aesthetics of the pattern :

    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)
    pattern <- c("negative" = "stripe", "positive" = "none")
    
    ggplot(data) + 
      geom_map(aes(map_id = country_map, fill = fct_rev(group)), map = world_map) + 
      geom_polygon(data = world_map, 
                   aes(x = long, y = lat, group = group), 
                   colour = 'grey', size = 0,  fill = NA) + 
      scale_fill_manual(values = c("negative" = "#abcdef", "positive" = "#fedcba")) +
      geom_map_pattern(data = data, 
                       map = world_map, 
                       aes(map_id = country_map, pattern = group),
                       pattern_alpha = 1,
                       fill = "transparent",
                       pattern_fill = "black",
                       pattern_colour = "black",
                       pattern_density = .1,
                       pattern_key_scale_factor = .5)+
      scale_pattern_manual(values = pattern)+
      guides(fill = "none",
            pattern = guide_legend(override.aes = list(fill = c("negative" = "#abcdef", "positive" = "#fedcba" ))))