shashwat vajpeyi
shashwat vajpeyi

Reputation: 155

ggplot: color gradient of bars using user values

I am plotting columns gene and logFC of following data frame using ggplot: I have omitted some lines for simplicity

gene       variable        logFC       logP
CS, gltA    25:1 vs 5:1   -1.6443477   11.3492020
ACO, acnA   25:1 vs 5:1   -1.4393181   8.1938808
IDH3        25:1 vs 5:1   -1.6374355   5.0329107
LSC1        25:1 vs 5:1    0.6577531   0.5235756
CS, gltA    250:1 vs 5:1  -1.0587372   1.3417342
ACO, acnA   250:1 vs 5:1  -0.4354191   7.1746206
IDH3        250:1 vs 5:1  -0.9513132   5.6105933
LSC1        250:1 vs 5:1  -0.3871476   3.9403641

and here is my code:

ggplot(tca_m, aes(gene,logFC))
last_plot()+geom_bar(aes(fill=tca_m$variable),width = 0.5, stat="identity",position = "dodge") 
last_plot()+ coord_flip()

which gives me the following plot:plot.jpg

Now I would like to change color of the bars using a color gradient based upon the value of column logP and follow a different color scheme for two different variable under the variable column. Is this possible using ggplot? Thanks!

Upvotes: 2

Views: 644

Answers (1)

Nate
Nate

Reputation: 10671

We can't have two different scales for an aesthetic in a ggplot. However we can use scale_fill_distiller and facet_wrap to build a nice plot :

ggplot(tca_m, aes(gene,logFC)) +
    geom_bar(aes(fill = logFC), stat="identity", position = "dodge", width = .5) +
    coord_flip() +
    scale_fill_distiller(palette = "RdBu") +
    facet_wrap(~variable, ncol = 1)

Using the logFC variable for fill shows the up/down regulation better than logP, but you could use which ever one you preferred.

enter image description here

Upvotes: 2

Related Questions