continuous coloring of a polygon

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

continuous coloring of a polygon

Roger Leenders

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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: continuous coloring of a polygon

Dieter Menne
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: continuous coloring of a polygon

Aiste Aistike
In reply to this post by Roger Leenders
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 <[hidden email]>

>
> 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
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: continuous coloring of a polygon

Roger Leenders

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 <[hidden email]
> <mailto:[hidden email]>>
>
>
>     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
>
>     ______________________________________________
>     [hidden email] <mailto:[hidden email]> 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.
>
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: continuous coloring of a polygon

Earl F. Glynn
In reply to this post by Roger Leenders
"Roger Leenders" <[hidden email]> wrote in message
news:[hidden email]...

> 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: continuous coloring of a polygon

Johannes Huesing
In reply to this post by Roger Leenders

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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: continuous coloring of a polygon

hadley wickham
In reply to this post by Roger Leenders
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 <[hidden email]> 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
>
> ______________________________________________
> [hidden email] 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/

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: continuous coloring of a polygon

Greg Snow-2
In reply to this post by Roger Leenders
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
[hidden email]
(801) 408-8111



> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of Roger Leenders
> Sent: Friday, August 15, 2008 6:01 AM
> To: [hidden email]
> 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
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: continuous coloring of a polygon

Roger Leenders
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
> [hidden email]
> (801) 408-8111
>
>
>
>  
>> -----Original Message-----
>> From: [hidden email]
>> [mailto:[hidden email]] On Behalf Of Roger Leenders
>> Sent: Friday, August 15, 2008 6:01 AM
>> To: [hidden email]
>> 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
>>
>> ______________________________________________
>> [hidden email] 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.
>>
>>    
>
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: continuous coloring of a polygon

barry rowlingson
2008/8/17 Roger Leenders <[hidden email]>:

> 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?
>

 add 'asp=1' to your plot() function to specify a 1:1 aspect ratio.
Should do the trick.

Barry

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: continuous coloring of a polygon

Roger Leenders

Thanks Barry, that works beautifully!

Roger


Barry Rowlingson schreef:

> 2008/8/17 Roger Leenders <[hidden email]>:
>  
>> 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?
>>
>>    
>
>  add 'asp=1' to your plot() function to specify a 1:1 aspect ratio.
> Should do the trick.
>
> Barry
>

______________________________________________
[hidden email] 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.