- APS
- Membership
- Meetings
- Publications
- Education
- Disease Impacts & Management
- Diseases and Pathogens
- For Educators
- Resources

- Careers

Graphics

Producing a simple x-y plot, or scatter plot, is easy in R. Use `help(plot)`

to get a description of options available.

DisSev <- c(0,12,67,34,23,28)

RelYield <- c(100,103,79,89,85,97)

plot(x=DisSev, y=RelYield, xlab='Disease Severity (%)',

ylab='Relative Yield (%)')

To draw a line through the points, from the smallest value of DisSev to the largest value of DisSev, the necessary ordering of the values can be determined using the `order`

command and then DisSev and RelYield can be indexed by these ordered values. The first command lines provide an introduction to `order`

and the related command `rank`

.

`# Set up a simple example vector`

ex8 <- c(103,101,102)

# The order command give the order in which entries

# must be drawn to put them in order from smallest to largest

order(ex8)

# First the 2nd entry must be drawn, then the 3rd, then the 1st.

# In contrast, the rank command gives the rank of the entries

# from smallest to largest.

rank(ex8)

# The first entry comes 3rd, the next entry comes first, and

# the next entry comes second.

`# Now use the order command and plot the DisSev data.`

DisSevOrd <- order(DisSev)

plot(DisSev[DisSevOrd],RelYield[DisSevOrd], type='l')

Suppose you would like to use color in your plots to make them easier to read or more attractive. Colors are easily added to plots using the `col='color name'`

statement in the `plot()`

command.

plot(DisSev, RelYield,

xlab='Disease Severity',

ylab='Relative Yield (%)',

col='orange',

pch=15,

)

To find a list of all colors available for use in R graphics type `colors()`

. Likewise, calling different numbers for `pch`

in the `plot`

command produces different shapes for the plotting points.

To add subscripts to your x or y labels enclose the text in `[]`

brackets, for example `H[2]O`

. To superscript text use a tilde, `~`

, before the text and use the `^`

before the part of the text to be raised, for example `~ cm^3`

.

plot(x=DisSev, y=RelYield,

xlab = quote(Size [subscript]),

ylab = quote(Volume ~ cm^3),

col='purple',

pch=17

)

Sometimes it's useful to specify the range for values represented in a graph rather than just using the default ranges produced by R. You can control the range of values on the x (horizontal) and y (vertical) axes using `xlim`

and `ylim`

.

plot(DisSev, RelYield, xlab='Disease Severity (%)',

ylab='Relative Yield (%)',

xlim=c(0,100), ylim=c(0,100))

Variations on the `plot()`

function are called when it is applied to different types of objects. For example, when `plot()`

is applied to categorical data, a boxplot is produced. When applied to the output from the linear regression function `lm`

, `plot()`

produces a set of four regression diagnostic plots.

R has interactive features, too. To see the ID for three points, try clicking on three points on the graph after entering the following:

identify(DisSev, RelYield, n=3)

To get the (x,y) coordinates for any four locations on the graph, try clicking any four places on the graph after entering `locator(4)`

. The coordinates will appear after all the locations have been clicked on.

You can also plot a particular function instead of just a set of variables.

curve(x^2, from=0, to=10,

xlab='Time', ylab='Population size')

or

curve(exp(x), from=0, to=10,

xlab='Time', ylab='Population size')

More examples of graphing capabilities in R are available on the web, http://addictedtor.free.fr/graphiques/, and detailed instructions and illustrations are available in Murrell (2005).

Other *Plant Health Instructor* documents give examples of more complicated plots: Modeling Plant Disease Progress Over Time, Modeling Dispersal Gradients, Introduction to Spatial Analysis, Disease Forecasting and Validation for Plant Pathology.

Graphs can be saved in a PDF file (or a range of other formats) for later use and addition to documents. For example, the following command would save graphics output to the PDF file as named.

pdf(file='testplot.pdf')

Then, when output to that file is completed, the following command will turn off the ‘device’ that was moving output to the named file.

`dev.off()`

Using the interactive R windows, the simplest way to save a graph may be to right click on the graphics window and select the format for saving the graph from the menu. This operation depends on your operating system. Options may vary.

Next: Loops