R - Plot (Graphic System)

> Procedural Languages > R

1 - Formula

The general form of a formula specifying a plot is

y ˜ x | g

where:

  • y is the coordinate on the vertical axis,
  • x is the coordinate on the horizontal axis,
  • and g is a grouping factor or expression.

For special cases like the histogram, the vertical axis is pre-specified. In these cases y is omitted.

2 - Graphic System

To read https://cran.r-project.org/web/views/Graphics.html

The R graphics system is composed of:

  • graphics packages;
  • graphics systems:
  • a graphics engine and devices;
  • and graphics device packages
Advertising

2.1 - Device

All graphics output is directed to an active device that will dictate the output format that will be produced.

Only one device can be currently active and receive the graphics output.

# Open a device
dev.new()
# Close a device
dev.off()
# List of open devices
dev.list()
# List of the current (ie active) open device
dev.cur()
# Make a device active
dev.set()
# Copies all output from the active device to another device.
dev.copy() 
# Make the next or previous device active
dev.next() and dev.prev()
# Close all open devices
graphics.off()

Example in png.

png(file="myplot.png")
plot(data)
dev.off()

The user can control the format of the default device using the options() function.

List:

3 - Syntax

plot(reponseVariable~predictorVariable,data.frame)
### Or
plot(data.frame$predictorVariable,data.frame$reponseVariable)

See help

?plot
Advertising

4 - Character

Plotting character (pch)

An easy way to see them all:

plot(1:20,1:20,pch=1:20,cex=2)

5 - Documentation / Reference