As @Alejandro Andrade mentioned, it is probably not possible with plot_ly
, but if you really want to have three categories of colours, you could trick it and use geom_bar. You could try:
#Create aplot and then extract the data
a <- ggplot(data=x, aes(x)) + geom_histogram()
temp <- layer_data(a, 1)
#calculate the mean and sd you want. Just an example
mean_vt <- mean(temp$x)
sd_vt <- sd(temp$x)
sd_vt2 <- 2*sd(temp$x)
sd_vt3 <- 3*sd(temp$x)
#create a new category for colors
temp$Color <-
ifelse(temp$x >= (mean_vt-sd_vt) & temp$x <= (mean_vt+sd_vt), "SD1",
ifelse(temp$x >= (mean_vt-sd_vt2) & temp$x <= (mean_vt+sd_vt2), "SD2",
ifelse(temp$x >= (mean_vt-sd_vt3) & temp$x <= (mean_vt+sd_vt3), "SD3",
"NA")))
#and then plot using ggplotly
pp <- ggplot(data = temp, aes(x =x,y=y, fill=Color)) +
geom_bar(stat = 'identity', width = 2.5) +
scale_fill_manual(values = c("blue", "green", "orange"))
ggplotly(pp)