Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R plot - normal curves with color gradient

Tags:

r

How can I make curves with a color gradient in R. Take a look at this enter image description hereflame.

It should look like that. I tried to make a normal curve and then another normal curve, but technically speaking, you can't make such a figure with a bunch of normal curves because they won't come down and intersect at the right spot on either side. How can I make such a figure in R? Any ideas?

like image 499
CodeGuy Avatar asked Feb 21 '13 21:02

CodeGuy


1 Answers

The best I've been able to do so far is:

 par(bg="black")
 plot(seq(0.15,0.85,by=0.01), 
      5*dbeta(seq(0.15,0.85,by=0.01),10,10 ), 
      type="l" , ylim=c(0,700) )  # this just sets up the plotting framework.

 for( i in 1:200 ) { lines(x= seq(0.15,0.85,by=0.01), 
                           y= i*dbeta(seq(0.15,0.85,by=0.01),10,10 ), 
                     col= colorRampPalette(c("yellow", "orange", "red", "hotpink", 
                             "violet", "blue", "lightblue", "lightgreen", "darkgreen",
                             "black"))(200)[i], 
                      lwd=13) }
 par(bg="white")

I did discover that putting a "black" color at the beginning of that series add an extra "glow" to the overall result, but I'm not posting that result.

enter image description here

-----------------

This is what I started with and then there are successive approximation and tweaks appearing below:

plot(seq(0.15,0.85,by=0.01), 5*dbeta(seq(0.15,0.85,by=0.01),10,10 ), 
            type="l" , ylim=c(0,100))
for( i in seq(0.2, 5) ) { lines(seq(0.15,0.85,by=0.01), 
                     i*5*dbeta(seq(0.15,0.85,by=0.01),10,10 ) ) }

enter image description here

For colors:

plot(seq(0.15,0.85,by=0.01), 5*dbeta(seq(0.15,0.85,by=0.01),10,10 ), type="l" ,
          ylim=c(0,130))
for( i in 1:35 ) {lines(seq(0.15,0.85,by=0.01), i*dbeta(seq(0.15,0.85,by=0.01), 10,10 ), 
                           col=colorRampPalette(c("yellow", "orange", "red", "violet", 
                                        "blue", "lightblue", "lightgreen"))(35)[i],
                   lwd=3) }

enter image description here

For black background and denser colors and a fade to black at top:

 par(bg = 'black')
 plot(seq(0.15,0.85,by=0.01), 5*dbeta(seq(0.15,0.85,by=0.01),10,10 ), type="l", 
          ylim=c(0,130) )
 for( i in 1:35 ) { lines(seq(0.15,0.85,by=0.01), i*dbeta(seq(0.15,0.85,by=0.01),10,10), 
                          col=colorRampPalette(c("yellow", "orange", "red", "violet", 
                                "blue", "lightblue", "lightgreen", "darkgreen", 
                                 "black")) (35)[i], 
                           lwd=13) }

enter image description here

I noticed that the fading to black aspect also controlled the line width at the sides. I hadn't been expecting that but it seems to be a desirable feature. The other aspect not addressed here is the possibility of adding transparency. There is an alpha argument in the R RGB functions.

One useful trick for finding colors byname:

grep("purple", colors(), value=TRUE)
 [1] "mediumpurple"  "mediumpurple1" "mediumpurple2" "mediumpurple3" "mediumpurple4"
 [6] "purple"        "purple1"       "purple2"       "purple3"       "purple4"      

If you are playing with the iteration to make the gradient smoother then you will need also adjust the ylim argument: choose 0.5^9*0.5^9/beta(10,10)*[iterations] , since that will be the maximum at x=0.5.

like image 87
IRTFM Avatar answered Sep 19 '22 22:09

IRTFM