Pie charts are very popular in business data reporting. However, they are not preferred by scientists and are often criticized for being hard to read and obscuring data. In this recipe, we will learn how to make better pie charts by ordering the slices by size.
In this recipe, we will use the browsers.txt
example dataset that contains data about the usage percentage share of different Internet browsers.
First, we will load the browsers.txt
dataset and then use the pie()
function to draw a pie chart:
browsers<-read.table("browsers.txt",header=TRUE) browsers<-browsers[order(browsers[,2]),] pie(browsers[,2], labels=browsers[,1], clockwise=TRUE, radius=1, col=brewer.pal(7,"Set1"), border="white", main="Percentage Share of Internet Browser usage")
The important thing about the graph is that the slices are ordered in the ascending order of their sizes. We have done this because one of the main criticisms of pie charts is that, when there are many slices and they are in a random order, it is not easy (often impossible) to tell whether one slice is bigger than another. By ordering the slices by size in a clockwise direction, we can directly compare the slices.
We ordered the dataset by using the order()
function, which returns the index of its argument in the ascending order. So, if we just type in order(browsers[,2])
at the R prompt, we get the following output:
[1] 7 6 5 3 2 1 4
This is a vector of the index of the share values in the ascending order in the original dataset. For example, Firefox, which has the largest share, is in the fourth row, so the last number in the vector is 4
. We then use the index to reassign the browser dataset in the ascending order of share by using the square bracket notation.
Then, we pass the share values in the second column as the first argument to the pie()
function of the base R graphics library. We set labels to the first column, the names of browsers (note that IE stands for Internet Explorer). We also set the clockwise
argument to TRUE
. By default, slices are drawn counterclockwise.
3.14.251.248