An Overview of the Base Plot Function in R

The base graphics function to create a plot in R is simply called plot(). This powerful function has many options and arguments to control all kinds of things, such as the plot type, line colors, labels, and titles.

The plot() function is a generic function and R dispatches the call to the appropriate method. For example, if you make a scatterplot, R dispatches the call to plot.default(). The plot.default() function itself is reasonably simple and affects only the major look of the plot region and the type of plotting. All the other arguments that you pass to plot(), like colors, are used in internal functions that plot.default() simply happens to call.

To get started with plot, you need a set of data to work with. One of the built-in datasets is islands, which contains data about the surface area of the continents and some large islands on Earth.

First, create a subset of the ten largest islands in this dataset. There are many ways of doing this, but the following line of code sorts islands in decreasing order and then uses the head() function to retrieve only the first ten elements:

> large.islands <- head(sort(islands, decreasing=TRUE), 10)

It is easy to create a plot with informative labels and titles. Try the following:

> plot(large.islands, main="Land area of continents and islands",
+   ylab="Land area in square miles")
> text(large.islands, labels=names(large.islands), adj=c(0.5, 1))

How does this work? The first line creates the basic plot with plot() and adds a main title and y-axis label. The second line adds text labels with the text().

image0.jpg
  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win $500. Easy.