[R] graphs with gradients of colors

Chris Stubben stubben at lanl.gov
Mon Sep 17 21:41:23 CEST 2007


You could use a loop and fill small polygons with colors.

x<-seq(-3, 3, .01)
y<-eval(expression(x^3-3*x))
plot(x,y, type="n", las=1)   
n<-length(x)

# vertical bars
for(i in 1:n)
{
polygon(c(x[i], x[i], x[i+1], x[i+1]), c(min(y), y[i], y[i+1], min(y)),
border=0, col = rainbow(n)[i]) 
}

## or split each vertical bars into 100 smaller blocks
plot(x,y, type="n", las=1)
for(i in 1:n)
{
  y1<-seq(y[i], min(y), length.out=101)
  y2<-seq(y[i+1], min(y), length.out=101)
  for(j in 1:100)
  { 
    polygon(c(x[i], x[i], x[i+1], x[i+1]), c(y1[j+1], y1[j], y2[j],
y2[j+1]), border=0, col = rev(heat.colors(100))[j]) 
  }
}

Chris Stubben




Van Dongen Stefan wrote:
> 
> Hi All,
>  
> I would like to fill the area under a curve with a gradient of colors. Are
> there any packages or trick I could use
>  
> Thanks
>  
> Stefan
>  
>  
> Stefan Van Dongen
> Antwerp
> 
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/graphs-with-gradients-of-colors-tf4466780.html#a12743725
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list