通过标准偏差曲线R颜色组织图

时间:2021-03-09 19:16:52

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

我正在生成表示正态分布数据的直方图。我想根据与平均值的标准偏差对直方图进行着色(即在一个SD =蓝色,2 =绿色,3 =橙色)内。

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

这是我正在使用的代码片段:

x <- rchisq(1000, 50, 10)
plot_ly(x=x, type="histogram")

2 个解决方案

#1


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

我认为不可能完全根据用户想要的标准差来定义它,但我认为这是一个很好的选择,使用ggplot2和plotg的ggplotly函数

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)

#2


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:

正如@Alejandro Andrade所说,使用plot_ly可能是不可能的,但是如果你真的想拥有三种颜色,你可以欺骗它并使用geom_bar。你可以尝试:

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

#1


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

我认为不可能完全根据用户想要的标准差来定义它,但我认为这是一个很好的选择,使用ggplot2和plotg的ggplotly函数

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)

#2


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:

正如@Alejandro Andrade所说,使用plot_ly可能是不可能的,但是如果你真的想拥有三种颜色,你可以欺骗它并使用geom_bar。你可以尝试:

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