sparandekar at worldbank.org
2007-Jul-24 19:40 UTC
[R] How to add circular text for a graph with concentric circles
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. Thank you very much, best regards, Suhas # Conurbano1a.R # R Program to generate circles, radii based on proportion of ALL STUDENTS # Prepared by Suhas, Tuesday, July 24, 2007 grid.circle(x=0.5, y=0.5, r=3*(.1268), default.units="npc", name=NULL, gp=gpar(fill="olivedrab1",col=NULL), draw=TRUE, vp=NULL) grid.circle(x=0.5, y=0.5, r=3*(0.095882), default.units="npc", name=NULL, gp=gpar(fill="cornflowerblue",col=NULL), draw=TRUE, vp=NULL) grid.circle(x=0.5, y=0.5, r=3*(0.077894), default.units="npc", name=NULL, gp=gpar(fill="aliceblue",col=NULL), draw=TRUE, vp=NULL) grid.circle(x=0.5, y=0.5, r=3*(0.061884), default.units="npc", name=NULL, gp=gpar(fill="seagreen",col=NULL), draw=TRUE, vp=NULL) grid.circle(x=0.5, y=0.5, r=3*(0.050740), default.units="npc", name=NULL, gp=gpar(fill="lightpink2",col=NULL), draw=TRUE, vp=NULL) grid.circle(x=0.5, y=0.5, r=3*(0.045906), default.units="npc", name=NULL, gp=gpar(fill="navy",col=NULL), draw=TRUE, vp=NULL) # Now to add the labels ? used trial and error for x and y values grid.text("Provincia Interior", x=0.5, y=0.85, gp=gpar(fontsize=6, col="black")) grid.text("Conurbano 4", x=0.5, y=0.75, gp=gpar(fontsize=6, col="black")) grid.text("Conurbano 3", x=0.5, y=0.71, gp=gpar(fontsize=6, col="black")) grid.text("Conurbano 2", x=0.5, y=0.67, gp=gpar(fontsize=6, col="white")) grid.text("Conurbano 1", x=0.5, y=0.645, gp=gpar(fontsize=6, col="black")) grid.text("Ciudad de Buenos Aires", x=0.5, y=0.56, gp=gpar(fontsize=6, col="white")) # Title of graph grid.text("All Students", x=0.2,y=0.95,gp=gpar(fontsize=20,col="navy")) ************************************************************************** Suhas D. Parandekar Senior Education Economist Latin American and Caribbean Region Tel: 202 458 7622 e-mail: sparandekar at worldbank.org
Jim Lemon
2007-Jul-25 11:38 UTC
[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