控制在GGPLOT2传说中的“阿尔法”级(Controlling the 'alpha

2019-09-01 06:42发布

在GGPLOT2,我怎样才能使传说有一个半透明的背景。

下面的代码,给出了一个完全透明的背景(和定位控制)

plot <- plot + theme(legend.position=c(1,1),legend.justification=c(1,1),
                       legend.direction="vertical",
                       legend.box="horizontal",
                       legend.box.just = c("top"), 
                       legend.background = element_rect(fill="transparent"))

但如何控制阿尔法的水平,我不相信element_rect具有这种能力。

Answer 1:

可以控制与功能半透明alpha()从包scales通过提供色彩和α值。 此功能可以在里面使用element_rect()时,您提供的颜色fill=

library(scales)    
p<-ggplot(iris,aes(Petal.Length,Petal.Width,color=Species))+geom_point()
p+theme(legend.position=c(1,1),legend.justification=c(1,1),
        legend.direction="vertical",
        legend.box="horizontal",
        legend.box.just = c("top"), 
        legend.background = element_rect(fill=alpha('blue', 0.4)))



文章来源: Controlling the 'alpha' level in a ggplot2 legend
标签: r ggplot2