 # Example R scripts The open-source statistical package R is able to produce a variety of fine graphs that can be easily exported into PDF and postscript formats.

This page demonstrates how easily a large variety of graphs can be generated. (Of course, other tools are also available for creating graphs -- here is a page that shows how to generate the same graphs using jgraph.)

Some general things. This is not a basic introduction to R. See

In this ``log'' graph ( pdf, source, data file 1, data file 2), a logarithmic scale is used on the y axis. Note some of the features on this graph.

• plot() is used to create axes and labels.
• lines() is used to plot single curves.
• t.test() is used to test if the means of the two data sets differ.
• legend() is used to manually create a legend.
• print() is used to output the graph.

In this multi-line graph (source, pdf, data file), several lines are plotted from the one data file.

Note some of the features on this graph.
• Subsets of data are chosen.
• A mix of strategies have been used to distinguish sets of lines from each other.
• Tick marks are manually placed.

In this graph (source, pdf, data file, data file), simple R commands are used to filter the data, and print an average every tenth point instead of printing every point.

Note some of the features on this graph.
• Two plots are added to the same set of axis.
• R is used to process the data prior to plotting.

A graph with two axes (source, pdf, data) is useful for plotting two curves that have the same domain but different ranges.

• plot() is used to draw plots of the graph with desired mark type and line style.
• use type ='n' to suppress the drawing of axes, then
• use axis to draw each axis with desired style.
• since the two y-axes are of different ranges, 2 separate plots are used to draw plots for the 1st and 2nd y-axis.
par(new=TRUE) is used so that the two plots end up in a single graph.
• Each axis needs major and minor ticks, axis() is called twice to draw minor and major ticks.
• use 'pos' in y-axis (x-axis) to specify the location of y-axis (x-axis) relative to x-axis (y-axis).
• use legend() to draw legend manually.
• mtext() is used for drawing the label of the 2nd y-axis.

Text is placed on this graph (pdf, source) using the text function. The most basic form of the function is

`text(x, y, "text")`
• This places the word "text" at the point (x, y).
• x, y and text can all be vectors. If vectors of different lengths are used, the shorter vectors are recycled.

### Error bars

A graph with error bars (source, pdf, data 1, data 2, data 3, data 4) and points. Note

• Shifting of points to left and right to separate out the points shown at each integer level.
• Use of a function (with variable number of arguments)
• Use of the arrows() function to draw error bars

### Bar graphs

Another useful form of graph is a bar graph (pdf, source, data).

• A simple bar graph that makes use of the transpose function t()

### Tips 'n' tricks

#### "join"ing two files

```f1 <- read.table("data1",col.names=c("word","freq")) 