[R] continuous coloring of a polygon

Roger Leenders r.t.a.j.leenders at rug.nl
Fri Aug 15 18:07:35 CEST 2008


Hi Aiste,

Thank you. It may be slow, but it is exactly what I was looking for! I'd 
be interested if anyone has a faster solution, but this already does the 
trick well.
Of course there will be additional elements in the figure, but it is the 
coloring I was struggling with.

thanks, Roger


Aiste Aistike schreef:
> Hello,
>  
> I slightly altered your code, but I hope that's what you want:
>  
> l <- 1000
> radius <- 3
> x <- seq(-radius,radius,length=l)
> 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=l)
> y1 <- sqrt(radius^2-x1^2)
> radius <- 2.0
> x2 <- seq(radius,-radius,length=l)
> y2 <- sqrt(radius^2-x2^2)
>
> for (i in 1:l){
>   
> polygon(c(x1[i],x1[i+1],x2[l-i],x2[(l+1)-i]),c(y1[i],y1[i+1],y2[l-i],y2[(l+1)-i]),
>           col = rainbow(1, start = 0+(0.33*i)/l, end = 2/6), 
> border=rainbow(1, start = 0+(0.33*i)/l))
> }
>
> #you can omit this line if you do not need the border:
> polygon(c(x1,x2),c(y1,y2))
>
> It's not ideal I think because for length l = 2000 it's quite slow. 
> Maybe someone will find a much better solution.
>  
>  
> Aiste
>  
> 2008/8/15 Roger Leenders <r.t.a.j.leenders at rug.nl 
> <mailto:r.t.a.j.leenders at rug.nl>>
>
>
>     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 <mailto: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
>     <http://www.r-project.org/posting-guide.html>
>     and provide commented, minimal, self-contained, reproducible code.
>
>



More information about the R-help mailing list