R Graphics: Cheat-Sheet

R Intro tutorial: Graphics

Help about graphics

> help(graphics)

List functions in base graphic library:
> library(help = "graphics")

Show help about X Window System graphics:
> help(x11)

Create a graphic device

> x11() # In UNIX based systems, create a graphic device.

A new blank window appears.

High level functions

Draw a set of dots from a vector:
> x = 1:100
> plot(x)

Plot points using X and Y coordinates:
> y = x*2 # y shall have same length as x
> plot(x,y)

Plot an histogram
> hist(x) # plot a histogram.

> hist(x, breaks=5) # plot an histogram using 5 cells.

Draw a 3D figure in perspective:
> x = seq(0, 1, length=100) # X grid
> y = seq(0, 1, length=100) # Y grid
> z = outer(x,y, function(x, y) {((x-0.5)*(y-0.5))^2}) # Inverted parabolic curve
> persp(x,y,z) # Draw the image.

Show logarithmic axes:
> plot(sin(x), log="x") # x axe is logarithmic.
> plot(sin(x), log="xy") # both x and y axes are logarithmic.

Write lines instead of dots:
> plot(sin(x), log="x", type="l") # write lines instead of dots.

Write + instead of dots:
> plot(sin(x), pch='+')

Label X and Y axes:
> plot(sin(x), log="x", type="l", xlab="Foo", ylab="Bar")

Set a title for the figure:
> plot(sin(x), log="x", type="l", xlab="Foo", ylab="Bar", main="Example")

Low level functions

Draw a point at X, Y coordinates
> points(10,0.5)

Graphic parameters

> par() # list parameters for current graphic device