我如何在绘图R图表中改变不透明度 [英] How can I vary opacity in a plotly R chart

查看:177
本文介绍了我如何在绘图R图表中改变不透明度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个玩具示例. data.frame中的不透明度值没有影响

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

我可能还想在df中扩展一个color列,并使用它代替上面的固定值 TIA

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

推荐答案

您可以添加group,以便它知道要查找多个不透明度:

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

更新

关于颜色,将color作为变量添加与group相似,但是它必须是一个因子或字符变量(请注意,我删除了group):

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

由于只有两个级别,所以这会给您一个警告,因此您可以将所有这些内容放入suppressWarnings().

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

这篇关于我如何在绘图R图表中改变不透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