Doing Graphs with R: Intro

As you might expect (or have heard about), R offers a rich numbers of possibilities for creating charts (graphs, plots) – and therefore graphs may require some effort on your part. Still, many graphs are very easy to obtain; the effort may increase if you wish to amend them to your purposes.

How this section is organized

In this section, I first introduce a number of graphs. I will present mostly basic plotting functions, but occasionally I will mention features that are offered by other packages. I will also use some options to change the look graphs, but not in any systematic way.

The later sections will try to introduce some possibilities for more complex graphs (starting with the entry on Combining Graphs), and I will try to deal with some topics in a more systematic manner. These entries are very much work in (slow) progress!

Here are some more things you might wish to know:

Some introductory remarks about graphs

Basic plots vs. additional libraries

Many advanced users recommend special libraries for plotting, in particular ggplot2 and lattice. Here, I will content myself to introducing you to the basic plot commands; only occasionally I will allude to possibilities that are offered in the specialized packages.

plot: a very special command

Unsurprisingly, plot is one important R command. But in fact it's more: It's a 'method'. This means that it is a generic procedure that may be associated with differents classes of objects. And this, again, means that it will do different things, depending on the class of your object.

Normal users will not worry too much; as long as you are using data.frame objects, plot will simply produce scatterplots. But with objects that belong the class of 'spatial' objects (polygons, geographical point data, and others) plot will (or may) produce maps!

Most of the plots have special commands anyway, so we will not deal very much with the plot command in this section. I am just writing this in order for you not to be surprised by the many different examples found on the web that are produced with plot.

Arguments to plot commands vs. low-level plot functions

A plot consists of many things: It shows a distribution or some parameter values or some data, but it also has axes which in most cases are labeled, possibly a title, a legend, and still other features. Not surprisingly, plot commands may be rather complex due to the many arguments that may be required to create, or change, such features.

Yet, you will often encounter examples on the web where plots are built step-by-step. For instance, you may first create a plot and later add a title and a legend to it, using special commands. These commands in R parlance are called low-level plot functions, in contrast to the plot commands proper that are sometimes referred to as high-level plot functions.

The following example demonstrates the difference, using a built-in R dataset. First, we will deploy the main argument to add a title:

attach(airquality)
plot(Ozone, Wind, main="Ozone vs. Wind")

But likewise you may first create the plot and in the next step add the title with the title command (or function), with main as an argument to that command:

plot(Ozone, Wind)
title(main="Ozone vs. Wind")

Note that in the latter case, you may have to take precautions to leave enough space for the title. This holds particularly true in the case of plots from user-created packages.

Arguments to plot commands vs. parameters

Graph commands typically offer arguments that influence the way a graph looks. Such arguments may also be set in advance, with the settings remaining valid for all graphs until they are changed.

Using the example of the previous section, we may wish to enlarge the title a bit, which may be accomplished as follows:

attach(airquality)
plot(Ozone, Wind, main="Ozone vs. Wind", cex.main = 1.5)

The value of 1.5 tells R to enlarge the title by a factor of 1.5. If we want to produce several graphs, all with a larger title, we may change the cex.main parameter as follows, with the same effect as before.

par(cex.main = 1.5)
plot(Ozone, Wind, main="Ozone vs. Wind")

Further below you will find an entry that deals with parameters.

© W. Ludwig-Mayerhofer, R Guide | Last update: 28 Nov 2024