Time for action - customizing graphics

Although the barplot(...) function only requires the height of each bar to be specified, creating a chart in this manner leaves us with a bland and difficult to decipher visual. In most cases, you will want to customize your R graphics by incorporating additional arguments into your functions. Let us explore how to use graphic customization arguments by expanding our bar chart:

  1. Expand your bar chart using graphic customization arguments:
    > #use additional arguments to customize a graphic
    > #define a title for the bar chart
    > barAllMethodsDurationLabelMain <-
    "Average Duration by Battle Method"
    > #define x and y axis labels for the bar chart
    > barAllMethodsDurationLabelX <- "Battle Method"
    > barAllMethodsDurationLabelY <- "Duration in Days"
    > #set the x and y axis scales
    > barAllMethodsDurationLimX <- c(0, 5)
    > barAllMethodsDurationLimY <- c(0, 120)
    > #define rainbow colors for the bars
    > barAllMethodsDurationRainbowColors <- rainbow(length(barAllMethodsDurationBars))
    > #incorporate customizations into the graphic function using
    the main, xlab, ylab, xlim, ylim, names, and col arguments
    > #use barplot(...) to create and display the bar chart
    > barplot(height = barAllMethodsDurationBars,
    main = barAllMethodsDurationLabelMain,
    xlab = barAllMethodsDurationLabelX,
    ylab = barAllMethodsDurationLabelY,
    xlim = barAllMethodsDurationLimX,
    ylim = barAllMethodsDurationLimY,
    col = barAllMethodsDurationRainbowColors)
    
  2. Your chart will be displayed in the graphic window, as shown in the following screenshot:
    Time for action - customizing graphics
  3. Add a legend to the chart, using the following snippet:
    > #add a legend to the bar chart
    > #the x and y arguments position the legend
    > #x and y can be defined using words or numerical coordinates
    > #the legend argument receives a vector containing the labels
    for the legend
    > barAllMethodsDurationLegendLabels <- c("Fire", "Ambush",
    "Head to Head", "Surround")
    > #the fill argument contains the colors for the legend
    > legend(x = 0, y = 120,
    legend = barAllMethodsDurationLegendLabels,
    fill = barAllMethodsDurationRainbowColors)
    
  4. Your legend will be added to the existing chart.
Time for action - customizing graphics

What just happened?

The barplot(...) function, as well as the other graphic functions that we will use in this book, accept a variable number of arguments. In fact, R graphics functions have many customizable options and therefore tend to accept several arguments. We expanded our bar chart using a collection of the most common customization arguments, which apply to nearly all R graphics functions.

Graphic customization arguments

We used six arguments to customize our bar chart:

  • main: a text title for the graphic
  • xlab: a text label for the x axis
  • ylab: a text label for the y axis
  • xlim: a vector containing the lower and upper limits for the x axis
  • ylim: a vector containing the lower and upper limits for the y axis
  • col: a vector containing the colors to be used in the graphic

The general format for these arguments is as follows:

argument = value

When incorporated into a graphics function, these arguments take on the following form:

graphicsFunction(..., argument = value)

Recognize that these six arguments can be applied to nearly every R graphics function. Each one can be used alone or they can be used in tandem. We will use these arguments throughout the chapter to refine and improve our visuals.

main, xlab, and ylab

The main, xlab, and ylab arguments are all used to add clarifying text to graphics. A primary title for a graphic is defined by main, while labels for the x and y axes are specified using xlab and ylab, respectively.

Our barplot(...) function made use of the main, xlab, and ylab arguments. We saved our argument values into variables prior to incorporating them into the barplot(...) function. First, we defined our text values as variables.

> #define a title for the bar chart
> barAllMethodsDurationLabelMain <-
"Average Duration by Battle Method"
> #define x and y axis labels for the bar chart
> barAllMethodsDurationLabelX <- "Battle Method"
> barAllMethodsDurationLabelY <- "Duration in Days"

Then, we used our variables in the final barplot(...) function:

> barplot(height = barAllMethodsDurationBars,
main = barAllMethodsDurationLabelMain,
xlab = barAllMethodsDurationLabelX,
ylab = barAllMethodsDurationLabelY,
xlim = barAllMethodsDurationLimX,
ylim = barAllMethodsDurationLimY,
col = barAllMethodsDurationRainbowColors)

This variable technique has the advantages of rendering our code more decipherable and making it easier for us to return to and reuse our data in future graphics. We will continue to use this method throughout the chapter.

xlim and ylim

The xlim and ylim arguments receive a vector containing the minimum and maximum values for the x and y axes respectively. Thus, in:

xlim = c(50, 250)

A graphic's x axis is told to present the data that fall between 50 and 250. The ylim argument operates in identical fashion to xlim, with the exception that it acts upon the y axis. These arguments are useful for rescaling a graphic's axes to improve its visual presentation. They can also have the effect of emphasizing or deemphasizing certain data ranges.

In our chart, we used xlim to set a minimum of 0 and a maximum of 5 for the x axis. This evenly and comfortably spaced our bars within the graphic window. We used ylim to set a minimum of 0 and maximum of 120 for the y axis. This ensured that all of our data were represented and that our bars were displayed at a reasonable height.

