Applications of pie charts
I personally do not recommend the use of pie charts to present data analysis results, I will explain my reasons at the end of this section, however, they have some practical applications for other purposes, therefore I will show you how to make them.
par(mar=c(0.1, 0.1, 0.1, 0.1))
CYL_table = table(mtcars$cyl)
pander(as.matrix(CYL_table))| 4 | 11 |
| 6 | 7 |
| 8 | 14 |
Another information that can be accessed using the frequencies is the percentages. To obtain percentage, divide each value by the total number of observations, nrow(mtcars), and multiply it by 100.
# par(mfcol) to present figures using 1x2 grid
# par(mar) to change margins.
par(mfcol=c(1,2), mar=c(0, 0.7, 0, 0.7))
# First table
CYL_table = table(mtcars$cyl)
# Table calculating percentages
CYL_tableProb = CYL_table/nrow(mtcars)*100
# Basic plotting of pie charts
pie1 = pie(CYL_table,
labels = CYL_table,
radius = 0.8)
pie2 = pie(CYL_tableProb,
labels = CYL_tableProb,
radius = 0.8)
Create a label object
In this case, the paste() code is used to combine all the information required into one object, then the name of that object is used for labels = inside the pie code.
Pie Chart codes
Start with the paste() code, providing a name to the object that is being created, in this case: pieLabels, then add the following elements:
Observe the R Chunk below with the pieLabels object. On your console, call the pieLabels object and observe all its elements.
pieLabels = paste(unique(sort(mtcars$cyl)),
"Cyl",
"\n",
"n=",
sort(CYL_table),
"\n",
round(CYL_tableProb,1),
"%")
Now it’s time for a pie!
• Start with the code pie() and use any of the two tables, both produce the same figure.
• For labels, use the object we created: pieLabels, it will replace any label generated on the original figure.
• Add the other elements listed below, play with their values.
# par(mar) to change margins.
par(mar=c(0.1, 0.1, 0.1, 0.1))
pie(CYL_table,
labels = pieLabels,
radius = 0.6,
col = brewer.pal(ncol(mtcars), "Paired"),
border = "white",
lty = 1,
cex=0.9,
font = 3)
pie(CYL_table,
labels = pieLabels,
radius = 0.6,
col = terrain.colors(3),
border = "white",
lty = 1,
cex=0.8,
font = 3
)
legend("topleft",
legend = paste(unique(sort(mtcars$cyl)), "Cylinders"),
fill = terrain.colors(3),
border = "white")
box(col="red")# edges=1, 10, 20, etc., can also be used.
# on paste() for labels, sep = "" can be used to remove spaces.
# Check the use of order() instead of sort()
Keep in mind: The size of the areas in a pie chart are meant to indicate the relative size or proportion of each value. This is a strong reason why pie charts are not recommended to display data results, the human eye is not that good at judging relative areas. It is always preferable to use dot charts or bar graphs. Pie charts are quite good for advertisement/selling purposes.
Disclaimer: This short series manual project is a work in progress. Until otherwise clearly stated, this material is considered to be draft version.