Data visualization in R: Pie charts

Before we start to look at the features and functions of pie charts let me introduce some thoughts about the benefits and cons of this chart type.

You will find a lot of discussions about this topic. Summary of these discussions: It is not recommended to use pie charts.

The reason is plausible: people are able to judge length more accurately than volume. So it is recommend using bar plots or dot plots over pie charts. But does this mean you should never use pie charts? In my opinion: No!

If you ignore pie charts you will miss a possibility to present your data. From a physiological point of view you should prefer bar plots because data shown in this format is easier to understand. But from psychological point of view it might be advantageous to use pie charts.

For example if you have to show data on management level then you can use pie charts. Management people love pie charts. In contrast if you want to show data to people on technical level you should use bar or dot plots. Technical people don’t like pie charts. Keep this background knowledge in mind and use pie charts rarely and at the right moment.

 
Simple pie chart

You may create a pie chart with the pie function by passing the vector of data and the vector of labels.

data <- c(5,6,3,8,2)
labels <- c(‚A‘,’B‘,’C‘,’D‘,’E‘)
pie(data, labels = labels)

R31_a

Pie Chart with percentages
At next we will at the percentages of each slice to the labels and use own colors.

data <- c(5,6,3,8,2)
labels <- c(‚A‘,’B‘,’C‘,’D‘,’E‘)
colors <- c(‚red‘,’green‘,’blue‘,’yellow‘,’orange‘)

percentage <- round(data/sum(data)*100)
labels <- paste(labels, ‚ (‚, percentage, ‚%)‘, sep=“)

pie(data, labels = labels, col = colors)

R31_b
Title and legend

In the final example I want to add a title and a legend. To show the legend we will decrease the size of the pie chart by using the radius parameter.

data <- c(5,6,3,8,2)
labels <- c(‚A‘,’B‘,’C‘,’D‘,’E‘)
colors <- c(‚red‘,’green‘,’blue‘,’yellow‘,’orange‘)

percentage <- round(data/sum(data)*100)
percentage <- paste(percentage, ‚%‘, sep=“)

pie(data, labels = percentage, col = colors, radius = 0.8, main = ‚My pie chart‘)
legend(‚topright‘, labels, fill = colors)

R31_c

Advertisements
Dieser Beitrag wurde unter R veröffentlicht. Setze ein Lesezeichen auf den Permalink.

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s