2014-12-04 86 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