r-plotly 图例背景(部分)透明

Nib*_*ced 1 r plotly r-plotly

我有与此 OP相同的问题,但对于 R 而言,是plotly 而不是plot()。

是否可以使绘图对象的图例背景透明?如何?如果可能,最好选择部分透明度,例如 alpha 级别为 0.4。

KGe*_*Gee 6

您应该能够使用 rgba(0,0,0,0) 的 alpha 元素调整透明度,例如:

library(plotly)
library(dplyr)

fig <- plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length, 
               color = ~Species, colors = "Set1")

# completely transparent
fig %>% layout(legend = list(x=.5, y=.5, bgcolor = 'rgba(0,0,0,0)'))

# blue background
fig %>% layout(legend = list(x=.5, y=.5, bgcolor = 'rgb(0,75,154)'))

# blue background, semi-transparent
fig %>% layout(legend = list(x=.5, y=.5, bgcolor = 'rgba(0,75,154,0.4)'))
Run Code Online (Sandbox Code Playgroud)

(出于演示目的,图例尴尬地放置在情节顶部)