Time for action - creating a pie chart

Pie charts are a fast and easy way to visualize a single relationship within a dataset. Let us look at how to create a pie chart in R:

  1. Use the pie(...) function to create a pie chart:
    > #create a pie chart that depicts the gold cost of the fire
    attack in relation to the total funds allotted to the Shu army
    > #get the data to be used in the chart
    > #what is the cost of the proposed fire attack?
    > functionGoldCost(2500, 225, 7)
    [1] 6791.667
    > #we already know that 1,000,000 gold has been allotted to
    the Shu army
    > #therefore our remaining funds after the fire attack would
    be 993,208
    > #create a vector to hold the values for the chart's slices
    > pieFireGoldCostSlices <- c(6792, 993208)
    > #use the labels argument to specify the text associated with
    each of the chart's slices
    > pieFireGoldCostLabels <- c("mission cost", "remaining funds")
    > #customize the chart
    > pieFireGoldCostMain <- "Gold Cost of Fire Attack"
    > pieFireGoldCostSpecificColors <- c("green", "blue")
    > #use pie(...) to create and display the pie chart
    > pie(x = pieFireGoldCostSlices,
    labels = pieFireGoldCostLabels, main = pieFireGoldCostMain,
    col = pieFireGoldCostSpecificColors)
    
  2. Your chart will be displayed in the graphic window, similar to the following:
    Time for action - creating a pie chart
  3. Add a legend to the chart, using the following code:
    > #use the legend(...) function to add a legend to the chart
    > legend(x = "bottom", legend = pieFireGoldCostLabels,
    fill = pieFireGoldCostSpecificColors)
    
  4. Your legend will be added to the existing chart, which will look like the following:
    Time for action - creating a pie chart
..................Content has been hidden....................

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