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 library below.
library(ggplot2)
# library(ggplot2)
Histogram of mpg
# Write your code below.
# Generate a histogram of 'mpg' (Miles per Gallon) from the mtcars dataset
ggplot(mtcars, aes(x = mpg)) +
geom_histogram(binwidth = 2, fill = "blue", color = "black") +
labs(title = "Histogram of Miles Per Gallon (mpg)", x = "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.
# Generate boxplots of `mpg` by `cyl`
ggplot(mtcars, aes(x = factor(cyl), y = mpg)) +
geom_boxplot(fill = "lightblue") +
labs(title = "Boxplot of mpg by Cylinder", x = "Number of Cylinders", y = "mpg")
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.
# MultiLine chart of 'wt' on the x-axis and 'mpg' on the y-axis, with separate lines for each transmission type ('am')
ggplot(mtcars, aes(x = wt, y = mpg, color = factor(am))) +
geom_line() +
geom_point() +
labs(title = "MultiLine Chart of wt vs mpg by Transmission",
x = "Weight (wt)",
y = "Miles per Gallon (mpg)",
color = "Transmission (am)") +
scale_color_discrete(labels = c("Automatic", "Manual"))
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.
# Generate a barchart with car names on the x-axis and car weight ('wt') on the y-axis
# To make the chart more readable, we rotate the x-axis labels so that car names are visible vertically
ggplot(mtcars, aes(x = rownames(mtcars), y = wt)) +
geom_bar(stat = "identity", fill = "lightgreen") +
labs(title = "Car Weight by Model", x = "Car Model", y = "Weight (wt)") +
theme(axis.text.x = element_text(angle = 90, 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).
# Generate a scatter chart with 'mpg' on the x-axis and 'wt' on the y-axis
# The color and size of each point represent 'qsec' (Quarter Mile Time), showing how fast each car is
ggplot(mtcars, aes(x = mpg, y = wt, color = qsec, size = qsec)) +
geom_point() +
labs(title = "Scatter Chart of wt vs mpg with qsec",
x = "Miles per Gallon (mpg)",
y = "Weight (wt)",
color = "Quarter Mile Time (qsec)",
size = "Quarter Mile Time (qsec)")