Controlling the 'alpha' level in a ggplot2 legend

后端 未结 1 1830
失恋的感觉
失恋的感觉 2020-12-13 03:48

In ggplot2, how can I make the legend have a semi-transparent background.

The following code, gives a fully transparent background (and positioning control)

相关标签:
1条回答
  • 2020-12-13 04:33

    You can control semitransparency with function alpha() from package scales by providing color and alpha value. This function can be used inside element_rect() when you provide color for 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)))
    

    enter image description here

    0 讨论(0)
提交回复
热议问题