Cobin
Cobin

Reputation: 930

How to apply legend.key backround transparent in ggplot?

I use mpg data in ggplot2 package to test plot parameters. I found that the lengend.key backgound (upper right, label vehicle model) colour could be changed any other colours but transparent or white. How to change the lengend key backgound default grey to transparent?

library(ggplot2)
p <- ggplot(data=mpg,mapping=aes(x=cty,y=hwy,colour=class))+
     geom_point(aes(size=displ),alpha=0.5)+
     stat_smooth(method='loess')+
     scale_size_continuous(range=c(4,10))+
     facet_wrap(~year,ncol=2)+
      labs(x='distance per gallon in city',y='distance per gallon in highway',
      title='fuel consumption and model',size='displacement',colour='vehicle model')

p+
theme(
    plot.background=element_rect(fill='transparent', colour='transparent'),
    panel.background=element_rect(fill='transparent', colour='gray'),
    panel.border=element_rect(fill='transparent', colour='black'),        
    axis.text=element_text(color='black'),
    panel.grid.major=element_line(colour='grey',linetype='dashed'),
    strip.background=element_rect(fill='transparent', colour='transparent'),
    panel.spacing.x=unit(4,'mm'),
    legend.box.background=element_rect(fill='blue', colour='transparent'),
    legend.key=element_rect(fill='transparent', colour='transparent')
    )

enter image description here

Upvotes: 7

Views: 9467

Answers (1)

Tung
Tung

Reputation: 28381

You need to either add se = FALSE or remove stat_smooth(method = "loess") to remove the grey filled background in vehicle model legend keys.

library(ggplot2)

p <- ggplot(data = mpg, mapping = aes(x = cty, y = hwy, colour = class)) +
  geom_point(aes(size = displ), alpha = 0.5) +
  stat_smooth(method = "loess", se = FALSE) + # Add se = FALSE
  scale_size_continuous(range = c(4, 10)) +
  facet_wrap(~year, ncol = 2) +
  labs(
    x = "distance per gallon in city", y = "distance per gallon in highway",
    title = "fuel consumption and model", size = "displacement", colour = "vehicle model"
  )

p + 
  theme(
    plot.background = element_blank(),
    panel.background = element_rect(fill = "transparent", colour = "gray"),
    panel.border = element_rect(fill = "transparent", colour = "black"),
    axis.text = element_text(color = "black"),
    panel.grid.major = element_line(colour = "grey", linetype = "dashed"),
    strip.background = element_blank(),
    panel.spacing.x = unit(4, "mm"),
    legend.box.background = element_rect(fill = "blue", colour = "transparent"),
    legend.key = element_rect(fill = "transparent", colour = "transparent")
  )

p +
  theme(
    plot.background = element_rect(fill = "transparent", colour = "transparent"),
    panel.background = element_rect(fill = "transparent", colour = "gray"),
    panel.border = element_rect(fill = "transparent", colour = "black"),
    axis.text = element_text(color = "black"),
    panel.grid.major = element_line(colour = "grey", linetype = "dashed"),
    strip.background = element_rect(fill = "transparent", colour = "transparent"),
    panel.spacing.x = unit(4, "mm"),
    legend.background = element_blank(),
    legend.box.background = element_blank(),
    legend.key = element_blank()
  )

Created on 2018-11-03 by the reprex package (v0.2.1.9000)

Upvotes: 7

Related Questions