This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Color bars from ggplot on p-values

I am trying to make a bar plot, the length of which is the percentage of genes in a GO term and I would like to color the bars corresponding to the range of their p-values.

My data looks like this:

head(Up_Down)
               GO            Percentage     q.val
1  extracellular region part  2.7028565 2.305075e-07
2        extracellular space  2.1167634 2.305075e-07
3          cytokine activity  0.5917833 1.498300e-06
4      inflammatory response  1.3599636 0.7

coloursv <- Up_Down$q.val
names(coloursv) <- Up_Down$q.val

ggplot(data=Up_Down, aes(x=GO, y=Percentage)) +
  geom_bar(stat="identity") +
  scale_fill_manual(coloursv,guide = F)+
  theme(axis.text.x = element_text(angle =90, hjust = 1,vjust=0.5))

The plot is generated successfully the way I want but unfortunately the bars are not coloured according to the q.val. Can somebody help me identify what is going wrong.

r

Why not just:

ggplot(data = Up_Down, aes(x = GO, y = Percentage, fill = q.val)) +
  geom_bar(stat = "identity") +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))

1 answer

Hi saamar.rajput,

Adding aes(fill = coloursv) to geom_bar should work:

ggplot(data = Up_Down, aes(x = GO, y = Percentage)) +
  geom_bar(stat = "identity", aes(fill = coloursv)) +
  theme(axis.text.x = element_text(
    angle = 90,
    hjust = 1,
    vjust = 0.5
  ))

barplot

Thanks a lot, i tried this too before but somehow it didn't work that time but now it works :)

Log in to answer this question.