# ch17: Print examples of chapter 17 of 'R for Dummies'. In rfordummies: Code Examples to Accompany the Book "R for Dummies"

 ch17 R Documentation

## Print examples of chapter 17 of 'R for Dummies'.

### Description

To print a listing of all examples of a chapter, use `ch17()`. To run all the examples of `ch17()`, use `example(ch17)`.

### Usage

```ch17()
```

`toc`

Other Chapters: `ch01()`, `ch02()`, `ch03()`, `ch04()`, `ch05()`, `ch06()`, `ch07()`, `ch08()`, `ch09()`, `ch10()`, `ch11()`, `ch12()`, `ch13()`, `ch14()`, `ch15()`, `ch16()`, `ch18()`, `ch19()`, `ch20()`

### Examples

```if (interactive()) {
# Chapter 17 - Creating Faceted Graphics with Lattice

# Creating a Lattice Plot

str(mtcars)

library("lattice")

## Making a lattice scatterplot

xyplot(mpg ~ hp | factor(cyl), data=mtcars)

xyplot(mpg ~ hp | factor(cyl), data=mtcars,
type=c("p", "r"))

# Changing Plot Options

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)",
)

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)
)

## Changing the font size of titles and 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)
)

## Using themes to modify plot options

xyplot(mpg ~ hp | factor(cyl), data=mtcars,
type=c("p", "r"),
par.settings=simpleTheme(col="red", col.line="blue")
)

# Plotting Different Types

## Making a bar chart

mtcars\$cars <- rownames(mtcars)

barchart(cars ~ mpg | factor(cyl), data=mtcars,
main="barchart",
scales=list(cex=0.5),
layout=c(3, 1)
)

## Making a box-and-whisker plot

bwplot(~ hp | factor(cyl), data=mtcars, main="bwplot")

# Plotting Data in Groups

## Using data in tall format

str(longley)
library("reshape2")
mlongley <- melt(longley, id.vars="Year")

str(mlongley)

xyplot(value ~ Year | variable, data=mlongley,
layout=c(6, 1),
par.strip.text=list(cex=0.7),
scales=list(cex=0.7)
)

## Creating a chart with groups

mtcars\$cars <- rownames(mtcars)
mtcars\$am <- with(mtcars, ifelse(am==0, "Automatic", "Manual"))

barchart(cars ~ mpg | factor(cyl), data=mtcars,
group=am,
scales=list(cex=0.5),
layout=c(3, 1),
)

barchart(cars ~ mpg | factor(cyl), data=mtcars,
main="barchart with groups",
group=am,
auto.key=TRUE,
par.settings = simpleTheme(col=c("grey80", "grey20")),
scales=list(cex=0.5),
layout=c(3, 1)
)

# Printing and Saving a Lattice Plot

## Assigning a lattice plot to an object

my.plot <- xyplot(mpg ~ hp | cyl, data=mtcars)
class(my.plot)

## Printing a lattice plot in a script

xyplot(mpg ~ hp | cyl, data=mtcars)

my.plot <- xyplot(mpg ~ hp | cyl, data=mtcars)
print(my.plot)

## Saving a lattice plot to file

filename <- file.path(tempdir(), "xyplot")
## Not run:
trellis.device(device="png", filename=filename)

## End(Not run)
print(my.plot)
## Not run:
dev.off()

## End(Not run)

}
```

rfordummies documentation built on March 18, 2022, 6:04 p.m.