[R] squared "pie chart" - is there such a thing?
(Ted Harding)
ted.harding at wlandres.net
Tue Jul 26 14:03:46 CEST 2011
On 26-Jul-11 11:26:14, Jim Lemon wrote:
> On 07/26/2011 02:40 AM, Naomi Robbins wrote:
>> Hello!
>> It's a shoot in the dark, but I'll try. If one has a total of 100
>> (e.g., %), and three components of the total, e.g.,
>> mytotal=data.frame(x=50,y=30,z=20), - one could build a pie chart with
>> 3 sectors representing x, y, and z according to their proportions in
>> the total.
>> I am wondering if it's possible to build something very similar, but
>> not on a circle but in a square...
>
>
> Is this still going on? Okay, here's a square pie:
>
> squarePie<-function(slices,x0=0,y0=0,x1=1,y1=1,firstcall=TRUE,pos=1,
> col=NULL,show.values=TRUE,...) {
> sliceprop<-slices[1]/sum(slices)
> if(firstcall) {
> oldmar<-par(mar=c(2,2,3,2))
>
> plot(0,xlim=c(0,1),ylim=c(0,1),xaxs="i",yaxs="i",type="n",axes=FALSE,
> xlab="",ylab="",...)
> if(is.null(col)) col=rainbow(length(slices))
> }
> if(pos == 1) {
> ytop<-y0+(y1-y0)*sliceprop
> rect(x0,y0,x1,ytop,col=col[1])
> if(show.values) text((x0+x1)/2,(y0+ytop)/2,slices[1])
> y0<-ytop
> }
> if(pos == 2) {
> xright<-x0+(x1-x0)*sliceprop
> rect(x0,y0,xright,y1,col=col[1])
> if(show.values) text((x0+xright)/2,(y0+y1)/2,slices[1])
> x0<-xright
> }
> if(pos == 3) {
> ybottom<-y1-(y1-y0)*sliceprop
> rect(x0,ybottom,x1,y1,col=col[1])
> if(show.values) text((x0+x1)/2,(ybottom+y1)/2,slices[1])
> y1<-ybottom
> }
> if(pos == 4) {
> xleft<-x1-(x1-x0)*sliceprop
> rect(xleft,y0,x1,y1,col=col[1])
> if(show.values) text((xleft+x1)/2,(y0+y1)/2,slices[1])
> x1<-xleft
> }
> pos<-ifelse(pos==4,1,pos+1)
> if(length(slices > 1))
>
> squarePie(slices[-1],x0,y0,x1,y1,firstcall=FALSE,pos=pos,col=col[-1])
> if(firstcall) par(oldmar)
> }
>
> squarePie(c(4,3,6,2,5,1),main="A square meal from Oz")
>
> Jim
A very nice implementation, Jim! However, its existence allows me
to test some perceptual concerns I have had about the "square pie
chart" concept -- not unlrelated to the one we all have about
ordinary pie charts, but somewhat different in nature.
Namely, it is interesting to comare the impressions one forms of:
squarePie(c(10,9,8,7,6,5,4,3,2,1),main="A square meal from Oz")
X11()
squarePie(c(1,2,3,4,5,6,7,8,9,10),main="A square meal from Oz")
Possibly there may be scope for additional flexibility regarding
the order in which the rectangles are placed in the chart. I know
the user can select this by re-arranging 'slices', but maybe the
function itself could try to be "intelligent" about this (though
I'm at a loss at the moment to suggest what the basis for this
should be)!
Best wishes,
Ted.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <ted.harding at wlandres.net>
Fax-to-email: +44 (0)870 094 0861
Date: 26-Jul-11 Time: 13:03:43
------------------------------ XFMail ------------------------------
More information about the R-help
mailing list