简体   繁体   中英

How can I vary opacity in a plotly R chart

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

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

在此输入图像描述

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

在此输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM