Appendix F. Solutions to Chapter Exercises

A solution is provided for each exercise in the book. Do not look at the solution until you have made a serious effort to solve the exercise! For many problems, there will be several possible solutions in R. If you come up with a solution different from the one provided, try to see if the two solutions are equivalent—do you get the same answer? Why or why not?

Exercises 1-1 Through 1-4

Solutions provided in the chapter.

Exercise 3-1

attach(mtcars)
stripchart(mpg ~ cyl, method = "jitter")

This helps to separate the cars a bit. Now we can see how many cars are in each group.

Not surprisingly, cars with fewer cylinders get better gas mileage.

Exercise 3-2

install.packages("plotrix", dependencies=TRUE)
library(plotrix)
attach(trees)
dotplot.mtb(Volume)

A type of jittering is automatic. Even so, some values that are very close still run together. One way to deal with this is to make the plot character smaller:

dotplot.mtb(Volume, pch = 20) # or
dotplot.mtb(Volume, pch = ".") # too small!
dotplot.mtb(Volume, pch = "/")  # Hmm...
detach(trees)

Exercise 4-1

dotchart(USArrests$Murder, labels = row.names(USArrests))

The state names are so big, they overwrite and become illegible!

Exercise 4-2

load("Nimrod.rda") # .rda shows it was saved as an R data frame
dotchart(Nimrod$time)

Good!

dotchart(Nimrod$time, labels = Nimrod$performer, cex = .5)

Better!

 Nimrod2 = Nimrod[order(Nimrod$time),] dotchart(Nimrod2$time, labels = Nimrod2$performer, ...

Get Graphing Data with R now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.