jimmyp1399473
jimmyp1399473

Reputation: 27

Change colours of particular bars in a bar chart - depending on two factors

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

Answers (1)

mnel
mnel

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)

enter image description here

Upvotes: 3

Related Questions