Reputation: 27
I wanted to do something similar to this, but a little more complex.
Change colours of particular bars in a bar chart
Here is my data:
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)
so this is what it makes:
chart http://www.diabolotricks.net/Rplot-test.jpg
What I want to do is then use the p-value of that change to color the bars that are not statistically significant grey as well.
pval<- c(0.01,0.03,0.04,0.89,0.45,0.01,0.03,0.02)
so the fourth bar would be grey too.
I tried using various combinations of ifelse to no avail.
Upvotes: 0
Views: 1347
Reputation: 115425
Just replace the appropriate values in cols
. This is done easily with [<-
or you could use replace
which is a wrapper for the same thing
Assuming you are using alpha = 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)
Upvotes: 3