Time for action - customizing a pie chart

Moving on to pie charts, we will learn how to add custom label text to a pie's slices:

  1. Use the 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)
    
  2. Your chart will be displayed in the graphic window, as follows:
    Time for action - customizing a pie chart
  3. Add a legend to the chart:
    > #add a legend to the pie chart
    > legend(x = "bottom", legend = pieFireGoldCostLabels,
    fill = pieFireGoldCostSpecificColors)
    
  4. Your legend will be added to the existing chart, the final pie chart should look like the following:
    Time for action - customizing a pie chart

What just happened?

We just customized our pie chart by taking advantage of a new labeling option. Let us discuss how this feature is implemented.

Custom labels

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:

  • originalValues: a vector containing the items that are to be appended
  • appendText: the text to be added to the original values
  • sep: an optional separator between the original value and the appended text; a single space by default

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

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.

legend(...)

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)

Pop quiz

  1. What would be the result of the following round(x, digits) function?
    > round(9.876543, 3)
    

    a. 9.877

    b. 9.876

    c. 9.87

    d. 9.88

  2. In the 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.

Have a go hero

Create a pie chart that conveys the relationship between the number of soldiers engaged in the planned fire attack and the total number of soldiers housed at Hanzhong. Be sure to experiment with the customization options that we have covered in our previous examples.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.215.1