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
Roger Leenders <r.t.a.j.leenders <at> rug.nl> writes:> > I have a polygon inside a circle as follows: >.. Example code removed (but it's nice you included it)> (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.What comes closest is function gradient.rect in package plotrix. You could use it as an starting example, and replace the rect() in it by polygon computed in polar coordinates. Dieter
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@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@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. >[[alternative HTML version deleted]]
"Roger Leenders" <r.t.a.j.leenders at rug.nl> wrote in message news:48A56FE6.60304 at rug.nl...> 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.I'd recommend using polar coordinates, like shown below. Instead of 2000 points, 200 or even 100 seem to be enough. Break your arc (polygon) into smaller trapezoids and color them with a continuously changing palette: red.to.green <- rainbow(N, start=0, end=0.35)) N <- 200 # points used for a full circle radius <- 3 theta <- seq(0.0, 2*pi, length.out=N) # Plot circle plot(radius*cos(theta), radius*sin(theta), xlim=c(-radius,radius),ylim=c(-radius,radius), type="l", ylab="", xlab="", axes=F) # Break arc into filled trapezoids radius.outer <- 2.7 radius.inner <- 2.0 N2 <- N / 2 theta <- seq(pi, 0.0, length.out=N/2) red.to.green <- rainbow(N2, start=0, end=0.35) for (i in 1:(N2-1)) { x1 <- radius.outer*cos(theta[i]) x2 <- radius.outer*cos(theta[i+1]) x3 <- radius.inner*cos(theta[i+1]) x4 <- radius.inner*cos(theta[i]) y1 <- radius.outer*sin(theta[i]) y2 <- radius.outer*sin(theta[i+1]) y3 <- radius.inner*sin(theta[i+1]) y4 <- radius.inner*sin(theta[i]) polygon( c(x1, x2, x3, x4, x1), c(y1, y2, y3, y4, y1), col=red.to.green[i], border=NA) } # Draw outline of arc x.outer <- radius.outer * cos(theta) y.outer <- radius.outer * sin(theta) x.inner <- radius.inner * cos(theta) y.inner <- radius.inner * sin(theta) polygon(c(x.outer, rev(x.inner), x.outer[1] ), c(y.outer, rev(y.inner), y.outer[1] ) ) efg Earl F Glynn Stowers Institute for Medical Reseach
Am 15.08.2008 um 14:00 schrieb Roger Leenders:> 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? >I don't know of another way than to segment the polygon and give the segments distinctive colours. The function rainbow(n, start=0, end=1/3) might help specifying the colours, where n is chosen sufficiently high.
It rather sounds like you might be designing a gauge display for a dashboard. If that's the case, I'd recommend checking out: Information Dashboard Design: The Effective Visual Communication of Data Stephen Few http://www.amazon.com/dp/0596100167 as well as some of his online writings: http://www.perceptualedge.com/articles/dmreview/dashboard_design.pdf http://www.perceptualedge.com/articles/visual_business_intelligence/pervasive_hurdles_to_dd.pdf http://www.perceptualedge.com/blog/?p=102 Hadley On Fri, Aug 15, 2008 at 7:00 AM, Roger Leenders <r.t.a.j.leenders at rug.nl> wrote:> > 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. >-- http://had.co.nz/
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. >