27
Nov

## I give up, I am embracing pie charts

754

Most statisticians know that pie charts are a terrible way to plot percentages. You can find explanations here, here, and here as well as the R help file for the `pie` function which states:

Pie charts are a very bad way of displaying information. The eye is good at judging linear measures and bad at judging relative areas. A bar chart or dot chart is a preferable way of displaying this type of data.

I have only used the `pie` R function once and it was to make this plot (R code below):

So why are they ubiquitous? The best explanation I've heard is that they are easy to make in Microsoft Excel. Regardless, after years of training, lay people are probably better at interpreting pie charts than any other graph. So I'm surrendering and embracing the pie chart. Jeff's recent post shows we have bigger fish to fry.

``` N=10 for(i in 0:(N-1)){ x=cos(2*pi/N*i);y=x+1;z=(y-2)*22.5 bitmap(sprintf("Rplot%02d.png",i)) pie(c(y,8-y),col=c("white","yellow"),init.angle=135-z,border=FALSE,labels=NA) dev.off() } system("convert Rplot*.png pacman.gif") ##system("rm *.png") edited to save caffo's pngs (see comments) system("rm Rplot*.png") ```

• http://yihui.name/ Yihui Xie

You can throw the loop into the animation package and produce the GIF animation directly:

library(animation)
saveGIF({
N=10
for(i in 0:(N-1)){
x=cos(2*pi/N*i);y=x+1;z=(y-2)*22.5
pie(c(y,8-y),col=c("white","yellow"),init.angle=135-z,border=FALSE,labels=NA)
}
}, 'pacman.gif', nmax = 10, interval = .1)

And I recommend next time before you post code, run formatR::tidy.source().

• Kasper D Hansen

The signature of Rafa's code is complete absence of any space (apart from leading), variable names that are no longer than one character, and liberal use of ; to have several expressions on one line. Formatting it would cramp his style.

• http://yihui.name/ Yihui Xie

Got it. It is good to do some brain exercise once in a while.

• Andrew Jaffe

• Kasper Daniel Hansen

I disagree on those two points. Rafa has as good comments in his code as anyone I have seen (granted, we could all comment more). And I have never seen an overuse of Reduce(lapply(sappy())).

• Roger Peng

I'll start using 'tidy.source()' when the default for 'reindent.spaces' is 8

• Brian Caffo

8 spaces? C'mon, now this is just becoming attention seeking behavior.

• http://yihui.name/ Yihui Xie

An eye for an eye, a space for a space

• Brian Caffo

PSA, don't run Rafa's code in an R working directory that has other png files that you'd like to keep. Also, if "convert" fails, you don't have image magic installed or in the path.

• Rafael Irizarry

also be careful if you have a file called pacman.gif that you dont want overwritten!

• Brian Caffo

Good catch. A surprisingly common occurrence for me.

• http://yihui.name/ Yihui Xie

These are addressed by animation::saveGIF() -- it will try to find ImageMagick under Windoze, and generate images in tempdir() by default.

• Andrew Jaffe

try throwing this in that for loop for some extra fun

``` points(0,0.5, pch = 19, col = "black", cex=4) if(i %in% 0:4) { points(-0.5,0, col = "red", pch = 19,cex=12) lines(c(-0.65, -0.8, -0.9), c(0, 0.4,0.2), lwd = 5, col="red") } ```

• Kasper D Hansen

Btw., best use of pie charts I have seen so far.

• Pingback: Life of Pie | make a powerful point()

• Pingback: Pros Write()