[R] How to add circular text for a graph with concentric circles

Jim Lemon jim at bitwrit.com.au
Wed Jul 25 13:38:36 CEST 2007


sparandekar at worldbank.org wrote:
> Dear R experts,
> 
> I am plotting the population of students who live in a city, and in 
> successive circular bands made of the contiguous districts that surround 
> the city. This is a stylized figure, where I specify the area of each 
> successive circle based on the cumulative population of students. I want 
> to compare two sets of concentric circles across different populations - 
> such as 'All students' and 'Private students' (those attending private 
> school) by using the same colours and the same dimension of the outer 
> circle. I have attached the .pdf file with the output, and the R code to 
> generate the first set of circles.
> 
> I would appreciate any tips about how to rotate the text label that marks 
> each concentric circle (except the central circle) to be curved around the 
> circle, located in the middle of each band, thus following the circle 
> instead of being horizontal, as I have it now.
> 
Hi Suhas,
This is kind of rough, being rejigged from an old piece of Postscript 
code that I wrote to get text in an arc. You specify the text and 
whatever else is needed as in the following example:

arctext<-function(x,center=c(0,0),radius=1,
  midangle=pi/2,stretch=1.1,cex=1,...) {
  oldcex<-par("cex")
  par(cex=cex)
  xwidth<-strwidth(x)*stretch
  startpos<-midangle+xwidth/(radius*2)
  xvec<-strsplit(x,"")[[1]]
  xwidths<-rep(mean(stretch*strwidth(xvec)),length(xvec))
  arcpos<-startpos-cumsum(xwidths)
  for(i in 1:length(arcpos))
   text(center[1]+radius*cos(arcpos[i]),center[2]+radius*sin(arcpos[i]),
    xvec[i],adj=c(0.5,0.5),srt=(arcpos[i]-midangle)*180/pi)
  par(cex=oldcex)
}

plot((1:5)
arctext("bendy as a bloody piece of spaghetti",center=c(3,3))

radius is obviously the radius of the arc,
midangle is where you want the center of the text,
stretch is how much to stretch the text to make it look nice
and cex is the expansion factor.
You will probably notice that I kludged the spacing by making
it monospaced. I'll have a try at getting proportionally spaced
text to work right when I get a chance.

Jim



More information about the R-help mailing list