How can I change the opacity in the R chart

Here is an example of a toy. The opacity value in data.frame does not affect

library(plotly) df <- data.frame(x=c(1,2),y=c(6,3),opacity=c(1,0.2)) plot_ly(df, type="bar", x=x, y=y, opacity=opacity, marker = list( color='#5a22e3' ) ) 

Maybe I also want to expand the color column in df and use this instead of a fixed value above TIA

+6
source share
1 answer

You can add group so that it knows more than one opacity:

 plot_ly(df, type="bar", x=x, y=y, group=x, opacity=opacity, marker = list( color='#5a22e3' ) ) 

enter image description here

Update

As for color, adding color as a variable does a similar thing as group , but it should be a factor or a character variable (note that I removed group ):

 plot_ly(df, type="bar", x=x, y=y, opacity=opacity, color=as.factor(x) ) 

enter image description here

Since there are only two levels, this will give you a warning, so you can put all this in suppressWarnings() .

+6
source

All Articles