0

I am generating a histogram representing normally distributed data. I would like to color the histogram based on the standard deviation from the mean(i.e. within one SD = blue, 2 = green, 3=orange).

Here is a snippet of the code I'm using:

x <- rchisq(1000, 50, 10)
plot_ly(x=x, type="histogram")
MLavoie
  • 9,671
  • 41
  • 36
  • 56
Isaac
  • 215
  • 2
  • 15

2 Answers2

1

I don't think it is possible to define it exactly for the standard deviation that the user wants but I think this is a good alternative using ggplot2 and the ggplotly function of plotly

x <- rchisq(1000, 50, 10)
p = qplot(x =x, fill=..count.., geom="histogram",bins=30) +
  scale_fill_gradient(low="orangered2",high="yellow",guide = 'none')+
  theme_bw()+labs(y="")
ggplotly(p)
Alejandro Andrade
  • 2,196
  • 21
  • 40
1

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)
MLavoie
  • 9,671
  • 41
  • 36
  • 56