Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using multiple scale_colour_gradient scales for different ranges of the data in one plot

Tags:

r

ggplot2

I am very new to R so please bear with me if something is not clear in my question.

I have a data.frame "protein" with 5 columns, namely;

1.protein_name, 2.protein_FC, 3.protein_pval, 4.mRNA_FC, 5.mRNA_pval and 6.freq.

I am trying to plot a volcano plot with x=log2(protein_FC), y=-log10(protein_pval). Then map the size of the dots to freq and colour to mRNA_FC. This all works fine and here is the code that I have used:

ggplot( protein [ which ( protein$freq <= 0.05 ),] , aes( x = log2( protein_FC ) ,
       y = -log10 ( protein_pval ) , size = freq , colour = mRNA_FC , 
       label = paste(protein_name,",",mRNA_pval), alpha=1/1000)) + 
  geom_point() + geom_text( hjust = 0 , vjust = 0 , colour = "black" , size = 2.5 ) + 
  geom_abline( intercept = 1.3 , slope = 0) + 
  scale_colour_gradient(limits=c(-3,3))

all is fine till here. But because of the nature of the experiment, data it is quite dense around mRNA_FC = 0. There, the default colour scheme that ggplot applies doesnt work very well in distinguishing different points.

I have tried various colour scales by using low="colour1" and high="colour2". However I think it will be best to use multiple colour scales over the ranges of mRNA_FC, i.e. something like. blue to white for -3<mRNA<-0.2, red to white for -0.2<mRNA_FC<0, green to white for 0<mRNA_FC<0.2 and black to white for 0.2<mRNA_FC<3.

But I havent found any way of doing it yet.

Any help would be appreciated. Cheers!

like image 895
ktyagi Avatar asked Apr 13 '12 13:04

ktyagi


1 Answers

For this type of thing you want to use scale_gradientn. For example:

library(ggplot2)

x = seq(-0.1, 0.1, len=100)
y = 0:10
dat = expand.grid(x=x, y=y)

ggplot(data=dat, aes(x=x, y=y, fill=x)) +
  geom_raster() +
  scale_fill_gradientn(colours=c('red', 'yellow', 'cyan', 'blue'),
    values   = c(-0.05,-1e-32,1e-32,0.05),
    breaks   = c(-0.05,-0.005,0.005,0.05),
    rescaler = function(x,...) x,
    oob      = identity)

enter image description here

like image 107
John Colby Avatar answered Oct 09 '22 14:10

John Colby