In learning to customize box plots, we will alter whisker lengths and create custom axes for our graphics.
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)
> #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)
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)
We customized our box plot to make it more presentable. Let us review the customization options offered by the boxplot(...)
function.
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)
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:at
contains a vector that holds the tick mark values for the axislabels
contains a vector of text items that will be paired with the at
values; if undefined, the at
values will be displayed on the axislas
positions the labels
either parallel (0) or perpendicular (1) to the axis; note that las
refers to the label style of the axisWhen 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.
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.
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.