gpt4 book ai didi

r - 控制ggplot2图例中的 'alpha'级别

转载 作者:行者123 更新时间:2023-12-03 07:39:59 25 4
gpt4 key购买 nike

在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"))

但是如何控制alpha的水平,我不相信element_rect有这种能力。

最佳答案

您可以通过提供颜色和 alpha 值,使用 scales 包中的函数 alpha() 来控制半透明度。当您为 fill= 提供颜色时,可以在 element_rect() 内部使用此函数。

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)))

enter image description here

关于r - 控制ggplot2图例中的 'alpha'级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16251966/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com