R Programming/Graphics

R includes at least three graphical systems, the standard graphics package, the lattice package for Trellis graphs and the grammar-of-graphics ggplot2 package. R has good graphical capabilities but there are some alternatives like gnuplot.

Interactive Graphics
This section discuss some ways to draw graphics without using R scripts.

The playwith package provides a graphical user interface to customize the graphs, add a title, a grid, some text, etc and it exports the R code you need if you want to replicate the analysis. If you want to know more, you can have a look at the screenshots on the website (link). See also the example on "R you Ready". This package require GTK+ libraries.

There is also a graphical user interface GrapheR which makes it very easy to draw graphs for beginners. This solution is cross-platform.

latticist (link) is another similar project.

Note also that some graphical user interface such as RKward and R Commander makes it easy to draw graphs.

Standard R graphs
In this section we present what you need to know if you want to customize your graphs in the default graph system.
 * is the main function for graphics. The arguments can be a single point such as 0 or c(.3,.7), a single vector, a pair of vectors or many other R objects.
 * is another important function which defines the default settings for plots.
 * There are many other plot functions which are specific to some tasks such as,  , etc. Most of them take the same arguments as the   function.

Titles
gives the main title,  the subtitle. They can be passed as argument of the  function or using the   function. the name of the x axis and  the name of the y axis.

The size of the text can be modified using the parameters,  ,  ,. Those parameters define a scaling factor, ie the value of the parameter multiply the size of the text. If you choose  the main title will be twice as big as usual.

Legend
. The position can be "bottomleft", "bottomright", "topleft", "topright" or exact coordinates.

Text in the margin
puts some texts in the margin. The margin can be at the bottom (1), the left (2), the top (3) or the right (4).

Mathematical annotations
We can add mathematical symbols using  and makes some substitution in a formula using.

Types
The type of a plot can be :
 * for none (nothing is printed),
 * for points,
 * for lines,
 * for both,
 * for both overlayed,
 * for histogram-like
 * and  for steps.

Axes
The default output print the axes. We can remove them with. We can also change them using the  function.

specifies the style of axis labels. It can be 0, 1, 2 or 3.
 * 0 : always parallel to the axis [default],
 * 1 : always horizontal,
 * 2 : always perpendicular to the axis,
 * 3 : always vertical.

It is also possible to add another y axis on the right by adding.

Margins
Margins can be computed in inches or in lines. The default is  which means that there are 5 lines at the bottom, 4 lines on the left, 4 lines in the top and 2 lines on the right. This can be modified using the  function. If you want to specify margins in inches, use. If you want to modify margins in lines, use. See  to learn more about the topic.

Colors
The color of the points or lines can be changed using the  argument,   for foreground colors (boxes and axes) and   for background colors.


 * (Hmisc) package plots the main R colors with their numeric code.
 * The list of all colors in R (pdf)


 * We can also generate new colors using the  function. The first argument is the intensity of red, the second, the intensity of green and the third, the intensity of blue. They vary between 0 and 1 by default but this can be modified with the option  .   returns the RGB code of R colors.   (gplots) gives the hexadecimal code.   and   (TeachingDemos) converts colors to grey scale.

Points
For points the symbols can be changed using the  option which takes integer values between 0 and 25 or a single character. can also takes a vector as argument. In that case the first points will use the first element of the vector as symbol, and so on.

The following code displays all the symbols on the same plot :

adds points to an existing plot.

Lines
We can change the line type with. The argument is a string ("blank", "solid", "dashed", "dotted", "dotdash", "longdash", or "twodash") or an integer (0=blank, 1=solid (default), 2=dashed, 3=dotted, 4=dotdash, 5=longdash, 6=twodash). The line width can be changed with. The default is. means that the width is twice the normal width.

adds an additional lines on a graph.

adds an horizontal line, a vertical line or a linear function to the current plot (  for the constant and   for the slope). can also plot the regression line.

Boxes
Each graph is framed by a box. specifies the box type.

See also  to add a box to an existing plot.

Grid
adds a grid to the current graph.

Although grid has an optional argument nx for setting the number of grid lines, it is not possible to tell it explicitly where to place those lines (it will usually not place them at integer values). A more precise and manageable alternative is to use abline.

Other figures
We can also add a circle to a plot with the  function in the calibrate package.

Background
You can choose the background of your plot. For instance, you can change the background color with.

Overlaying plots
can plot several plots at the same time.

