R includes several packages for visualizing data:
before making you chart, you need to think in the following:
You can close the graphics device for your system with dev.off() or set it by dev.set() or turn all the graphics devices with graphics.off().
The base graphics system has many parameters that can set and tweaked for the whole session using par() function. Any of these parameters can be overridden as arguments to specific plotting functions. Some important parameters are:
you can get the default value of a parameter by par(“param_name”)
now try to add some red points to it (this can be used make different types of points on the same scatterplot)
Now lets create a plot in a PDF file.
nothing will appear on the screen but a file names “testPlot.pdf” will be created in your working directory and contains the histogram. Notice that you have to call dev.off() to close the PDF device.
You can copy your plot to another device. This is useful because some plots require a lot of code and it can be a pain to type all that in again for a different device.
Major lattice functions
Lattice functions generally take a formula for their first argument, usually of the form y ~ x | f * g where x, y are the x, y variables, after | are the conditioning variables (optional). The second argument is the data frame or list from which the variables in the formula should be obtained. If no data frame or list is passed, then the parent frame is used. If no other arguments are passed, there are defaults that can be used. To see some example in action before getting all the details, run the following:
> library(lattice)
> library(nlme)
> xyplot(distance ~ age | Subject, data = Orthodont)
> xyplot(distance ~ age | Subject, data = Orthodont, type = “b”)
Lattice functions behave differently from base graphics functions. Base graphics functions plot data directly on graphics device. Lattice graphics functions return an object of class trellis which can be stored. On the command line, trellis objects are auto printed; otherwise you have to print the trellis object.
Lattice functions have a panel function that controls what happens inside each panel of the entire plot. Lets create two sets of random values that are linearly related; and create a factor level that will be used as condition between them :
> x <- rnorm(100)
> y <- x + rnorm(100, sd = 0.5)
> f <- gl(2, 50, labels = c(“Group 1”, “Group 2”))
> xyplot(y ~ x | f)
In the previous example we used the default panel function to draw each panel but we can write our own function to draw panels. In below example we just added a line representing the median of y.
> xyplot(y ~ x | f,
+ panel = function(x, y, …) {
+ panel.xyplot(x, y, …)
+ panel.abline(h = median(y), lty = 2) }
+ )
R can produce LaTeX-like symbols on a plot for mathematical annotation. Math symbols in R are expressions that need to be wrapped in expression() function and passed to plotting functions (that accepts text like text, mtext, axis, legend). The output will be formatted according to LaTeX-like rules. ?plotmath gives you a list of allowed symbols. The following shows LaTex-like chart, x-axis, and y-axis titles:
> plot(x, y, main=expression(theta==0), ylab=expression(hat(gamma)==0), xlab=expression(sum(x[i]*y[i],i==1,n)))
You can concatenate strings with LaTex symbols using *. like :
If you want to use a computed value in the annotation, use substitute() to substitute the right hand side variable with your computed value (provided in list()).
In this post we explored the basic charting capabilities in R using both base package and lattice package. Stay tuned for more R notes.