Posted on by & filed under Content - Highlights and Reviews, design, Programming & Development.

code Tom Barker is a software engineer, an engineering manager, a professor and an author. Currently he is Director of Software Engineering and Development at Comcast, and an Adjunct Professor at Philadelphia University. He has authored Pro JavaScript Performance: Monitoring and Visualization, Pro Data Visualization with R and JavaScript, and Technical Management: A Primer, and can be reached at @tomjbarker.

Continuing the series of posts that I’ve been writing, focusing on data visualization and specifically different types of data visualizations in R, this post will focus on pie charts.

A pie chart is a circle that is sliced into wedges. The circle represents the entirety of a data set, or the whole. The wedges demonstrate how the whole is divided into parts.

Talking about the history of the pie chart will point back to familiar territory – once again William Playfair gets credit for creating the pie chart in his Statistical Breviary published in 1801. Playfair created his first pie chart to show the landholdings of the Ottoman Empire across Asia, Africa, and Europe. See below for Playfair’s pie charts in his Statistical Breviary.

fig01

Another historically significant pie chart is the Diagram of the Causes of Mortality in the Army in the East, by Florence Nightingale and William Farr. You can see this chart here:

fig02

Nightingale and Farr created this chart in 1856 to demonstrate the relative amount of preventable deaths, and at a higher level, to improve the sanitary conditions of military installations.

Today, pie charts are fairly ubiquitous in the business world, but there is a running debate in the world of data visualization around their usefulness compared to other types of charts. Stephen Few heads up the opposition to pie charts – the main point is that differences in angles are harder for the eye to discern (outside of the 25%, 50%, and 75% intervals) than differences in lengths of rectangles, so bar charts can generally be of better use when you are inclined to use a pie chart. See here for a deeper and more thorough analysis of the pie chart from Few. Even the creators of R are not fans of pie charts, as evidenced by the documentation for the pie function in R:

“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.”

Cleveland (1985), page 264: “Data that can be shown by pie charts always can be shown by a dot chart. This means that judgements of position along a common scale can be made instead of the less accurate angle judgements.” This statement is based on the empirical investigations of Cleveland and McGill as well as investigations by perceptual psychologists.

But there are contrary opinions out there as well. The high level points can be summed as audiences are more emotionally receptive to rounded edges than straight edges, and pie charts are more obviously demonstrating parts of a whole, where as a bar chart just compares discrete values not necessarily parts of a whole. Bruce Gabrielle talks to these points here.

Regardless of your leanings in regard to pie charts, they are widely used in the business world and it would be remiss to not have them be part of your available toolset.

So how do you craft pie charts in R?

R supports pie charts using the pie function. The pie function can accept, among other parameters, a vector of numbers and a list of strings to use as labels. Let’s take a look. To demonstrate, we’ll create a vector of numbers and a list of strings that correspond to some numbers. Our hypothetical data set could correspond to how many days we’ve spent vacationing by state. We will pass both of these lists to the pie function.

This produces the chart shown here:

fig03

That’s about it for built in pie chart functionality (remember the authors of the language lean to the anti-pie chart side of the spectrum). But there are some packages that you can install that allow significantly more options for creating pie charts, and the best of the bunch is plotrix.

You can install plotrix like so:

To use it you simply reference it via the library function:

The plotrix library supports the fan chart, which is a derivation of the pie chart, just splayed horizontally. You can create fan charts with the fan.plot function in plotrix. Let’s take a look at the fan plot using the same hypothetical data that we created above:

This creates the fan plot shown here:

fig04

Plotrix also supports 3D pie charts using the pie3D function. 3D pie charts are exactly what they sound like, 3-dimensional representation of a pie chart. But the fun thing with 3D pie charts is that they can be exploded, so that the pieces separate on the chart.

The pie3D function accepts a list of numbers, and a list of labels, but also among other things an explode parameter that specifies how far apart to separate the pie pieces. See below for a the code to produce a 3D pie chart:

This creates the chart shown here:

fig05

Another fun parameter for the pie3D function is theta, which specifies the viewing angle of the 3D pie chart. Be warned, though, that adding additional dimensions to charts in general makes them harder to read, or discern the exact differences in comparisons.

But remember the chart that Florence Nightingale and William Farr created? That’s actually called a polar area chart, and it differs semantically from a pie chart in that the angle of the wedge in a pie chart represents the value, but the distance from the center of the pie indicates the value in a polar area chart.

Plotrix also supports polar area charts. To create a polar area chart, use the radial.pie function:

This creates the chart shown here:

fig06

Conclusion

Whatever you feelings toward pie charts, they are an interesting chart with a storied history, and they are a widely used paradigm in the business world.

For more details about R, see the resources below from Safari Books Online.

Not a subscriber? Sign up for a free trial.

Safari Books Online has the content you need

R for Everyone: Advanced Analytics and Graphics shows how by using the open source R language, you can build powerful statistical models to answer many of your most challenging questions. R has traditionally been difficult for non-statisticians to learn, and most R books assume far too much knowledge to be of help. R for Everyone is the solution. You’ll download and install R; navigate and use the R environment; master basic program control, data import, and manipulation; and walk through several essential tests. Then, building on this foundation, you’ll construct several complete models, both linear and nonlinear, and use some data mining techniques.
The Art of R Programming is both broad in its coverage of various language constructs and data structures, and deep and co mprehensive in explaining them. It provides working examples, and iluminates the R philosophy: a clean functional language with strong vector operation support, and a “do more with less typing” foundation that can make programs an order of magnitude smaller and expressive.
Pro Data Visualization using R and JavaScript by Tom Barker, makes the R language approachable, and promotes the idea of data gathering and analysis. You’ll see how to use R to interrogate and analyze your data, and then use the D3 JavaScript library to format and display that data in an elegant, informative, and interactive way. You will learn how to gather data effectively, and also how to understand the philosophy and implementation of each type of chart, so as to be able to represent the results visually.
Pro JavaScript Performance: Monitoring and Visualization by Tom Barker, gives you the tools to observe and track the performance of your web applications over time from multiple perspectives, so that you are always aware of, and can fix, all aspects of your performance.
Learning R will help you learn how to perform data analysis with the R language and software environment, even if you have little or no programming experience. With the tutorials in this hands-on guide, you’ll learn how to use the essential R tools you need to know to analyze data, including data types and programming concepts.

Tags: Farr, Nightingale, Pie Charts, R, Statistical Breviary, William Playfair,

Comments are closed.