How can I vary opacity in a plotly R chart

被刻印的时光 ゝ 提交于 2019-12-10 12:49:10

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/34683630/how-can-i-vary-opacity-in-a-plotly-r-chart

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!