Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply legend.key backround transparent in ggplot?

Tags:

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

like image 703
Cobin Avatar asked Nov 04 '18 04:11

Cobin


1 Answers

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)

like image 178
Tung Avatar answered Oct 19 '22 22:10

Tung