# [R] squared "pie chart" - is there such a thing?

Jim Lemon jim at bitwrit.com.au
Tue Jul 26 13:26:14 CEST 2011

```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

```