2014-12-04 80 views
4

如果我有一個直方圖:如何我可以用顏色特定數據條中的R直方圖

> hist(faithful$waiting,seq(min(faithful$waiting),max(faithful$waiting))) 

和「特殊」的頻率列表:

> c(51, 52, 57, 59, 64) 

是有可能顏色對應的條到這些特殊的頻率與組織塊的其餘部分不同的顏色?

+0

您是否嘗試過分配特殊頻率的因素,然後使用填補這個因素? – lawyeR 2014-12-04 13:59:29

+0

你有兩個答案。除非您有後續問題,否則請接受您的回答,以便將問題標記爲已回答。 – cdeterman 2014-12-05 14:10:10

回答

4

您可以簡單地創建顏色矢量並使用col選項。

data(faithful) 

# make sure frequencies in order and unique for the histogram call 
special <- ifelse(sort(unique(faithful$waiting)) %in% c(51, 52, 57, 59, 64), "red", "white") 

# same call with the 'col' option 
hist(faithful$waiting,seq(min(faithful$waiting),max(faithful$waiting)), col=special) 

enter image description here

2

樂趣GGPLOT2 ...

faithful$special <- faithful$waiting %in% c(51, 52, 57, 59, 64) 

library(ggplot2) 

ggplot(data = faithful, aes(x = waiting, fill = special)) + 
    geom_histogram(binwidth = 1, colour = 'white') 

enter image description here