You can add group so that it knows more than one opacity:
plot_ly(df, type="bar", x=x, y=y, group=x, opacity=opacity, marker = list( color='#5a22e3' ) )

Update
As for color, adding color as a variable does a similar thing as group , but it should 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 this in suppressWarnings() .
source share