In Chapter 12, we explored many different ways to “look at” the
numbers. For this lab, let’s explore the mtcars
dataset
that is included within R.
This activity description does not provide the same level of code
prompts as previous labs – it is assumed that you remember or can look
up the necessary code. The overall goal of this activity is to use
ggplot2
to show different attributes of the
mtcars
dataset. Please be sure to include both the code and
the images that were generated with your assignment.
Add all of your libraries that you use for this assignment here.
# Add your libraries here
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.4.3
# Generate the following visualizations:
## Step 1 - Histogram
# Write your code below.
# Histogram of mpg
ggplot(mtcars, aes(x = mpg)) +
geom_histogram(binwidth = 2, fill = "skyblue", color = "black") +
labs(title = "Histogram of MPG", x = "Miles per Gallon (mpg)", y = "Count")
Boxplots of mpg
by cyl
(i.e. 3 box plots:
one for all cars with 4 cylinders, one for all cars with 6 cylinders,
and one with all the cars with 8 cylinders).
# Write your code below.
# Boxplots of mpg by cyl
ggplot(mtcars, aes(x = factor(cyl), y = mpg, fill = factor(cyl))) +
geom_boxplot() +
labs(title = "MPG by Number of Cylinders", x = "Cylinders", y = "Miles per Gallon (mpg)") +
scale_fill_manual(values = c("orange", "forestgreen", "dodgerblue")) +
theme(legend.position = "none")
MultiLine chart of wt
on the x-axis, mpg
for the y-axis. With a line for each am
(i.e. two lines).
Also be sure to show each point on the chart.
# Write your code below.
# MultiLine chart of wt vs mpg for each am, with points
ggplot(mtcars, aes(x = wt, y = mpg, group = factor(am), color = factor(am))) +
geom_line() +
geom_point(size = 3) +
labs(title = "MPG vs Weight by Transmission", x = "Weight (wt)", y = "Miles per Gallon (mpg)", color = "AM (0 = Auto, 1 = Manual)") +
scale_color_manual(values = c("red", "blue"))
Barchart with the x-axis being the name of each car, and the height
being wt
. Make sure to rotate the x-axis labels, so we can
actually read the car name.
# Write your code below.
# Barchart with car names on x-axis, wt as height
ggplot(mtcars, aes(x = reorder(rownames(mtcars), wt), y = wt)) +
geom_bar(stat = "identity", fill = "violet") +
labs(title = "Car Weight by Model", x = "Car Name", y = "Weight (wt)") +
theme(axis.text.x = element_text(angle = 60, hjust = 1))
Scatter chart with the x-axis being the mpg
and the
y-axis being the wt
of the car. Have the color and the size
of each “symbol” (i.e., circle) represent how fast the car goes (based
on the qsec
attribute).
# Write your code below.
# Scatter chart: mpg vs wt, color and size mapped to qsec
ggplot(mtcars, aes(x = mpg, y = wt, color = qsec, size = qsec)) +
geom_point(alpha = 0.7) +
labs(title = "MPG vs Weight Colored by Quarter Mile Time",
x = "Miles Per Gallon (mpg)", y = "Weight (wt)", color = "qsec", size = "qsec") +
scale_color_gradient(low = "gold", high = "purple")