> barplot(height = barAllMethodsDurationBars,
main = barAllMethodsDurationLabelMain,
xlab = barAllMethodsDurationLabelX,
ylab = barAllMethodsDurationLabelY,
xlim = barAllMethodsDurationLimX,
ylim = barAllMethodsDurationLimY,
col = barAllMethodsDurationRainbowColors)

Col

R can generate colors in two different forms using Col; they can be rainbow colors which are automatic, or you can specify colors of your choice.

Rainbow colors

R can generate an automatic sequence of colors for a chart with the rainbow(...) function. For our purposes, we simply identified the number of colors that we wished to generate for our chart. To obtain the appropriate number of colors, we used the length(object) command. This function tells us the number of items contained in a given object. In our case, using length(object) on the barAllMethodsDurationBars yielded a result of 4, which represents each of our chart's bars:

> barAllMethodsDurationSpecificColors <-
rainbow(length(barAllMethodsDurationBars))

Consequently, the rainbow(...) function generated four colors. These colors were applied to the chart's bars when we included the barAllMethodsDurationRainbowColors variable in the col argument of our barplot(...) function.

> barplot(height = barAllMethodsDurationBars,
main = barAllMethodsDurationLabelMain,
xlab = barAllMethodsDurationLabelX,
ylab = barAllMethodsDurationLabelY,
xlim = barAllMethodsDurationLimX,
ylim = barAllMethodsDurationLimY,
col = barAllMethodsDurationRainbowColors)

Specific colors

Alternatively, specific colors can be defined using the col argument in tandem with a vector list of color names. Common color names such as red, green, blue, and yellow are valid inputs. In this situation, the col argument takes on the following form:

col = colorVector

Where colorVector is a variable storing a vector of color values like the following:

c("red", "green", "blue", "yellow")

Note

You can see a complete list of the colors available in R by executing the colors() function.

Had we wanted to use specific colors in our bar chart, we could have employed the following code:

> #define specific colors for the bars
> barAllMethodsDurationSpecificColors <- c("red", "green", "blue",
"yellow")
> #use barplot(...) to create and display the bar chart
> barplot(height = barAllMethodsDurationBars,
main = barAllMethodsDurationLabelMain,
xlab = barAllMethodsDurationLabelX,
ylab = barAllMethodsDurationLabelY,
xlim = barAllMethodsDurationLimX,
ylim = barAllMethodsDurationLimY,
col = barAllMethodsDurationSpecificColors)

legend(...)

The finishing touch to our bar chart was a legend, or key, that indicated what our bars represented. In R, the legend(...) function employs the following arguments:

  • x: the x position of the chart in numeric terms; alternatively you can set the overall position of the legend using one of the text values topleft, top, topright, left, center, right, bottomleft, bottomcenter, or bottomright
  • y: the y position of the chart in numeric terms; if text is used for x, omit this argument
  • legend: a vector containing the labels to be used in the legend
  • fill: a vector containing the colors to be used in the legend

The basic format for the legend function is as follows:

legend(x = xPosition, y = yPosition, legend = labelVector,
fill = colorVector)

For instance, the following code:

> legend(x = "topleft", legend = c("a", "b"), fill = rainbow(2))

This would yield a legend placed at the top-left position with labels for a and b whose colors were generated by the rainbow(...) function. Note that the x argument used a text value and y was omitted as an alternative to defining the exact numerical position of the legend.

Our function used the x and y coordinates from our chart to position the legend in the upper left-hand corner. When using numbers to define the x and y arguments, the values will always depend on the limits of the x and y axes. For instance, a position of (0, 120) specified the upper left-hand corner in our chart, but a graphic with a maximum y value of 50 would have an upper left-hand corner position of (0, 50). Our legend and fill arguments incorporated the same labels and colors that were used to generate our bar chart. Thus, our legend was matched to the information depicted in our chart:

> legend(x = 0, y = 120,
legend = barAllMethodsDurationLegendLabels,
fill = barAllMethodsDurationRainbowColors)

Notice the peculiar implementation of the legend(...) function, which we have not previously encountered. As we will see with other graphics functions, legend(...) does not stand alone. To be properly employed, a compatible graphic must already exist for legend(...) to act upon. In this situation, legend(...) adds a new legend on top of the visual that is displayed in the graphic window. However, if no graphic is currently displayed when the legend(...) function is executed, an error message is returned. This is demonstrated in the following code:

> #using the legend(...) function when no graphic already exists
results in the following error
> legend(x = "topleft", legend = c("a", "b"), fill = rainbow(2))
Error in strwidth(legend, units = "user", cex = cex) :
plot.new has not been called yet

Therefore, to add a legend to your graphics in R, be sure to always create the graphic first, then apply the legend(...) function.

Pop quiz

  1. An xlim value of c(100, 300) means which of the following?

    a. Present the data that are not equal to 100 or 300 on the x axis.

    b. Present the data that are equal to 100 or 300 on the x axis.

    c. Present the data that are less than 100 or greater than 300 on the x axis.

    c. Present the data that are between 100 and 300 on the x axis.

  2. When should the legend(...) function be called?

    a. Before a graphic function is called.

    b. During a graphic function, included as an argument.

    c. After a graphic function.

    d. When a compatible graphic is displayed in the graphic window.

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

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