2014-02-12 315 views

回答

6

enter image description here檢查col參數和直方圖breaks財產。請參閱以下示例:

set.seed(0) 
x = rnorm(100, mean=0.5, sd=0.5) 
h = hist(x, breaks=10, plot=F) 

colors = rep("blue", length(h$breaks)) 
colors[h$breaks >= 0.3] = "red" 
colors[h$breaks >= 0.5] = "green" 
colors[h$breaks >= 0.7] = "orange" 
hist(x, breaks=10, col=colors) 
2

如何ggplot2

require(ggplot2) 
df<-data.frame(x=runif(100)) 
ggplot(df)+geom_histogram(aes(x,fill=factor(..x..)),binwidth=0.1) 

enter image description here

1

必須爲每個支柱設置顏色。 我這個例子中有8個支柱=>您需要發佈8種顏色HIST

set.seed(123) 
    a<-rnorm(20) 
    hist(a) 
    col<-c(rep("red",2),rep("blue",6)) 
    hist(a,col=col) 

鮑曼的答案是好得多!

+0

+1引用Baumann的答案。 – fotNelton

1

如果你想讓整個酒吧成爲相同的顏色,以前的答案很棒。如果您希望過渡符合所述的精確值,即使條中的斷點不匹配,也可以使用另一種方法(使用基本圖形):

set.seed(0) 
x = rnorm(100, mean=0.45, sd=0.25) 

hist(x, col='blue') 

tmp <- par('usr') 
clip(0.3,0.5, tmp[3], tmp[4]) 
hist(x, col='red', add=TRUE) 

clip(0.5, tmp[2], tmp[3], tmp[4]) 
hist(x, col='green', add=TRUE) 
相關問題