2013-01-10 36 views
0

我想做类似的事情,但稍微复杂一点。更改条形图中特定条的颜色 - 取决于两个因素

Change colours of particular bars in a bar chart

这里是我的数据:

x <- c(3,-1.4,0.8,-0.3,-1.2,-2.5,1.5,-1.4) 
breaks <- c(-Inf, -1, 1, Inf) 
cols <- c("blue", "grey", "red")[findInterval(x, vec=breaks)] 
barplot(x, col = cols, horiz=T) 

所以这是它使:

chart http://www.diabolotricks.net/Rplot-test.jpg

我想要做的就是再使用的p值是什么这种变化也会使不具统计意义的灰色条纹变色。

pval<- c(0.01,0.03,0.04,0.89,0.45,0.01,0.03,0.02) 

所以第四个栏也是灰色的。

我试过使用ifelse的各种组合无济于事。

回答

3

只需更换cols中的相应值即可。这与[<-轻松完成或者你可以使用replace这是同样的事情

假设你正在使用阿尔法的包装= 0.05

myalpha <- 0.05 
cols[pval > myalpha] <- 'grey' # could also be cols <- replace(cols, pvals > 0.05, 'grey') 
barplot(x, col = cols, horiz=T) 

enter image description here

+1

应该有废弃矿井按钮/ upvote你的。 –