How to Add Points to a Plot in R
To illustrate some different plot options and types, like points and lines, in R, use the built-in dataset faithful. This is a data frame with observations of the eruptions of the Old Faithful geyser in Yellowstone National Park in the United States.
The built-in R datasets are documented in the same way as functions. So, you can get extra information on them by typing, for example, ?faithful.
You’ve already seen that plot() creates a basic graphic. Try it with faithful:
Because faithful is a data frame with two columns, the plot is a scatterplot with the first column (eruptions) on the x-axis and the second column (waiting) on the y-axis.
Eruptions indicate the time in minutes for each eruption of the geyser, while waiting indicates the elapsed time between eruptions (also measured in minutes). As you can see from the general upward slope of the points, there tends to be a longer waiting period following longer eruptions.
Add points to a plot in R
You add points to a plot with the points() function. You may have noticed on the plot of faithful there seems to be two clusters in the data. One cluster has shorter eruptions and waiting times — tending to last less than three minutes.
Create a subset of faithful containing eruptions shorter than three minutes:
> short.eruptions <- with(faithful, faithful[eruptions < 3, ])
Now use the points() function to add these points in red to your plot:
> plot(faithful) > points(short.eruptions, col="red", pch=19)
You use the argument col to change the color of the points and the argument pch to change the plotting character. The value pch=19 indicates a solid circle. To see all the arguments you can use with points, refer to ?points.
Change the shape of points in R
You’ve already seen that you can use the argument pch to change the plotting character when using points. This is described in more detail in the Help page for points, ?points. For example, the Help page lists a variety of symbols, such as the following:
pch=19: Solid circle
pch=20: Bullet (smaller solid circle, two-thirds the size of 19)
pch=21: Filled circle
pch=22: Filled square
pch=23: Filled diamond
pch=24: Filled triangle, point up
pch=25: Filled triangle, point down
Change the color of data points in R
You can change the foreground and background color of symbols as well as lines. You’ve already seen how to set the foreground color using the argument col=”red”. Some plotting symbols also use a background color, and you can use the argument bg to set the background color (for example, bg=”green”). In fact, R has a number of predefined colors that you can use in graphics.
To get a list of available names for colors, you use the colors() function (or, if you prefer, colours()). The result is a vector of 657 elements with valid color names. Here are the first ten elements of this list:
> head(colors(), 10)  "white" "aliceblue" "antiquewhite" "antiquewhite1"  "antiquewhite2" "antiquewhite3" "antiquewhite4" "aquamarine"  "aquamarine1" "aquamarine2"