R Fundamentals

Graphics in R - an overview

R has built in libraries that offer excellent graphics support. The R installation contains three important packages namely graphics, lattice and grid that provide tools for drawing a wide variety of plots and shapes. In addition, many external packages like ggplot2 can be downloaded into R via internet for creating R plots with enhanced capabilities. We will give a brief description of these three packages and ggplot2 below.

The graphics package

The graphics library is the standard base graphics package that comes with R installation. This was originally developed for R. Many standard plots like basic plots of points and lines, histograms, bar charts, Pie charts, bplots, etc. can be drawn with this library. There are devices such as postscript, png, jpeg and pdf for converting the plots into various image file formats for all platforms running R.


In this library, there is a static device called canvas onto which objects are drawn by calling functions. Objects once drawn cannot be edited, and they have to be redrawn for any modifications. The global parameters such as margins and layouts can be manipulated by calling an appropriate function.


For all practical purposes, the graphics library is quiet sufficient for creating excellent quality plots and graphs that we generally use for data analysis and statistics.


This package comes along with R installation.


The grid package

The grid graphics system was later developed by Paul Murrel and added to R. This is a low level graphics system that allows the drawing and arranging of basic geometric shapes like polygons, curves, raster images etc. The grid package contains functions to access the canvas and allows the creation of multiple regions called viewports on a single canvas.


This package comes along with the R installation. We have to load the grid library into R before using it. (The graphics package automatically loads when we start R).


To load the grid library in R, type

> library("grid")
The lattice package

The lattice, developed by Deepayan Sarkar, is a high level visualization system based on a method called trellis graphics . This package handles multivariate data very efficiently.


The lattice package consists of high level functions for each task. These functions return objects that can be converted to graphs by the plot() functions of base R package. This package is based on grid graphics engine mentioned above.


This package also comes along with R installation and requires the package grDevices to be loaded.


To load the grDevices library in R, type

> library("grDevices")
The ggplot2 package

The ggplot2 is a graphics library for R, created by Hadley Wiskham. It is mentioned in its home page that "ggplot2 is a plotting system for R, based on the grammer of graphics, which tries to take the good parts of base and lattice graphics and none of the bad parts". (The Grammer of Graphics mentioned here is a classic book on graphical methods for scientific data visualization authored by Leyland Wilkinson).


We can create very elegant plots with this library.


The ggplot2 is an external package that has to be downloaded from inside R. To install this online from R prompt, type
> install.packages("ggplot2")

After installing once, to load the ggplot2 in R, type

> library("ggplot2")
The plot3D package for 3D plots in R

The R package plot3D is an excellent package for crating multidimensional data. Extending from the persp(), image() and contour() functions of R, this package provides functions for creating ribbons, histograms, scatterplots, surfaces, slices, arrows, countours, segments and images in three dimensions.


Visit here for a nice description of packages for 3D scatter plots in R.

About the graphics tutorials

We have mentioned four different graphic libraries in R. Many more are there.


In the following tutorials under the section titled "Graphics in R", we will describe the base graphics library commands in detail. The user can consult the manuals of these libraries for further learning.


For an excellent description of the lattice and grid packages with example codes, users can consult the three chapters of the book R Graphics by Paul Murrell. .