0
votes

I want to plot some lines on a plot and for each segment depending on the score I wish to plot the color as a gradient from Blue (-values) - white(0 values) - Red (+ values). Now using the Segments part of my code I can do this and if I give a specific colors, such as col="red" or col="blue", it works nice. So due to the sheer size of my data I wanted to then automate it using the :

rbPal <- colorRampPalette(c('red','white','blue'))
jcolor<-rbPal(nrow(datasetsize))

The problem is that when I get to plotting the data it just plots random colors and not the colors I have assigned using color palette even though checking the html color code it is the correct color. Any help would be greatly appreciated. (See Code Below)

TEST DATA:

Sample  loc Start   End p   sm
S1  9   1000000 10000000    2   -7.5751
S2  9   11000000    18000000    6   -1.5906
S3  9   20000000    40000000    3   0
S4  9   50000000    70000000    2   0.8
S5  9   80000000    100000000   2   1.25
S6  9   110000000   140000000   6   7.789

Code:

### Read in Data
data<-read.table("stack_eg.txt",header=TRUE,sep="\t")

### Order Setdata Object
data<-data[order(data$sm,decreasing=T),]

### Setup Plot Space
plot(1, type="n", axes=T, xlab="X", ylab="Y",ylim=c(1,6),xlim=c(0,142000000))

### Color setup
rbPal <- colorRampPalette(c('red','white','blue'))
jcolor<-rbPal(6)
jonzo<-cbind(data,jcolor)

### Plot data
for(l in 1:nrow(jonzo)){
  startpos<-jonzo$Start[l]
  endpos<-jonzo$End[l]
  segments(startpos,2,endpos,2,col=jonzo$jcolor[l])
}

I get an image Like this: enter image description here

Instead of an image like this: enter image description here

1
Thanks for the tip. What I have noticed is that it seems to be an issue with the reading the color code from jonzo. If I remove this and just pull the colors from jcolor it works well.Jcrow06

1 Answers

0
votes

Ok so it looks like that the problem is in the following section:

jonzo<-cbind(data,jcolor)

This reads the color code #FF0000 from the column bound files. If you simply read the color code from the the jcolor object then it works and the following image is produced: enter image description here

Here is the adjusted code for those interested:

### Read in Data
data<-read.table("stack_eg.txt",header=TRUE,sep="\t")

### Order Setdata Object
data<-data[order(data$sm,decreasing=T),]

### Setup Plot Space
plot(1, type="n", axes=T, xlab="X", ylab="Y",ylim=c(1,6),xlim=c(0,142000000))

### Color setup
rbPal <- colorRampPalette(c('red','white','blue'))
jcolor<-rbPal(6)

### Plot data
for(l in 1:nrow(jonzo)){
  startpos<-jonzo$Start[l]
  endpos<-jonzo$End[l]
  segments(startpos,2,endpos,2,col=jcolor[l])
}