[R] continuous coloring of a polygon

Roger Leenders r.t.a.j.leenders at rug.nl
Sun Aug 17 11:20:38 CEST 2008


WinXP, R2.7.1

Thanks so much to all who have reponded to my inquery. The solutions are 
most helpful.
There is only one final thing that I can't seem to get right.
All of the proposed solutions yield a figure that is not quite round, 
but is wider than its height. This can be resolved by manually resizing 
the window in which it is plotted, but that becomes cumbersome when many 
of these figures need to be generated. Is there a way to enforce the 
figure to be perfectly round?
I have tried plotting directly to a windows device (using for example 
the code below) or to a pdf-device in which I specifically specify the 
square size of the plot, but the situation remains in which the figure 
is wider than it is high. Does anyone know how to solve this final piece 
of the puzzle?

Thanks, Roger


example code:

#pdf(file = ifelse(onefile, "Rplots.pdf", "Rplot%03d.pdf"), 
width=4,height=4,family="Palatino", title=c("De speedometer"), fonts = 
"AvantGarde",paper = "a4")

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)

tmp <- 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)
dev.off()




Greg Snow schreef:
> 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