Quiz 2 Exploratory Data
AnalysisUnder the lattice graphics system, what do the primary plotting
functions like xyplot() and bwplot()
return?
According to slide 7 from the Lattice Plotting System in R document:
- Lattice graphics functions return an object of the class trellis.
What is produced by the following code?
library(nlme)
library(lattice)
xyplot(weight ~ Time | Diet, BodyWeight)
First, we need to investigate the nlme package and what
is inside of BodyWeight dataset.
library(nlme)
library(lattice)
# First rows of BodyWeight dataset.
head(nlme::BodyWeight)
## Grouped Data: weight ~ Time | Rat
## weight Time Rat Diet
## 1 240 1 1 1
## 2 250 8 1 1
## 3 255 15 1 1
## 4 260 22 1 1
## 5 262 29 1 1
## 6 258 36 1 1
# Counting the number of Diet factors
length(unique(nlme::BodyWeight$Diet))
## [1] 3
We already know the graphic should present 3 facets.
Also, according to the Lattice Graphic notation, weight
will be on the y-axis and Time on the x-axis.
# Printing the Lattice Graphic to prove it.
xyplot(weight ~ Time | Diet, BodyWeight)
The graphic will display three facets presenting how each
Diet will affect the Weight over
Time.
Annotation of plots in any plotting system involves adding points, lines, or text to the plot, in addition to customizing axis labels or adding titles. Different plotting systems have different sets of functions for annotating plots in this way.
Which of the following functions can be used to annotate the panels in a multi-panel lattice plot?
Based on slides 11 and 12 from The
Lattice Plotting System in R document, I found a way to add an
annotation in Lattice using the panel.xyz(), where
xyz() could be abline lmline or other function.
The example on slide 12 uses the panel.lmline()
annotation.
To better understand llines(), I have used the
?llines.
The following code does NOT result in a plot appearing on the screen device.
library(lattice)
library(datasets)
data(airquality)
p <- xyplot(Ozone ~ Wind | factor(Month), data = airquality)
Which of the following is an explanation for why no plot appears?
The graphic is OK unless it lacks a printing command to show it. The auto-printing will only work if the plot is not assigned to a variable, which is the current case.
In the lattice system, which of the following functions can be used to finely control the appearance of all lattice plots?
?trellis.par.set.
Description
Functions used to query, display and modify graphical parameters for fine control of Trellis displays. Modifications are made to the settings for the currently active device only.
What is ggplot2 an implementation of?
Found in slide 2 of ggplot2 document.
An implementation of the Grammar of Graphics by Leland Wilkinson
Load the `airquality’ dataset form the datasets package in R
library(datasets)
data(airquality)
I am interested in examining how the relationship between ozone and wind speed varies across each month. What would be the appropriate code to visualize that using ggplot2?
qplot(Wind, Ozone, data = airquality, facets = . ~ factor(Month))qplot(Wind, Ozone, data = airquality)qplot(Wind, Ozone, data = airquality, geom = "smooth")airquality = transform(airquality, Month = factor(Month)); qplot(Wind, Ozone, data = airquality, facets = .~Month)The transform() function explicitly ensures the Month
variable will be a factor. Although, the . ~factor(Month)
is more neatly for me.
What is a geom in the ggplot2 system?
According to the slide 24 of Ggplot2 Document.
geoms: geometric objects like points, lines, shapes.
When I run the following code I get an error:
library(ggplot2)
library(ggplot2movies)
g <- ggplot(movies, aes(votes, rating))
print(g)
I was expecting a scatterplot of ‘votes’ and ‘rating’ to appear. What’s the problem?
It is necessary to install the ggplot2movies package.
The problem relies on the lack of geom method to select the way graphic
will display the data.
The following code creates a scatterplot of ‘votes’ and ‘rating’ from the movies dataset in the ggplot2 package. After loading the ggplot2 package with the library() function, I can run
qplot(votes, rating, data = movies)
How can I modify the the code above to add a smoother to the scatterplot?
There are two ways to do it:
Option 1 - Example on slide 9 from Gpplot2 document
# Adding geom argument on qplot function:
qplot(votes, rating, data = movies, geom = c("point","smooth"))
Option 2 - Example on slide 19 from Gpplot2 document
# Adding geom_smooth to qplot function:
qplot(votes, rating, data = movies) + geom_smooth()