title: ggplot2 make legend key fill transparent
body: <p>I am trying to make the legend key fill for a ggplot transparent.  I followed the instructions on one of Hadley's ggplot2 guides for changing the legend key fill, but for some reason when I set the fill to transparent it fills with gray.  Even when I set the legend key fill to white, it still appears gray in the final plot.</p>
  
  <p>Here is an example:</p>
  
  <pre><code>library(ggplot2)
  
  data1 = c(0,10, 11, 23, 33, 40, 41, 50, 59, 68, 76, 88, 90, 99)
  data2 = c(2, 8, 10, 22, 39, 47, 49, 55, 62, 70, 76, 86, 88, 95)
  
  df = data.frame(data1, data2)
  
  (plot = ggplot() +
    geom_smooth(data=df, aes(data1, data2,colour="sample1"))+
    geom_abline(intercept=0, slope=1,linetype="dashed", color = "black")+
    scale_x_continuous(expand=c(0,0), limits=c(0,100)) + 
    scale_y_continuous(expand=c(0,0), limits=c(0,100))+
    theme_classic()+
    labs(y="data2", x="data1", 
         title="sample 1 data1 vs data2") +
    theme(plot.title = element_text(size=18, face="bold"),
          legend.key = element_rect(colour = "transparent", fill = "white"),
          legend.justification = c(1,0), legend.position = c(1,0))+
    scale_color_discrete(name="Sample") )
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/dVcoi.png" rel="noreferrer"><img src="https://i.stack.imgur.com/dVcoi.png" alt="Example_plot"></a></p>
  
  <p>If I set <code>theme(legend.key = element_rect(colour = "transparent", fill = "red"))</code> I get the following plot:
  <a href="https://i.stack.imgur.com/w59wZ.png" rel="noreferrer"><img src="https://i.stack.imgur.com/w59wZ.png" alt="red_fill"></a></p>
  
  <p>So it appears that I can change the legend key fill, but just not to the color white or transparent.</p>
  
  <p>Does anyone know what I am doing wrong, or if there is just no way to make the legend key fill transparent/white ?</p>
  
  <p><strong>EDIT: Setting <code>theme(legend.key = element_rect(fill = alpha("white", 0.0)))</code> Does not fix the problem.</strong>  </p>
  
  <p><strong>See here:</strong></p>
  
  <pre><code>library(ggplot2)
  library(scales)
  
  data1 = c(0,10, 11, 23, 33, 40, 41, 50, 59, 68, 76, 88, 90, 99)
  data2 = c(2, 8, 10, 22, 39, 47, 49, 55, 62, 70, 76, 86, 88, 95)
  
  df = data.frame(data1, data2)
  
  (plot = ggplot() +
    geom_smooth(data=df, aes(data1, data2,colour="sample1"))+
    theme_classic()+
    labs(y="data2", x="data1", 
         title="sample 1 data1 vs data2") +
    theme(plot.title = element_text(size=18, face="bold"),
          legend.key = element_rect(colour = "transparent", fill = alpha("red", 0)),
          legend.justification = c(1,0), legend.position = c(1,0))+
    scale_color_discrete(name="Sample") )
  </code></pre>
  
  <p><strong>EDIT2:  If I use <code>geom_line()</code> instead of <code>geom_smooth</code> I am able to set the legend key fill to NA, so it must be because the line in <code>geom_smooth</code> has a gray area for the confidence interval around it, therefore the legend key mirrors that look.</strong></p>
  
  <pre><code>(plot = ggplot() +
    geom_smooth(data=df, aes(data1, data2,colour="sample1"))+
    geom_abline(intercept=0, slope=1,linetype="dashed", color = "black")+
    scale_x_continuous(expand=c(0,0), limits=c(0,100)) + 
    scale_y_continuous(expand=c(0,0), limits=c(0,100))+
    theme_classic()+
    labs(y="data2", x="data1", 
         title="sample 1 data1 vs data2") +
    theme(plot.title = element_text(size=18, face="bold"),
          legend.key = element_rect(colour = NA, fill = NA),
          legend.justification = c(1,0), legend.position = c(1,0))+
    scale_color_discrete(name="Sample") )
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/o1Xut.png" rel="noreferrer"><img src="https://i.stack.imgur.com/o1Xut.png" alt="geom_line"></a></p>