Si618f08 - week3 graphics - Interspike Codex

Si618f08 - week3 graphics

I found that png and jpg won't work on bart due to lack of X11. Several graphics output formats are available as you can see by saying help(device) at an R prompt.

I chose pdf, since it's easy to view. Here's how I checked to see that it works in R:

> setwd ("/u/mcq/HTML")
> pdf(file="myplot.pdf")
> plot (1:10)
> rect(1,5,3,7,col="white")
> dev.off()
null device
          1

Note that any graphics commands between the pdf(file... line and the dev.off() line will cause this file to be modified. The usual thing in R is to build up a graph with a few commands. The pdf(file ... command is where you can set options for the size and shape of the graphic and whether the background is transparent. You can say help(pdf) to find out a lot of the options. You can do the same for help(plot) and help(rect).

R tries to make a lot of assumptions if you leave things out. For example, in the above plot(1:10) you are supposed to specify the variables on the x axis, then the variables on the y axis, then any other plot attributes. Since we just gave the numbers 1 through 10 by saying 1:10, R used those for the x variables, then reused them for the y variables. The result is a plot of the points (1,1), (2,2) and so on.

rect() can draw lots of rectangles with one command, but here we've just said to draw a white one with the left, bottom, right, and top edges at the given coordinates.

The expression "null device 1" simply tells me that R is no longer sending its graphics anywhere and that I should include another pdf(file... command before I use any more graphics commands.

To view this graphic, I gave the following url to my browser:

 http://bart.si.umich.edu/~mcq/myplot.pdf

I usually use the Skim pdf viewer (on Mac OS X) because it allows me to refresh a pdf without closing and reopening the window, unlike Preview. For Windows I believe a similar program is PDF Annotator. For Linux I think some versions of Xpdf, including one called qpdf allow refreshing a window without closing when a pdf file is updated.

To test a more impressive looking example, try the following. This is a famous data set that convinced people of the importance of looking at scatterplots in more than two dimensions.

> library(lattice)
> data(iris)>
> pdf (file="iris.pdf")
> splom(~ iris)
> dev.off()
null device
          1

To see why this data set is so interesting, try this color version, as suggested by the wikipedia page on the iris data:

 http://en.wikipedia.org/wiki/Iris_flower_data_set

where you can see some background on it.

> pdf(file="iris-color.pdf")
> pairs(iris[1:4],main="Iris Data (red=setosa,green=versicolor,blue=virginica)", pch=21, bg=c("red","green3","blue")[unclass(iris$Species)])
> dev.off()
null device
          1

You should notice that it's hard to find a single line in any single scatterplot that separates the green and blue, although it's quite easy to separate the red.