Doing Graphs with R: Intro

R offers rich 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 adapt them to your purposes.

The entries gathered here give only a first introduction to some of the most basic graphs for social scientists (in addition to those in this section on graphs, see also the entries on EDA and on maps). However, there are enough projects on the web where you can find very substantial information on R graphs, and I would like to refer you to some of those:

  • R charts, a very comprehensive and helpful website by José Carlos Soage (who has also other R-related tutorials).
  • Robert Kabacoff's Modern Data Visualization with R which is a little less comprehensive, but helpful nevertheless.
  • Another helpful tutorial is provided by STHDA, i.e., "Statistical tools for high-throughput data analysis".
  • endmemo.com offers overviews on many R-related topics.

There are certainly many more out there, so I cannot claim that these are the best or the most helpful for you.

Furthermore, I wish to direct to your attention to a special website, dedicated to graphs as they were suggested by the great Edward Tufte. Tufte wrote several books on how to create graphs that are antithetic to the "chart junk" we so often encounter (the notion "chart junk" is his creation, afaik). The most important of these books probably is The Visual Display of Quantitative Information, self-published in 1982, and arguably the most beautiful book by a social scientist you'll ever see (apart from, possibly, Tufte's later books). This site on

demonstrates how Tufte's suggestions can be put to work in R. Note that Tufte (afaik) did not suggest new graphs, he just suggested ways to make well-known graphs more informative.

How this section about graphs is organized

This entry provides a brief overview. The following entries will 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 of graphs, but not in any systematic way.

The later entries 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!

Some introductory remarks about graphs

Basic plots vs. additional libraries

The R package contains one important, fundamental command for graphs, namely, plot. Details will be explained in the next section. In addition to plot (which covers some of the most elementary graphs, but not much more), there are several functions for special graphs, such as hist for histograms, stripchart for stripcharts, and countless others.

Many advanced users recommend special libraries for graphics, in particular lattice, ggplot2 or ggvis. Here, I will content myself to introducing you to the basic plot commands; only occasionally I will allude to or outline possibilities that are offered by 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. This, in turn implies that depending on the class of your object it will do different things. E.g., with objects that belong to the class of 'spatial' objects (polygons, geographical point data, and others) plot will (or may) produce maps! Another aspect is the type of variables involved. With a 'normal' data object (such as a matrix or a data.frame), plot will produce a scatterplot if it deals with two continous variables, and a mosaic plot with two categorical variables.

Many plots require special commands, so we will not deal too 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")

You can read this special entry that deals with parameters.

Saving graphs

A fast, but limited way is to right-click with your mouse, which will open a pop-up menu from which you can save you graph either in .emf (Extended Meta File) or in .eps (Extended Postscript) format. (At least this is what happens under a Windows operating system.)

For other formats, you have to select a special "device". This can work in two ways. The better way is probably this:

plot(Ozone, Wind, main="Ozone vs. Wind")
dev.print(jpeg, "Ozone-Wind.jpg")

This will send the graph to a jpeg file the name of which is indicated in the parentheses. (The file will be found in the working directory. You may also specify the directory as a prefix to the name.) The advantage of this is that what is saved to the disk will look more or less exactly like the graph you see on the screen.

Besides jpeg, R offers functions png, pdf, btm, tiff or svglite (the latter, for svg format, may have to be installed separately with install.packages("svglite")). All of these work in the same way. The list is probably not exhaustive.

A second way works as follows:

jpeg("Ozone-wind.jpg")
plot(Ozone, Wind, main="Ozone vs. Wind")
dev.off()

This tells R to send the graph not to the screen (!) but rather to a jpeg file the name of which is indicated in the parentheses. However, there is no garantuee that the graph that is saved looks exactly like the one that might be seen on the screen. (I have seen examples of this; e.g., axis labels were way too small in the saved version). So I don't know exactly why this mode exists, but since it does, and since it is often referred to on the web (and I have used it myself in the past), I wanted to let you know.

© W. Ludwig-Mayerhofer, R Guide | Last update: 23 Jun 2025