Moving on to pie charts, we will learn how to add custom label text to a pie's slices:
labels
argument to add percentages to the pie chart.> #modify the chapter 8 pie chart that depicted the gold cost of the fire attack in relation to the total funds allotted to the Shu army > #use the labels argument to add percentages to a pie chart > #create a vector containing the labels to be used for the pie's slices > pieFireGoldCostLabelsPercent <- round(pieFireGoldCostSlices / sum(pieFireGoldCostSlices) * 100, 1) > #use the paste(...) function to add a percent sign (%) to the end of each label > pieFireGoldCostLabelsPercent <- paste(pieFireGoldCostLabelsPercent, "%", sep="") > #note that paste(...) can be used to add any kind of text before or after a label > #use the pie(...) function to create and display the pie chart > pie(x = pieFireGoldCostSlices, labels = pieFireGoldCostLabelsPercent, main = pieFireGoldCostMain, col = pieFireGoldCostSpecificColors)
> #add a legend to the pie chart > legend(x = "bottom", legend = pieFireGoldCostLabels, fill = pieFireGoldCostSpecificColors)
We just customized our pie chart by taking advantage of a new labeling option. Let us discuss how this feature is implemented.
We revised our pie chart's labels to display percentage values, rather than raw gold amounts. To accomplish this, we calculated the necessary percentages using the round(x, digits)
function in tandem with some routine mathematics. In the round(x, digits)
function x
is a number, and digits
is the number of decimal places that x
should be rounded to.
Therefore, rounding the number 1.2345 using:
> round(1.2345, 2)
The code would yield an output of:
[1] 1.23
For our chart, x
contained a formula that yielded the percentage that each slice represents out of our total. The digits
argument dictated that this percentage be rounded to a single decimal point:
> pieFireGoldCostLabelsPercent <- round(pieFireGoldCostSlices / sum(pieFireGoldCostSlices) * 100, 1)
To improve the readability of our percentages, we then used the paste(...)
function to append a percent sign (%) to each of our labels. In our activity, the paste(...)
function included the following arguments:
Hence, the general paste(...)
function takes on the following form:
paste(originalValues, appendText, sep = "sep")
Thus, if we were to enter the following code into the R console:
> paste(c("a", "b"), "c", sep = "/")
Our resulting output would be:
[1] "a/c" "b/c"
We used the paste(...)
function to append a percentage sign (%) to each of our percentage labels (pieFireGoldCostLabelsPercent) and indicated that they should not be separated by any blank space or characters (sep = ""):
> pieFireGoldCostLabelsPercent <- paste(pieFireGoldCostLabelsPercent, "%", sep="")
Lastly, our pie(...)
function incorporated our custom percentage labels:
> pie(x = pieFireGoldCostSlices, labels = pieFireGoldCostLabelsPercent, main = pieFireGoldCostMain, col = pieFireGoldCostSpecificColors)
Note that the paste(...)
function can be used to add any kind of text to a label. Its general purpose is to append text to the front and back of values. As such, it is applicable in many situations.
Yet again, we found it necessary to include a legend in our chart. Without a legend, our graphic would not indicate what our percentage labels referred to. Our legend was placed at the bottom of our graphic and reflected our chart's original text labels (rather than percentages) and colors; the following is the code:
> legend(x = "bottom", legend = pieFireGoldCostLabels, fill = pieFireGoldCostSpecificColors)
round(x, digits)
function?> round(9.876543, 3)
a. 9.877
b. 9.876
c. 9.87
d. 9.88
paste(originalValues, appendText, sep = "sep")
function, what does the sep
argument represent?a. A vector containing the items that are to be appended.
b. The text to be added to the original values.
c. An optional separator between the original value and the appended text.
d. A vector containing the text to be appended.