rggplot2legend

How to remove legend.key background box in second legend guide in ggplot


I have two y axes in a plot and legends to go with each. The right axis for the point is in the plot and the accompanying legend titled "I" here. In the code, it's the scale_color_manual or color inside of guides(). I want to get rid of the dark grey square that surrounds the point in the legend. I want to keep the legend and the point. I've circled the symbol with red in the attached image.

I tried setting legend.key = element_rect(fill = NA) as well as tried NULL in place of the NA. A similar question and answer set the legend.key.width = unit(0, "mm") and I tried that, but that just made the box into a vertically oriented rectangle.

Here are some data:

dput(del1)
structure(list(month = structure(c(6L, 6L, 6L, 6L, 6L), levels = c("January", 
"February", "March", "April", "May", "June", "July", "August", 
"September", "October", "November", "December"), class = "factor"), 
    n_inf = c(2L, 2L, 2L, 2L, 2L), zone = c("Zone 5", "Zone 5", 
    "Zone 5", "Zone 5", "Zone 5"), fac1 = c(120L, 120L, 120L, 
    120L, 120L), gr_mo1 = c(5L, 5L, 5L, 5L, 5L), gr_mo2 = c(9L, 
    9L, 9L, 9L, 9L), variables = c("pct0", "pct1", "pct2", "pct3", 
    "pct4"), vals = c(0.268043178947998, 0.465607260195834, 0.265910323616373, 
    0.000438256452026712, 9.80787768735849e-07)), row.names = 146:150, class = "data.frame")

And minimal code to produce a plot. This plot is a very minimal version of what I'm making, but it has the two legends.

library(tidyverse)
library(patchwork)

label_map <- c("pct0" = "0", "pct1" = "1", "pct2" = "2", "pct3" = "3", "pct4" = "4")

ggplot(data = del1, aes(x=month)) +
  geom_bar(aes(y=vals, fill=factor(variables, labels=label_map)), color = "white",
           stat = "identity", show.legend = T) +
  geom_point(aes(y = n_inf/fac1, col = "grey20"), size = 3) +
  theme(panel.grid.major.x= element_blank(), panel.grid.major.y = element_blank(),
        panel.grid.minor.y = element_blank(), axis.title.x = element_blank(), 
        plot.title = element_blank(),
        axis.title.y = element_blank(),
        axis.text.x = element_text(size = 14, face = "bold", color = "black"),
        axis.text.y.left = element_blank(),
        axis.text.y.right = element_text(size = 14, face = "bold", color = "black"))+
  scale_y_continuous(sec.axis = sec_axis(~.*120, name = "Infections, N")) +
  scale_color_manual(values = "grey20", name = "Infection Count") +
  guides(color = guide_legend(order = 2, title = "I", 
                              theme = theme(legend.title = element_text(size = 20, face = "bold"), 
                                            legend.text = element_blank(), legend.key = element_rect(fill = NULL, color = NULL)),
                              override.aes = list(color = "grey20"))) 

enter image description here


Solution

  • Drop show.legend=TRUE from the geom_bar to avoid the key glyph for the bars to show up in the color legend and set fill=NA for the legend.key to remove the default background fill for the legend key.

    library(ggplot2)
    
    label_map <- c("pct0" = "0", "pct1" = "1", "pct2" = "2", "pct3" = "3", "pct4" = "4")
    
    ggplot(data = del1, aes(x = month)) +
      geom_bar(aes(y = vals, fill = factor(variables, labels = label_map)),
        color = "white",
        stat = "identity"
      ) +
      geom_point(aes(y = n_inf / fac1, col = "grey20"), size = 3) +
      theme(
        panel.grid.major.x = element_blank(), panel.grid.major.y = element_blank(),
        panel.grid.minor.y = element_blank(), axis.title.x = element_blank(),
        plot.title = element_blank(),
        axis.title.y = element_blank(),
        axis.text.x = element_text(size = 14, face = "bold", color = "black"),
        axis.text.y.left = element_blank(),
        axis.text.y.right = element_text(size = 14, face = "bold", color = "black")
      ) +
      scale_y_continuous(sec.axis = sec_axis(~ . * 120, name = "Infections, N")) +
      scale_color_manual(values = "grey20", name = "Infection Count") +
      guides(color = guide_legend(
        order = 2, title = "I",
        theme = theme(
          legend.title = element_text(size = 20, face = "bold"),
          legend.text = element_blank(),
          legend.key = element_rect(fill = NA, color = NULL)
        ),
        override.aes = list(color = "grey20")
      ))