Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the opacity of a plotly polygon or ribbon in R?

Tags:

r

plotly

In plotly for R there is an opacity argument for lines and markers, but how do I change the opacity of fill colours in add_ribbons and add_polygons?

e.g. opacity = 0 makes the line disappear here:

plot_ly() %>% add_lines(x = 1:4, y = 1:4, color = I("red"), opacity = 0)

but has no effect on the polygon here:

plot_ly() %>% add_polygons(x = c(1, 1, 2, 2), y = c(1, 2, 2, 1), 
                           color = I("red"), opacity = 0)

Using alpha doesn't work either. How can I change the opacity of fill colors?

like image 292
jim Avatar asked Oct 17 '25 18:10

jim


1 Answers

As an alternative you could try using fillColor and rgba color (last digit being alpha):

%>% add_polygons(fillcolor = 'rgba(7, 164, 181, 1)')

vs

%>% add_polygons(fillcolor = 'rgba(7, 164, 181, 0.2)')

Just adjust the numbers for the colour you want.

like image 189
MLavoie Avatar answered Oct 20 '25 08:10

MLavoie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!