[R] continuous coloring of a polygon

Roger Leenders r.t.a.j.leenders at rug.nl
Fri Aug 15 14:00:38 CEST 2008


R2.7.1, WinXP

Hi,

I have a polygon inside a circle as follows:

radius <- 3
x <- seq(-radius,radius,length=2000)
y <- sqrt(radius^2-x^2)
xx <- c(x,-x)
yy <- c(y,-y)
plot(xx,yy, xlim=c(-radius,radius),ylim=c(-radius,radius), type="l", 
ylab="", xlab="", axes=F)

radius <- 2.7
x1 <- seq(-radius,radius,length=2000)
y1 <- sqrt(radius^2-x1^2)
radius <- 2.0
x2 <- seq(radius,-radius,length=2000)
y2 <- sqrt(radius^2-x2^2)

polygon(c(x1,x2),c(y1,y2))

(the graph much resembles a speed dial inside a car).
Now I want to fill the polygon with color, such that it starts on the 
left with red and ends on the right with green, following the coloring 
of the rainbow.
Preferably, the coloring should be "continuous", such that colors 
naturally fade into each other.
I can draw the polygon as above, but I don't know how to do the 
coloring. It is easy to give the polygon only one color (e.g. through 
polygon(c(x1,x2),c(y1,y2), col="red")), but I need a way in which to 
color the polygon such that the color moves through the color spectrum 
from red (left) to green (right).
Can anyone help me to achieve this?

Thanks, Roger



More information about the R-help mailing list