draw a circle with a gradient fill

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

draw a circle with a gradient fill

R help mailing list-2
Hi all,I would like to draw a simple circle where the color gradient follows the rule color = 1/(r^2) where r is the distance from the circle. I would also like to add a color bar with values going from -40 to -110 (and associate those with the color gradient that fills the circle).
So far I experiemented with draw circle install.packages('plotrix')require(plotrix)colors<-c('#fef0d9','#fdcc8a','#fc8d59','#e34a33','#b30000')plot(c(-15,15),c(-15,15),type='n')draw.circle(0, 0, 10, nv = 1000, border = NULL, col = colors[1], lty = 1, lwd = 1)draw.circle(0, 0, 8, nv = 1000, border = NULL, col = colors[2], lty = 1, lwd = 1)draw.circle(0, 0, 6, nv = 1000, border = NULL, col = colors[3], lty = 1, lwd = 1)draw.circle(0, 0, 4, nv = 1000, border = NULL, col = colors[4], lty = 1, lwd = 1)draw.circle(0, 0, 2, nv = 1000, border = NULL, col = colors[5], lty = 1, lwd = 1)

but this package does not give easily color gradients and so my solutions contains 5 same colors filled rings.
Will there be any suggested improvements on my code above?
Thanks a lotAlex
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: draw a circle with a gradient fill

Bert Gunter-2
There are lots of different ways to do this. I don't use plotrix, which
uses base R graphics functionality, so I can't help you with your current
approach. However, it is easy to do this "by hand" in the newer,
object-based grid package (on which ggpot and lattice are built) . The
idea, which probably works for plotrix, too, is to simply draw concentric
circles of *decreasing* radiii with a gradient of fill colors; each
successive fill will overwrite the previous one.

Here's some code using grid that you can customize that gives you the idea.
Obviously, you'd also have to add your color bar using grid graphics
functionaity, which is again straightforward.

grid.newpage()
mycols<- colorRampPalette(c("yellow","orange","red"))
radii = .3*seq(1,.05,length.out = 25)
grid.circle(x=.5,y=.5, r = radii,
         gp= gpar(fill=mycols(25), lwd = 0))

However, probably plotrix and ggplot would allow you to do this more easily
with higher level graphics funtions. Searching (e.g. on rseek.org) might
bring up what you want if you don't get a more satisfactory reply here.

Finally, **Please** post in plain text, not HTML. As you can see from the
quoted message below, your text got mangled. This, of course, will
discourage others to give you the response you need.

Cheers,
Bert




Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Tue, Oct 24, 2017 at 2:56 AM, Alaios via R-help <[hidden email]>
wrote:

> Hi all,I would like to draw a simple circle where the color gradient
> follows the rule color = 1/(r^2) where r is the distance from the circle. I
> would also like to add a color bar with values going from -40 to -110 (and
> associate those with the color gradient that fills the circle).
> So far I experiemented with draw circle install.packages('plotrix')
> require(plotrix)colors<-c('#fef0d9','#fdcc8a','#fc8d59','#
> e34a33','#b30000')plot(c(-15,15),c(-15,15),type='n')draw.circle(0, 0, 10,
> nv = 1000, border = NULL, col = colors[1], lty = 1, lwd = 1)draw.circle(0,
> 0, 8, nv = 1000, border = NULL, col = colors[2], lty = 1, lwd =
> 1)draw.circle(0, 0, 6, nv = 1000, border = NULL, col = colors[3], lty = 1,
> lwd = 1)draw.circle(0, 0, 4, nv = 1000, border = NULL, col = colors[4], lty
> = 1, lwd = 1)draw.circle(0, 0, 2, nv = 1000, border = NULL, col =
> colors[5], lty = 1, lwd = 1)
>
> but this package does not give easily color gradients and so my solutions
> contains 5 same colors filled rings.
> Will there be any suggested improvements on my code above?
> Thanks a lotAlex
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: draw a circle with a gradient fill

Jim Lemon-4
In reply to this post by R help mailing list-2
Hi Alex,
This is harder than it looks for a number of reasons. First you want
to desaturate the colors as you move out, which requires something
more sophisticated than the approximation I have done in the code
below. RGB may not be the best colorspace to use for this. Second is
that when you draw a circle, it isn't a circle, it's a polygon.
Therefore you have to fiddle with the line width and spacing to get it
to look smooth. I hope that this example is helpful, but I am not sure
that it illustrates what you want:

library(plotrix)
plot(c(-15,15),c(-15,15),type="n")
ccolors<-rep(NA,60)
cindex<-1
for(rad in seq(10,0.1,length.out=60)) {
 r<-floor(179+76*rad*rad/10000)
 g<-b<-floor(2*rad*rad)
 ccolors[cindex]<-rgb(r/255,g/255,b/255)
 draw.circle(0,0,rad,border=ccolors[cindex],col=NA,
  lwd=3,nv=100+rad*10)
 cindex<-cindex+1
}
ccolors
color.legend(2,-15,15,-13,legend=seq(-40,-110,length.out=5),
 rect.col=ccolors[c(1,22,35,47,60)])

Jim

On Tue, Oct 24, 2017 at 8:56 PM, Alaios via R-help <[hidden email]> wrote:

> Hi all,I would like to draw a simple circle where the color gradient follows the rule color = 1/(r^2) where r is the distance from the circle. I would also like to add a color bar with values going from -40 to -110 (and associate those with the color gradient that fills the circle).
> So far I experiemented with draw circle install.packages('plotrix')require(plotrix)colors<-c('#fef0d9','#fdcc8a','#fc8d59','#e34a33','#b30000')plot(c(-15,15),c(-15,15),type='n')draw.circle(0, 0, 10, nv = 1000, border = NULL, col = colors[1], lty = 1, lwd = 1)draw.circle(0, 0, 8, nv = 1000, border = NULL, col = colors[2], lty = 1, lwd = 1)draw.circle(0, 0, 6, nv = 1000, border = NULL, col = colors[3], lty = 1, lwd = 1)draw.circle(0, 0, 4, nv = 1000, border = NULL, col = colors[4], lty = 1, lwd = 1)draw.circle(0, 0, 2, nv = 1000, border = NULL, col = colors[5], lty = 1, lwd = 1)
>
> but this package does not give easily color gradients and so my solutions contains 5 same colors filled rings.
> Will there be any suggested improvements on my code above?
> Thanks a lotAlex
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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 -- To UNSUBSCRIBE and more, see
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: draw a circle with a gradient fill

Berry, Charles
In reply to this post by R help mailing list-2

> On Oct 24, 2017, at 2:56 AM, Alaios via R-help <[hidden email]> wrote:
>
> Hi all,I would like to draw a simple circle where the color gradient follows the rule color = 1/(r^2) where r is the distance from the circle.

This is called a radial gradient fill in SVG speak.

[snip]

> but this package does not give easily color gradients and so my solutions contains 5 same colors filled rings.


I would look at the gridSVG package. Examples of various gradient fills are here:

https://www.stat.auckland.ac.nz/~paul/Reports/leaf/leaf.html#idp625472

HTH,

Chuck
______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.