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

sparandekar at worldbank.org sparandekar at worldbank.org
Wed Jul 25 16:23:53 CEST 2007


Thanks, Jim. I tried to implement this but the text swings all over the place. I
had thought that there could be a function where I could specify the circle's
origin, radius and angle of the location of the starting letter, then, based on
a specified font size and letter spacing, the arc could be plotted out. But
clearly, the problem is not as small as I thought, and it appears that I need to
spend some time learning more of the basics  before attempting something this
complicated. Thanks for your help anyways.

best regards,
Suhas



                                                                                
             Jim Lemon                                                          
             <jim at bitwrit.co                                                    
             m.au>                                                           To 
                                     sparandekar at worldbank.org                  
             07/25/2007                                                      cc 
             07:38 AM                r-help at stat.math.ethz.ch                   
                                                                        Subject 
                                     Re: [R] How to add circular text for a     
                                     graph with concentric circles              
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                




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