Color bars from ggplot on p-values
1
0
Entering edit mode
5.5 years ago

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 • 5.1k views
ADD COMMENT
1
Entering edit mode

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))
ADD REPLY
4
Entering edit mode
5.5 years ago
AK ★ 2.2k

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

ADD COMMENT
0
Entering edit mode

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

ADD REPLY

Login before adding your answer.

Traffic: 2529 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6