Advertisement
Online Test Banks
Score higher
See Online Test Banks
eLearning
Learning anything is easy
Browse Online Courses
Mobile Apps
Learning on the go
Explore Mobile Apps
Dummies Store
Shop for books and more
Start Shopping

How to Change Lattice Plot Options in R

R has a very good reputation for being able to create publication-quality graphics, however if you want to use your lattice graphics in reports or documents, you’ll probably want to change the plot options.

The lattice package makes use of the grid graphics engine, which is completely different from base graphics.

How to add titles and labels to Lattice plots in R

To add a main title and axis labels to a lattice plot, you can specify the following arguments:

  • main: Main title

  • xlab: x-axis label

  • ylab: y-axis label

    image0.jpg
> xyplot(mpg ~ hp | factor(cyl), data=mtcars,
+   type=c("p", "r"),
+   main="Fuel economy vs. Performance",
+   xlab="Performance (horse power)",
+   ylab="Fuel economy (miles per gallon)",
+ )

How to change the font size of Lattice titles and labels in R

To change the size of your labels, you need to modify your arguments to be lists. Similar to base graphics, you specify a cex argument in lattice graphics to modify the character expansion ratio. For example, to reduce the main title and axis label text to 75 percent of standard size, specify cex=0.75 as an element in the list argument to main, xlab, and ylab.

To keep it simple, build up the formatting of your plot step by step. Start by changing the size of your main title to cex=0.75:

> xyplot(mpg ~ hp | factor(cyl), data=mtcars,
+   type=c("p", "r"),
+   main=list(
+     label="Fuel economy vs. Performance given Number of Cylinders",
+     cex=0.75)
+ )

Do you see what happened? Your argument to main now contains a list with two elements: label and cex.

You construct the arguments for xlab and ylab in exactly the same way. Each argument is a list that contains the label and any other formatting options you want to set. Expand your code to modify the axis labels:

> xyplot(mpg ~ hp | factor(cyl), data=mtcars,
+   type=c("p", "r"),
+   main=list(
+     label="Fuel economy vs. Performance given Number of Cylinders",
+     cex=0.75),
+   xlab=list(
+     label="Performance (horse power)",
+     cex=0.75),
+   ylab=list(
+     label="Fuel economy (miles per gallon)",
+     cex=0.75),
+   scales=list(cex=0.5)
+ )

If you look carefully, you’ll see that the code includes an argument to modify the size of the scales text to 50 percent of standard (scales=list(cex=0.5)).

image1.jpg

How to use themes to modify Lattice plot options

One neat feature of lattice graphics is that you can create themes to change the plot options of your charts. To do this, you need to use the par.settings argument. The par.settings argument in lattice is similar.

The easiest way to use the par.settings argument is to use it in conjunction with the simpleTheme() function. With simpleTheme(), you can specify the arguments for the following:

  • col, col.points, col.line: Control the colors of symbols, points, lines, and other graphics elements such as polygons

  • cex, pch, font: Control the character expansion ratio (cex), plot character (pch), and font type

  • lty, lwd: Control the line type and line width

For example, to modify your plot to have red points and a blue regression line, use the following:

image2.jpg
> xyplot(mpg ~ hp | factor(cyl), data=mtcars,
+   type=c("p", "r"),
+   par.settings=simpleTheme(col="red", col.line="blue")
+ )
  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win an iPad Mini. Enter to win now!