A Pie Chart is a special chart that shows relative sizes of data using pie slices.
They are good if you are trying to compare parts of a single data series to the whole.
The pie() function
In R, you can create a pie chart using the pie()
function.
It has many options and arguments to control many things, such as labels, titles and colors.
Syntax
The syntax for the pie()
function is:
pie(clockwise,init.angle,labels,density,angle,col,border,lty,main,…)
Parameters
Parameter | Description |
clockwise | If True, slices are drawn clockwise ortherwise counter-clockwise |
init.angle | The starting angle for the slices |
labels | The names for the slices |
density | The density of shading lines |
angle | The slope of shading lines |
col | A vector of colors to be used in filling or shading the slices |
border | The color to be used for the border |
lty | Type of lines used for plotting pie chart |
main | An overall title for the plot |
… | Other graphical parameters |
Create a Simple pie chart
To get started, you need a set of data to work with. Let’s consider a survey was conducted of a group of 190 individuals, who were asked “What’s your favorite fruit?”.
The result might appear as follows:
Fruit: | Apple | Kiwi | Grapes | Banana | Pears | Orange |
People: | 40 | 15 | 30 | 50 | 20 | 35 |
Let’s put this data in a vector.
survey <- c(apple=40, kiwi=15, grape=30, banana=50, pear=20, orange=35)
survey
apple kiwi grape banana pear orange
40 15 30 50 20 35
To create a pie chart just specify the vector in pie()
function.
# Create a pie chart from a vector of data points
pie(survey)
It is really a good way to show relative sizes: you can see which fruits are most liked, and which are not, at a glance.
Coloring a Pie Chart
You can change the colors of each pie slice by passing a vector of colors to the col argument.
# Change the colors of each pie slice
survey <- c(apple=40, kiwi=15, grape=30, banana=50, pear=20, orange=35)
pie(survey,
col=c("steelblue4", "steelblue", "steelblue3", "steelblue2", "steelblue1", "skyblue1"))
# Create a shaded pie chart
survey <- c(apple=40, kiwi=15, grape=30, banana=50, pear=20, orange=35)
pie(survey,
col=gray(seq(0.4, 1.0, length = 6)))
Labeling a Pie Chart with Percentage
Often you want to label each pie slice with the percentage of the whole that slice represents. You can do that by passing the precalculated percent values to the labels argument.
survey <- c(apple=40, kiwi=15, grape=30, banana=50, pear=20, orange=35)
pct <- round(survey/sum(survey)*100) # calculate percentages
lbls <- paste(names(survey), pct, "%") # add percents to labels
pie(survey,
col=c("steelblue4", "steelblue", "steelblue3", "steelblue2", "steelblue1", "skyblue1"),
labels=lbls)
If this argument is omitted, then the labels are taken from the names attribute of a vector.
Adding Hatches
Adding hatches to each pie slice is rather easy, just specify the density argument in the pie()
function.
By default the chart is hatched with 45° slanting lines, however, you can change it with the angle argument.
# Create a hatched pie chart with different slanting lines
survey <- c(apple=40, kiwi=15, grape=30, banana=50, pear=20, orange=35)
pie(survey,
col="steelblue",
density = 20,
angle = 30*1:6)
Pie Chart Start Angle and Direction
Use the init.angle and clockwise arguments to set the starting angle for the first segment in a pie chart, and the direction of the segments (clockwise or counter-clockwise).
By default, the init.angle is 0° (3 o’clock) and the direction of the segments is counter-clockwise.
If you change the direction of the segments to clockwise, the init.angle defaults to 90° (12 o’clock).
# Change the start angle to 90° and the direction of the segments to clockwise
survey <- c(apple=40, kiwi=15, grape=30, banana=50, pear=20, orange=35)
pie(survey,
col=c("steelblue4", "steelblue", "steelblue3", "steelblue2", "steelblue1", "skyblue1"),
init.angle = 90,
clockwise = TRUE)
3D Pie Chart
To create a 3D pie chart, use pie3D()
function of plotrix package and pass in the vector of data points.
You can alter the appearance of your 3D pie chart by using following parameters.
Parameter | Description |
col | A vector of colors to be used in filling slices |
main | An overall title for the plot |
labels | The names for the slices |
labelcex | The character expansion factor for the labels |
explode | The amount to explode the pie |
theta | The angle of viewing in radians |
edges | The number of lines forming an ellipse |
radius | The radius of the pie |
height | The height of the pie |
start | The angle at which to start drawing sectors |
border | The color of the sector border lines |
library(plotrix)
survey <- c(apple=40, kiwi=15, grape=30, banana=50, pear=20, orange=35)
pie3D(survey,
col=c("steelblue4", "steelblue", "steelblue3", "steelblue2", "steelblue1", "skyblue1"),
labels = names(survey),
labelcex = 1,
explode=0.1,
theta = 0.8,
main="3D Pie Chart")