R 如何在ggplot中应用legend.key反向透明?

R 如何在ggplot中应用legend.key反向透明?,r,ggplot2,R,Ggplot2,我使用ggplot2包中的mpg数据来测试绘图参数。我发现lengend.key backgound(右上角,标签车型)颜色可以更改为透明或白色以外的任何其他颜色。如何将lengend key backgound默认灰色更改为透明 library(ggplot2) p <- ggplot(data=mpg,mapping=aes(x=cty,y=hwy,colour=class))+ geom_point(aes(size=displ),alpha=0.5)+ stat

我使用ggplot2包中的
mpg
数据来测试绘图参数。我发现lengend.key backgound(右上角,标签车型)颜色可以更改为透明或白色以外的任何其他颜色。如何将lengend key backgound默认灰色更改为透明

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')
    )
库(ggplot2)

p您需要添加
se=FALSE
或删除
stat\u smooth(method=“leash”)
以删除
车型中的灰色填充背景

库(ggplot2)
P