Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I vary opacity in a plotly R chart

Tags:

r

plotly

Here is an toy example. The opacity value in data.frame has no impact

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'    
    )
    )

I might also want to extend have a color column in df and utilize that in place of the fixed value above TIA

like image 219
pssguy Avatar asked Jan 08 '16 18:01

pssguy


1 Answers

You can add a group so that it knows to look for 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

With respect to color, adding color as a variable does a similar thing as group, but it needs to 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 that into a suppressWarnings().

like image 51
Sam Dickson Avatar answered Sep 19 '22 17:09

Sam Dickson