ggplot2 make legend key fill transparent

前端 未结 5 2054
不知归路
不知归路 2021-02-07 03:27

I am trying to make the legend key fill for a ggplot transparent. I followed the instructions on one of Hadley\'s ggplot2 guides for changing the legend key fill, but for some

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-07 04:02

    To make use of the transparency levels, one can use:

    For the entire legend:

    theme(legend.background=element_rect(fill = alpha("white", 0.5)))
    

    alpha("white", 0) being completely transparent like element_blank(), and alpha("white", 1) having no transparency.

    Now, for the key - if key and background have different transparencies:

    theme(legend.background=element_rect(fill = alpha("white", 0)),
          legend.key=element_rect(fill = alpha("white", .5)))
    

    Note: background transparency overrules the one for the key, i.e. background alpha must be smaller than key alpha.

提交回复
热议问题