[R] to represent color range on plot segment

Jim Lemon jim at bitwrit.com.au
Mon Aug 29 11:57:30 CEST 2011


On 08/28/2011 04:07 AM, karthicklakshman wrote:
> Dear R community,
>
> With an advantage of being "NEW" to R, I would like to post a very basic
> query here,
>
> I am in need of representing gene expression data which ranges from -0.09 to
> +4, on plot "segment". please find below the data df, the expression values
> are in df[,2]. kindly help me with the code, so that I can represent the
> values with a clear color gradient (something like -0.09 to 0 as red
> gradient and 0 to +4 as green gradient)
>
>              location         value
> 15  chr+:14001-15001  0.99749499
> 16  chr+:15001-16001  0.99957360
> 17  chr+:16001-17001  0.99166481
> 18  chr+:17001-18001  0.97384763
> 19  chr+:18001-19001  0.94630009
> 20  chr+:19001-20001  0.90929743
> 21  chr+:20001-21001  0.86320937
> 22  chr+:21001-22001  0.80849640
> 23  chr+:22001-23001  0.74570521
> 24  chr+:23001-24001  0.67546318
> 25  chr+:24001-25001  0.59847214
> 26  chr+:25001-26001  0.51550137
> 27  chr+:26001-27001  0.42737988
> 28  chr+:27001-28001  0.33498815
> 29  chr+:28001-29001  0.23924933
> 30  chr+:29001-30001  0.14112001
> 31  chr+:30001-31001  0.04158066
> 32  chr+:31001-32001 -0.05837414
> 33  chr+:32001-33001 -0.15774569
> 34  chr+:33001-34001 -0.25554110
> 35  chr+:34001-35001 -0.35078323
> 36  chr+:35001-36001 -0.44252044
> 37  chr+:36001-37001 -0.52983614
> 38  chr+:37001-38001 -0.61185789
> 39  chr+:38001-39001 -0.68776616
> 40  chr+:39001-40001 -0.75680250
> 41  chr+:40001-41001 -0.81827711
> 42  chr+:41001-42001 -0.87157577
> 43  chr+:42001-43001 -0.91616594
> 44  chr+:43001-44001 -0.95160207
>
Hi karthick,
Here's one way to do it:

library(plotrix)
df[,3]<-NA
df[df[,2]<0,3]<-color.scale(df[df[,2]<0,2],1,c(0,1),c(0,1))
df[df[,2]>0,3]<-color.scale(df[df[,2]>0,2],c(1,0),1,c(0,1))

df[,3] will then be a vector of colors that range from red at the 
minimum value to white at 0 to green at the maximum value.

Jim



More information about the R-help mailing list