【问题标题】:Controlling the 'alpha' level in a ggplot2 legend控制 ggplot2 图例中的“alpha”级别
【发布时间】:2013-04-27 12:10:46
【问题描述】:

在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有这个能力。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您可以使用包scales 中的函数alpha() 通过提供颜色和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)))
    

    【讨论】:

    • 我试过了,但我的 ggplot2 报告错误“找不到函数 alpha”...???使用最新版本,0.9.3.1
    • 这个函数在库(scales)中。更新了我的答案。
    猜你喜欢
    • 2013-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 2011-07-14
    • 1970-01-01
    相关资源
    最近更新 更多