[R] draw a circle with a gradient fill

Jim Lemon drjimlemon at gmail.com
Tue Oct 24 23:45:11 CEST 2017


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 <r-help at r-project.org> 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]]
>
> ______________________________________________
> R-help at r-project.org 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.



More information about the R-help mailing list