Week 2 Quiz:Exploratory Data Analysis(Data Science Specialization):Answers2025
Question 1
Under the lattice graphics system, what do the primary plotting functions like xyplot() and bwplot() return?
✅ an object of class “trellis”
❌ nothing; only a plot is made
❌ an object of class “lattice”
❌ an object of class “plot”
Explanation:
Lattice plotting functions return an object of class “trellis”, which represents the entire plot and can be printed or modified later.
Question 2
What is produced by the following code?
library(nlme)
library(lattice)
xyplot(weight ~ Time | Diet, BodyWeight)
✅ A set of 3 panels showing the relationship between weight and time for each diet.
❌ A set of 11 panels showing the relationship between weight and diet for each time.
❌ A set of 16 panels showing the relationship between weight and time for each rat.
❌ A set of 3 panels showing the relationship between weight and time for each rat.
Explanation:Diet has 3 factor levels, so conditioning by Diet (| Diet) creates 3 panels, one for each diet, showing weight vs Time.
Question 3
Which of the following functions can be used to annotate the panels in a multi-panel lattice plot?
✅ panel.abline()
❌ text()
❌ lines()
❌ points()
❌ axis()
Explanation:
In lattice, annotation within panels is handled by panel functions such as panel.abline(), panel.points(), etc., not base graphics functions.
Question 4
The following code does NOT result in a plot appearing:
library(lattice)
data(airquality)
p <- xyplot(Ozone ~ Wind | factor(Month), data = airquality)
✅ The object ‘p’ has not yet been printed with the appropriate print method.
❌ There is a syntax error in xyplot()
❌ Variables not found in dataset
❌ xyplot() sends plots to PDF by default
Explanation:
Lattice plots only appear when printed. In an interactive session, xyplot() prints automatically, but if assigned to a variable (p), you must explicitly call print(p).
Question 5
In the lattice system, which function controls the appearance of all lattice plots?
✅ trellis.par.set()
❌ splom()
❌ print.trellis()
❌ par()
Explanation:trellis.par.set() adjusts global graphical parameters (colors, line types, fonts) for lattice plots, similar to par() in base graphics.
Question 6
What is ggplot2 an implementation of?
✅ the Grammar of Graphics developed by Leland Wilkinson
❌ a 3D visualization system
❌ the S language from Bell Labs
❌ the base plotting system in R
Explanation:ggplot2 implements the Grammar of Graphics, providing a structured way to build plots layer by layer.
Question 7
To visualize how ozone vs wind varies by month using ggplot2:
✅
airquality = transform(airquality, Month = factor(Month))
qplot(Wind, Ozone, data = airquality, facets = . ~ Month)
❌ qplot(Wind, Ozone, data = airquality, facets = . ~ factor(Month))
❌ qplot(Wind, Ozone, data = airquality, geom = "smooth")
❌ qplot(Wind, Ozone, data = airquality)
Explanation:
Converting Month to a factor ensures proper paneling across months using facets = . ~ Month.
Question 8
What is a geom in ggplot2?
✅ a plotting object like point, line, or other shape
❌ a method for mapping data to color/size
❌ a statistical transformation
❌ a method for conditioning plots
Explanation:
A geom defines the type of plot layer, e.g., geom_point(), geom_line(), etc.
Question 9
Why does this code fail to produce a plot?
g <- ggplot(movies, aes(votes, rating))
print(g)
✅ ggplot does not yet know what type of layer to add to the plot.
❌ The dataset is too large
❌ Syntax error
❌ Object ‘g’ has no print method
Explanation:ggplot() only defines the aesthetic mappings. You must add a layer (like + geom_point()) to specify what to draw.
Question 10
Add a smoother to a ggplot2 scatterplot:
✅ qplot(votes, rating, data = movies) + geom_smooth()
❌ qplot(votes, rating, data = movies) + stats_smooth("loess")
❌ qplot(votes, rating, data = movies, panel = panel.loess)
❌ qplot(votes, rating, data = movies, smooth = "loess")
Explanation:
In ggplot2, layers are added with +. geom_smooth() adds a smoothing curve (LOESS or linear).
🧾 Summary Table
| Q# | ✅ Correct Answer | Key Concept |
|---|---|---|
| 1 | object of class “trellis” | Lattice plots return Trellis objects |
| 2 | 3 panels by diet | Conditioning by factor in lattice |
| 3 | panel.abline() | Panel functions annotate lattice plots |
| 4 | Object not printed | Lattice plots show after print() |
| 5 | trellis.par.set() | Controls lattice theme |
| 6 | Grammar of Graphics | ggplot2 theory |
| 7 | transform + qplot(… facets = . ~ Month) | Faceted plot in ggplot2 |
| 8 | plotting object like point/line | Geom definition |
| 9 | ggplot needs layer | Layer-based system |
| 10 | + geom_smooth() | Add smoothing layer in ggplot2 |