Search code examples
rggplot2legendggpattern

How do you remove pattern_fill legend?


I'm wondering how and if it's possible to remove the legend created by pattern_fill?

This is my data:

#To use non-english characters
Sys.setlocale("LC_ALL", "nb-NO.UTF-8")
library(ggpattern)

#Data
df <- data.frame(country = c("Afghanistan", "Australia", "Bangladesh", "Bhutan", 
                             "Brunei", "Cambodia", "China", "India" , 
                             "Indonesia", "Iran", "Japan", "Kazakhstan",
                             "Kyrgyzstan", "Laos", "Malaysia", "Maldives", 
                             "Mongolia", "Myanmar","Nepal", "North Korea", 
                             "Pakistan", "Philippines", "Russia", "Singapore",
                             "South Korea", "Sri Lanka", "Taiwan", "Tajikistan",
                             "Thailand", "Timor-Leste","Turkmenistan", 
                             "Uzbekistan", "Vietnam"),
                 main = c(0, 1, 3, 0, 2, 3, 6, 2, 3, 0, 1, 2, 2, 0, 2, 2, 2, 0,
                          2, 5, 3, 1, 4, 1, 1, 3, 2, 3, 2, 2, 2, 2, 2))

#Combining df with map data
mapdata <- map_data('world')
mapdata <- mapdata %>% 
  rename(country = region) %>% 
  left_join(df, by = 'country')

And here is my map:

map3 <- ggplot(mapdata1, aes(x = long, y = lat)) +
  geom_polygon_pattern(aes(fill = as.factor(main), group = group, 
                           pattern = as.factor(main), pattern_fill = '#ff4f8a'), 
                       colour = 'grey30', pattern_density = .5, pattern_spacing = .025,
                       pattern_colour = 'NA') +
  annotate(
    geom = 'curve', x = 135, y = 40, xend = 75, yend = 25, 
    curvature = -0.9, linewidth = 2, colour = '#0b81ac',
    alpha = .4
  ) +
  coord_sf(xlim = c(70, 145), ylim = c(55, -15)) +
  scale_fill_manual(values = c("grey", 
                               "#1481ff", 
                               "#b16cec", 
                               "#b16cec", 
                               "#ff4f8a", 
                               "#ff6b52", 
                               "#ff9214"),
                    labels = c("Nøytral", 
                               "Alliert USA",
                               "USA",
                               "USA og Kina",
                               "Kina",
                               "Alliert Kina",
                               "Kina"),
                    name = 'Militært samarbeid:') +
  scale_pattern_manual(values = c('none', 
                                  'none', 
                                  'none', 
                                  'stripe', 
                                  'none', 
                                  'none', 
                                  'none'),
                       labels = c("Nøytral", 
                                  "Alliert USA",
                                  "USA",
                                  "USA og Kina",
                                  "Kina",
                                  "Alliert Kina",
                                  "Kina"),
                       name = 'Militært samarbeid:') +
  labs(colour = "Militærbaser:") +
  guides(pattern = guide_legend(override.aes = list(fill = c("grey", 
                                                             "#1481ff", 
                                                             "#b16cec", 
                                                             "#b16cec", 
                                                             "#ff4f8a", 
                                                             "#ff6b52", 
                                                             "#ff9214"),
                                                    pattern_fill = '#ff4f8a')),
         pattern_fill = guide_legend(position = 'none')) +
  theme(axis.text = element_blank(),
        axis.title = element_blank(),
        axis.ticks = element_blank(),
        legend.position = "right",
        panel.background = element_rect(fill = "#ccf3ff"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank())
map3

Here is what I get (the points are irrelevant in this context): my map

I want to remove the upper most legend, but I can't really find from where it appears. If I remove the pattern_fill from the geom_polygon_pattern, it disappears, but the stripes turn grey and I want to have them in the colour it is in the picture.

geom_polygon_pattern(aes(fill = as.factor(main), group = group, 
                           pattern = as.factor(main), pattern_fill = '#ff4f8a'),

Any help would be much appreciated


Solution

  • There are already some indications here. I think that adding to your ggplot + guides(pattern = none) should do the work.

    Thanks to the comment, the final trick is: + guides(pattern_fill = 'none')