Time for action - building a graphic

Having explored an extensive range of graphic types and customizations in R, our next challenge is to build a graphic from the ground up. To accomplish this feat, we will start with an empty foundation and use our customization arguments and functions to build a complete graphic:

  1. Use the plot(...) function to create a foundation for the graphic:
    > #build a custom graphic from scratch
    > #step 1: create a foundation
    > #create a graphic that depicts the number of Shu and Wei soldiers engaged in past fire attacks
    > #prepare the graphic's basic parameters
    > #note that this will require thinking ahead about the information that you want to display
    > buildFireSoldiersMain <- "Soldiers Engaged by Kingdom"
    > buildFireSoldiersLabX <- "Battle Number"
    > buildFireSoldiersLabY <- ""
    > buildFireSoldiersLimX <- c(0, 30)
    > buildFireSoldiersLimY <- c(0, 50000)
    > #hide the points and axes
    > buildFireSoldiersType <- "n"
    > buildFireSoldiersAxtX <- "n"
    > buildFireSoldiersAxtY <- "n"
    > #use the plot(...) function to create a foundation for the graphic
    > plot(x = 0, y = 0, main = buildFireSoldiersMain,
    xlab = buildFireSoldiersLabX, ylab = buildFireSoldiersLabY,
    xlim = buildFireSoldiersLimX, ylim = buildFireSoldiersLimY,
    type = buildFireSoldiersType, xaxt = buildFireSoldiersAxtX,
    yaxt = buildFireSoldiersAxtY)
    
  2. An empty foundation for our graphic will open in the graphic window, as shown:
    Time for action - building a graphic
  3. Add axes to the graphic.
    > #step 2: add axes
    > #use axis(...) to add custom x and y axes to the graphic
    > #x axis
    > axis(side = 1, at = c(0:30), las = 0)
    > #y axis
    > axis(side = 2,
    at = c(1000, 5000, 10000, 20000, 30000, 40000, 50000),
    las = 1)
    
  4. Your custom axes will be added to the existing graphic, and will look like the following:
    Time for action - building a graphic
  5. Add data to the graphic:
    > #step 3: add data
    > #use points(...) to add data to the graphic
    > #note that lines(...) can also be used to add data to a graphic
    > #add points to show the number of Shu soldiers engaged in past fire attacks
    > pointsFireShuSoldiersDataX <- c(1:30)
    > pointsFireShuSoldiersDataY <- subsetAmbush$ShuSoldiersEngaged
    > pointsFireShuSoldiersType <- "p"
    > pointsFireShuSoldiersColor <- "forestgreen"
    > points(x = pointsFireShuSoldiersDataX,
    y = pointsFireShuSoldiersDataY,
    type = pointsFireShuSoldiersType,
    col = pointsFireShuSoldiersColor)
    > #add points to show the number of Wei soldiers engaged in past fire attacks
    > pointsFireWeiSoldiersDataX <- c(1:30)
    > pointsFireWeiSoldiersDataY <- subsetAmbush$WeiSoldiersEngaged
    graphicsdata, adding> pointsFireWeiSoldiersType <- "p"
    > pointsFireWeiSoldiersColor <- "blue"
    > pointsFireWeiSoldiersPch <- 0
    > points(x = pointsFireWeiSoldiersDataX,
    y = pointsFireWeiSoldiersDataY,
    type = pointsFireWeiSoldiersType,
    col = pointsFireWeiSoldiersColor,
    pch = pointsFireWeiSoldiersPch)
    
  6. Your custom points will be added to the graphic, as shown in the following:
    Time for action - building a graphic
  7. Add a legend to the graphic:
    > #step 4: add a legend, if necessary
    > #use legend(...) to add a legend to the graphic
    > legend(x = 0, y = 50000, legend = c("Shu", "Wei"),
    fill = c(pointsFireShuSoldiersColor,
    pointsFireWeiSoldiersColor))
    
  8. Your legend will be added to the graphic. The final graphic will look like the following:
    Time for action - building a graphic

What just happened?

