[R] continuous coloring of a polygon

Greg Snow Greg.Snow at imail.org
Fri Aug 15 22:31:25 CEST 2008


Here are a couple of other solutions, use whichever works best for you (after modifications, changing increments, etc.).

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)

#tmp <- rainbow(1000, start=2/6, end=0/6)
tmp <- rev(rainbow(1000, start=0/6, end=2/6))
theta <- seq(pi, 0, length=1000)

segments(2.7*cos(theta),2.7*sin(theta),
                2.0*cos(theta), 2.0*sin(theta), col=tmp, lwd=2)

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


library(TeachingDemos)



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)



tmpfun <- function(...){
   image( seq(-3,3,length=101), c(-3.24,3.24), matrix( 1:100, ncol=1 ),
        col=rev(rainbow(100,start=0, end=1/3)), add=TRUE )
}

top <- approxfun( x1, y1 )
bottom <- approxfun( c(-3, x2, 3), c(min(y2), y2, min(y2) ) )

xx <- seq(-2.7,2.7, length.out=1000)
xxx <- embed(xx,2)[,2:1]
for(i in 1:999) {
        clipplot(tmpfun(), xxx[i,], c( min(bottom(xxx[i,])), max(top(xxx[i,]))) )
}

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





Hope this helps,

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
(801) 408-8111



> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Roger Leenders
> Sent: Friday, August 15, 2008 6:01 AM
> To: r-help at r-project.org
> Subject: [R] continuous coloring of a polygon
>
>
> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list