Chapter 1. Base Graphics in R – One Step at a Time

The goal of this chapter is to give you a comprehensive introduction to base graphics in R. By base graphics, I mean graphics created in R without the use of any additional software or contributed packages. In other words, for the time being, we are using only the default packages in R. After reading this chapter, you should be able to create some nice graphs. Therefore, in this chapter I will introduce you to the basic syntax and techniques used to create and save scatterplots and line plots, though many of the techniques here will be useful for other kinds of graph. We will begin with some basic graphs and then work our way to more complex graphs that include several lines and have axes and axis labels of your choice.

In this chapter, we will cover the following topics:

In R, we create graphs in steps, where each line of syntax introduces new attributes to our graph. In R, we have high-level plotting functions that create a complete graph such as a bar chart, pie chart, or histogram. We also have low-level plotting functions that add some attributes such as a title, an axis label, or a regression line. We begin with the plot() command (a high-level function), which allows us to customize our graphs through a series of arguments that you include within the parentheses. In the first example, we start by setting up a sequence of x values for the horizontal axis, running from -4 to +4, in steps of 0.2. Then, we create a quadratic function (y) which we will plot against the sequence of x values.

Enter the following syntax on the R command line by copying and pasting into R. Note the use of the assigns operator, which consists of the less than sign followed by a minus sign. In R, we tend to use this operator in preference to the equals sign, which we tend to reserve for logical equality.

You can enter x and y at the command line to see the values that R has created for us.

Now use the plot() command. This command is very powerful and provides a range of arguments that we can use to control our plot. Using this command, we can control symbol type and size, line type and thickness, color, and other attributes.

Now enter the following command:

You will get the following graph:

Learning basic graphics techniques

This is a very basic plot, but we can do much better. Let's start again and build a nice plot in steps. Enter the following command, which consists of the plot() command and two arguments:

The argument pch controls symbol type. The symbol type 16 gives solid dots. A very wide range of colors is available in R and are discussed later in this chapter. The list of available options for symbol type is given in many online sources, but the Quick-R website (http://www.statmethods.net/advgraphs/parameters.html) is particularly helpful. Using the previous command, you will get the following graph:

Learning basic graphics techniques

Now we use the arguments xlim, ylim, xlab, and ylab. Enter the following plotting syntax on the command line:

This command will produce the following graph:

Learning basic graphics techniques

The arguments xlim and ylim control the axis limits. They are used with the c operator to set up the minimum and maximum values for the axes. The arguments xlab and ylab let you create labels, but you must include your labels within quotation marks.

Now, create line segments between the points using the following command:

Note that the lines() command is used after the plot() command. It will run provided that the graph produced by the plot() command remains open. Next, we will use the abline() command, where abline(a, b) draws a line of intercept a and slope b. The commands abline(h = k) and abline(v = k) draw a horizontal line at the value k and a vertical line at the value k.

We enter each of these commands on a new line as shown:

Your legend begins at the point (-8, 36) and is now centered on the point (-4, 36). The text() command will be discussed in more detail in Chapter 2, Advanced Functions in Base Graphics. The legend() command is very powerful and provides many options for creating and placing legends; it is also discussed in Chapter 2, Advanced Functions in Base Graphics. For now, it is enough to know that you specify the position of your legend by entering relevant coordinates as the first two arguments, and then enter your text inside quotation marks.

Consider the following command:

The rug() command indicates the location of points on the horizontal axis. Here is the resulting graph:

Learning basic graphics techniques

In just a few lines of syntax, you have learned how to make a fairly complex graph and you can now use the same techniques to draw your own graphs.