2

I have four very simple example vectors:

a<-c(1,3,5,7,9)
b<-c(1,2,3,4,5)
c<-c(1,1.8,2.6,3.4,4.2)
d<-c(1,1.5,2,2.5,3)

I have plotted the vector a and added the rest of the vectors in with lines(), like so

plot(a,type="l")
lines(b,type="l")
lines(c,type="l")
lines(d,type="l")

Now I have this basic graph

enter image description here

I wish to alter the coloring of the lines by implementing a color gradient, let's say from light pink to dark red.

I have a matrix with the same samples that were plotted, but with values independent from the plot.

Sample    Value
a         634
b         473
c         573
d         124

So my question is: How do I add a color gradient in to the plot so that the value of the sample dictates the hue of the color so that the higher value in the matrix value-column colors the line of the respective sample in a more darker red?

My elementary proficiency in R has led me to a point where I have the plot depicted in the image but I lack the know-how required to get started on the code that would produce the color gradient.

Gradient of n colors ranging from color 1 and color 2 I found this to be of some help, but I don't really know how to apply this knowledge in a way my question dictates.

All help is much appreciated. Thank you in advance.

-Olli J

Community
  • 1
  • 1
Olli J
  • 599
  • 2
  • 6
  • 22

1 Answers1

3

Try:

dd = data.frame(a,b,c,d)

colvalue
  Sample Value
1      a   634
2      b   473
3      c   573
4      d   124

colvalue$scaledvalue = with(colvalue, (Value-min(Value))/ (max(Value)-min(Value)) )
colvalue
  Sample Value scaledvalue
1      a   634   1.0000000
2      b   473   0.6843137
3      c   573   0.8803922
4      d   124   0.0000000

plot(a,type="n")
dd = data.frame(a,b,c,d)
for(i in 1:length(dd)){
    lines(dd[i], type='l', col = rgb(colvalue$scaledvalue[i],0,0))
}

enter image description here

rnso
  • 20,794
  • 19
  • 81
  • 167