Multiple plots
With  we can display multiple figures on the same plot. prints 6 figures on the same plot with 3 rows and 2 columns. does the same but the order is not the same.

Plotting a function

 * plots a function. This can be added to an existing plot with the option.
 * can also plots functions.

Exporting graphs
How can you export a graph ?
 * First you can plot the graph and use the context menu (right click on Windows and Linux or control + click on Mac) to copy or save the graphs. The available options depend on your operating system. On Windows, you can also use copy the current graph to the clipboard as a Bitmap file (raster graphics) using CTRL + C or as a Windows Metafile (vector graphics) using CTRL + W. You can then paste it into another application.
 * You can export a plot to pdf, png, jpeg, bmp or tiff by adding,  ,  ,   or   prior to the plotting, and   after the plotting.
 * You can also use the  function to save existing graphs.
 * Sweave also produce ps and pdf graphics (See the Sweave section).

It is better to use vectorial devices such as pdf, ps or svg.

How can you know the list of all available devices ?
 * Use the  function to see the list of available devices on your computer.
 * Use the  function to see the list of available devices on your computer.

We can also export to SVG using the  function.

The RSvgDevice library which was used in earlier versions of R seems now outdated.

Animated plots
The animation package provides dynamic graphics capabilities. It is possible to export the animation in flash, mpeg or gif format. There are more example on the aniwiki website : http://animation.yihui.name/.

You can also create motion charts using the googleVis package.

Interactive Graphics
The iplots package provides a way to have interactive data visualization in R,.
 * R GUI now offers interactive graphics – Deducer 0.4-2 connects with iplots

To create an interactive, animated plot viewable in a web browser, the animint package can be used. The main idea is to define an interactive animation as a list of ggplots with two new aesthetics:
 * showSelected=variable means that only the subset of the data that corresponds to the selected value of variable will be shown.
 * clickSelects=variable means that clicking a plot element will change the currently selected value of variable.

Graphics gallery
In this section, we review all kind of statistical plots and review all alternatives to draw them using R. This include code for the standard graphics package, the lattice package and the ggplot2 package. Also, we add some examples from the commons repository. We only add examples which are provided with the R code. You can click on any graph and find the R code.

Line plot
To draw a line plot, use the generic  function by setting.

Then, you can add further lines on the same plot using the  function.

Scatter plot

 * (lattice)
 * (ggplot2)
 * (lattice)
 * (ggplot2)

Log scale
Sometimes it is useful to plot the log of a variable and to have a log scale on the axis. It is possible to plot the log of a variable using the  option in the   function.
 * For a log log plot, use
 * For a log in the x axis only, use
 * For a log in the x axis only, use

Label points in a plot

 * It is possible to add labels with the  function.
 * (calibrate) makes it easy to add labels.

Histogram

 * (lattice)
 * (lattice)

You can learn more about histograms in the Non parametric methods page.

Box plot
Box plot :

Bar charts
See Bar charts on wikipedia.
 * takes a table as argument and returns a bar chart.
 * (ggplot2) with the option  takes a variable as argument and returns a bar chart.
 * takes a variable as argument and returns a bar chart.

Dot plot
See also Dot plot on Wikipedia.


 * dotchart

Treemap
The  function in the treemap package makes it easy to draw a treemap.

Confidence interval plot
Standard error bar chart are very useful to plot several estimates with confidence intervals.
 * The Hmisc package has an  function. This function takes the upper and lower bounds of the confidence intervals as argument.


 * function in Gelman and Hill's arm package. This functions is designed to display estimation results. It takes point estimates and standard errors as arguments.

See also
 * There is another  function in the sfsmisc package.
 * (gplots) also plot error bars.
 * (gplots)
 * (hacks)
 * See also Error bar on Wikipedia

3D plots

 * (rgl)
 * (lattice)
 * (lattice)

Diagrams

 * grid package by Paul Murrell
 * diagram package
 * Rgraphviz package
 * igraph package

Arc Diagrams
It is also possible to draw Arc Diagrams.

Dendrograms
It is possible to plot dendrograms in R.

Treemap
It is possible to draw a treemap using the  function in the treemap package.

Wordcloud
There is :
 * the  function in the wordcloud package
 * the  function in the tagcloud package

Timeline

 * in the timeline package

Resources

 * Tables 2 Graphs
 * R Graphics by Paul Murrell
 * ggplot2
 * Graphical Parameters