We used our custom graphics functions to build an entire graphic from scratch. Let us review the steps involved in this process:

  1. Building the foundation

    We began by using our plot(...) function to create a foundation for our new graphic. The main difference when creating a foundation graphic compared to a normal one is that we do not want to display any data. Hence, we set the x and y values to 0, the type to n, and the xaxt and yaxt arguments to n. This yields a blank square to which we can add custom information later. However, it is still critical to match the xlim and ylim arguments to the bounds of the data that we plan to use, in spite of the fact that the axes themselves are hidden. The overall title and x and y labels can be optionally defined, if we would like them to appear on the graphic:

    > buildFireSoldiersMain <- "Soldiers Engaged by Kingdom"
    > buildFireSoldiersLabX <- "Battle Number"
    > buildFireSoldiersLabY <- ""
    > buildFireSoldiersLimX <- c(0, 30)
    > buildFireSoldiersLimY <- c(0, 50000)
    > buildFireSoldiersType <- "n"
    > buildFireSoldiersAxtX <- "n"
    > buildFireSoldiersAxtY <- "n"
    

    Note

    Note that this step required that we think ahead about the data that we wanted to display, especially as it pertains to the x-axis and y-axis limits. At times, this preparation step may also call for experimentation with default graphics to lend us a better idea of how the data will display when customized.

  2. Our plot(...) function incorporated all of these parameters and rendered us with a foundation graphic that was prepared to incorporate our data:
    > plot(x = 0, y = 0, main = buildFireSoldiersMain,
    xlab = buildFireSoldiersLabX, ylab = buildFireSoldiersLabY,
    xlim = buildFireSoldiersLimX, ylim = buildFireSoldiersLimY,
    type = buildFireSoldiersType, xaxt = buildFireSoldiersAxtX,
    yaxt = buildFireSoldiersAxtY)
    
  3. Adding axes

    Since we completely hid our axes from the foundation graphic, our next step involves creating custom ones. We added custom axes to our graphic using the familiar axis(...) function:

    > #x axis
    > axis(side = 1, at = c(0:30), las = 0)
    > #y axis
    > axis(side = 2,
    at = c(1000, 5000, 10000, 20000, 30000, 40000, 50000),
    las = 1)
    
  4. For a review of the axis(...) function, see the Customizing a box plot section of this chapter.
  5. Adding data

    After the axes are defined, it is time to add data to the graphic. While we chose to use the points(...) function to add data in this activity, note that lines(...) could also be used, depending on the type of visual effect that you prefer. Our data consisted of the number of Shu and Wei soldiers engaged in past fire attacks:

    > #add points to show the number of Shu soldiers engaged in past fire attacks
    > pointsFireShuSoldiersDataX <- c(1:30)
    > pointsFireShuSoldiersDataY <- subsetAmbush$ShuSoldiersEngaged
    > pointsFireShuSoldiersType <- "p"
    > pointsFireShuSoldiersColor <- "forestgreen"
    > points(x = pointsFireShuSoldiersDataX, y = pointsFireShuSoldiersDataY, type = pointsFireShuSoldiersType, col = pointsFireShuSoldiersColor)
    > #add points to show the number of Wei soldiers engaged in past fire attacks
    > pointsFireWeiSoldiersDataX <- c(1:30)
    > pointsFireWeiSoldiersDataY <- subsetAmbush$WeiSoldiersEngaged
    > pointsFireWeiSoldiersType <- "p"
    > pointsFireWeiSoldiersColor <- "blue"
    > pointsFireWeiSoldiersPch <- 0
    > points(x = pointsFireWeiSoldiersDataX,
    y = pointsFireWeiSoldiersDataY,
    type = pointsFireWeiSoldiersType,
    col = pointsFireWeiSoldiersColor,
    pch = pointsFireWeiSoldiersPch)
    
  6. For a review of points(...) and lines(...) functions, see the Customizing a scatterplot and Customizing a line chart sections of this chapter.
  7. Add a legend, if necessary

    The final step in building a custom graphic is to add a legend, if the graphic that you have created calls for one. This can be done using the same legend(...) function that we have exercised throughout our time exploring R's graphic capabilities:

    > legend(x = 0, y = 50000, legend = c("Shu", "Wei"),
    fill = c(pointsFireShuSoldiersColor,
    pointsFireWeiSoldiersColor))
    
  8. For a review of the legend(...) function, see the Customizing graphics section of the previous chapter.

In the end, we managed to build a complete, fully customized scatterplot starting from scratch. This development process is invaluable when you are creating graphics to present to others, use in reports, or publish outside of R.

Pop quiz

  1. Is it important to define the xlim and ylim arguments on a foundation graphic? Why or why not?

    a. It is important, because these arguments scale our axes in preparation for data that will be added later.

    b. It is important, because the plot(...) function will not execute if these arguments are left undefined.

    c. It is not important, because the default axes will automatically scale to our data.

    d. It is not important, because the x and y axes are hidden.

  2. Could steps 2 (adding axes), 3 (adding data), and 4 (adding legend) of the graphic building process occur in a different order than the one that was demonstrated in this section?

    a. No, they must be executed precisely in the order specified.

    b. Yes, axes or data can occur in any order, but a legend must be added last.

    c. Yes, the data or legend can occur in any order, but axes must be added first.

    d. Yes, these steps merely add visual elements to our graphic and therefore can be executed in any order.

Have a go hero

You have practiced generating highly customized graphics and even learned to build your own graphic from scratch. Use your refined R talents to create at least three graphics that will convince the top generals of the Shu army to join you in battle. Recall that the generals are most interested in scrutinizing the details of your proposed attack and comparing it with alternative combat strategies. Be sure to explore new combinations of graphic arguments and functions. Refer back to the individual sections of this chapter for assistance with creating graphics of particular types.

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

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