问题
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