Time for action - customizing a box plot

In learning to customize box plots, we will alter whisker lengths and create custom axes for our graphics.

  1. Use the range argument to alter the whisker length of each box:
    > #modify the chapter 8 multiple box plot that that compares the number of Shu soldiers required across the battle methods
    > #rescale the y axis to best display the new range
    > boxPlotAllMethodsShuSoldiersLimY <- c(0, 100000)
    > #use the range argument to alter the whisker length of each box
    > #use range = 0 to extend the whiskers to the most extreme points
    > #use range > 0 to extend the whisker to a value of n times the interquartile range
    > #here, limit the whisker range to 1 times the interquartile range
    > boxPlotAllMethodsShuSoldiersRange <- 1
    > #use boxplot(...) to create and display the revised line chart
    > boxplot(formula = boxplotAllMethodsShuSoldiersData,
    main = boxPlotAllMethodsShuSoldiersLabelMain,
    xlab = boxPlotAllMethodsShuSoldiersLabelX,
    ylab = boxPlotAllMethodsShuSoldiersLabelY,
    ylim = boxPlotAllMethodsShuSoldiersLimY,
    range = boxPlotAllMethodsShuSoldiersRange)
    
  2. Your plot will be displayed in the graphic window. Note the rescaling of the y-axis and the change in whisker length for the boxes:
    Time for action - customizing a box plot
  3. Prepare to create custom axes by hiding your box plot's default axes:
    > #hide the box plot's default axes
    > #redraw the box plot using the xaxt and yaxt arguments to hide the axes
    > boxplotAllMethodsShuSoldiersAxtX = "n"
    > boxplotAllMethodsShuSoldiersAxtY = "n"
    > #use boxplot(...) to create a display the box plot
    > #your box plot will have no labels or tick marks on the x and y axes
    > boxplot(formula = boxplotAllMethodsShuSoldiersData,
    main = boxPlotAllMethodsShuSoldiersLabelMain,
    xlab = boxPlotAllMethodsShuSoldiersLabelX,
    ylab = boxPlotAllMethodsShuSoldiersLabelY,
    ylim = boxPlotAllMethodsShuSoldiersLimY,
    range = boxPlotAllMethodsShuSoldiersRange,
    xaxt = boxplotAllMethodsShuSoldiersAxtX,
    yaxt = boxplotAllMethodsShuSoldiersAxtY)
    
  4. Your plot will be displayed in the graphic window. Note the lack of x-axis and y-axis labels and tick marks:
    Time for action - customizing a box plot
  5. Use axis(...) to create custom axes for the box plot:
    > #use axis(...) to add custom x and y axes to the box plot
    > #your custom axes will be drawn atop the plot that is displayed in the graphic window
    > #your axes will be displayed when the axis(...) function is executed in the R console
    > #custom x axis
    > axis(side = 1, at = c(1, 2, 3, 4), labels = c("Ambush",
    "Fire", "Head to Head", "Surround"), las = 0)
    > #custom y axis
    > axis(side = 2, at = c(1000, 25000, 50000, 75000, 100000),
    las = 0)
    
  6. Your custom axes will be added to the existing plot:
    Time for action - customizing a box plot

What just happened?

We customized our box plot to make it more presentable. Let us review the customization options offered by the boxplot(...) function.

range

We used the boxplot(...) function's range argument to alter the length of each box's whiskers. In general, range will take on a positive value between 0 and 1.5. At 0, a box's whiskers will extend all the way to the most extreme data points. At a value greater than 0, the boxes' whiskers will reach data points within one interquartile range times the range value. An interquartile range is the distance between the top (third quartile) and bottom (first quartile) of a given box. This measure gives us an idea of how spread out the data are. By using a range value closer to 0, we are shortening our boxes' whiskers and excluding more extreme data points. On the other hand, a higher range value will include more extreme points and lengthen each box's whiskers. In our case, we used a range value of 1:

> boxPlotAllMethodsShuSoldiersRange <- 1

This shortened our whiskers by extending them to points no more than one interquartile range beyond their boxes. Note that we also revised our ylim argument to better display our boxes, given the new range:

> boxplot(formula = boxplotAllMethodsShuSoldiersData,
main = boxPlotAllMethodsShuSoldiersLabelMain,
xlab = boxPlotAllMethodsShuSoldiersLabelX,
ylab = boxPlotAllMethodsShuSoldiersLabelY,
ylim = boxPlotAllMethodsShuSoldiersLimY,
range = boxPlotAllMethodsShuSoldiersRange)

axis(...)

To further improve our plot's aesthetics, we revised its x-axis and y-axis labels. Before adding our own axes, we had to eliminate the default ones generated by R. This entailed giving the xaxt and yaxt arguments an n value:

> boxplotAllMethodsShuSoldiersAxtX = "n"
> boxplotAllMethodsShuSoldiersAxtY = "n"

Subsequently, we redrew our box plot without x-axis and y-axis:

> boxplot(formula = boxplotAllMethodsShuSoldiersData,
main = boxPlotAllMethodsShuSoldiersLabelMain,
xlab = boxPlotAllMethodsShuSoldiersLabelX,
ylab = boxPlotAllMethodsShuSoldiersLabelY,
ylim = boxPlotAllMethodsShuSoldiersLimY,
range = boxPlotAllMethodsShuSoldiersRange,
xaxt = boxplotAllMethodsShuSoldiersAxtX,
yaxt = boxplotAllMethodsShuSoldiersAxtY)

We then used the axis(...) function twice, once for the x-axis and once for y-axis, to customize our plot's axis labels. The axis(...) function accepts several optional arguments, a number of which were employed in the creation of our plot:

  • side refers to the placement of the axis, where:
    • 1 = left
    • 2 = bottom
    • 3 = top
    • 4 = right
  • at contains a vector that holds the tick mark values for the axis
  • labels contains a vector of text items that will be paired with the at values; if undefined, the at values will be displayed on the axis
  • las positions the labels either parallel (0) or perpendicular (1) to the axis; note that las refers to the label style of the axis

When executed, the axis(...) function draws a new axis atop the visual currently displayed in the graphic window. For instance:

> axis(side = 2, at = c(10, 20, 30), labels = c("a", "b", "c"),
las = 1)

The code would draw a new x-axis on the bottom of the chart with tick marks at 10, 20, and 30 paired with the labels a, b, and c, that have been oriented vertically. Similarly, we used the following code to customize our x and y axes:

> #custom x axis
> axis(side = 1, at = c(1, 2, 3, 4), labels = c("Ambush", "Fire",
"Head to Head", "Surround"), las = 0)
> #custom y axis
> axis(side = 2, at = c(1000, 25000, 50000, 75000, 100000),
las = 0)

Our custom x-axis was placed at the bottom of the plot and effectively renamed our four boxes to Ambush, Fire, Head to head, and Surround. Our custom y-axis was placed on the left side of the plot and incorporated more meaningful soldier values than were present in the default axis.

Pop quiz

  1. In boxplot(...), a range argument of 0 would have what effect?

    a. It would eliminate the whiskers.

    b. It would extend the whiskers to the most extreme data points.

    c. It would eliminate the boxes.

    d. It would extend the boxes to the most extreme data points.

  2. Which of the following is not true of the axis(...) function?

    a. It accepts several optional arguments.

    b. It allows for the creation of axes in four different positions.

    c. It will use the labels argument by default when the at argument is undefined.

    d. It draws atop the visual that is currently displayed in the graphic window.

Have a go hero

Create a box plot that depicts the relationship between the number of Wei soldiers targeted by each of the four battle methods. Be sure to customize your plot to improve its readability and emphasize its most important features.

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

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