In this activity, we explore the mtcars dataset, which
contains information about various car models. The variables
include:
| Variable | Description | Type |
|---|---|---|
mpg |
Miles per gallon | Numeric |
cyl |
Number of cylinders | Numeric |
disp |
Displacement (cubic inches) | Numeric |
hp |
Gross horsepower | Numeric |
drat |
Rear axle ratio | Numeric |
wt |
Weight (1000 lbs) | Numeric |
qsec |
1/4 mile time (seconds) | Numeric |
vs |
Engine shape (0 = V-shaped, 1 = straight) | Binary |
am |
Transmission (0 = automatic, 1 = manual) | Binary |
gear |
Number of forward gears | Numeric |
carb |
Number of carburetors | Numeric |
install.packages("rlang")
if (!require(tidyverse)) install.packages("tidyverse")
if (!require(ggplot2)) install.packages("ggplot2")
library(tidyverse)
library(ggplot2)
data_mtcars <- mtcars
data_mtcars$am <- as.factor(data_mtcars$am)
data_mtcars$cyl <- as.factor(data_mtcars$cyl)
head(data_mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
Weight vs. Miles Per Gallon, colored by number of cylinders.
ggplot(data_mtcars, aes(x = wt, y = mpg, color = cyl)) +
geom_point(size = 3) +
labs(
title = "Weight vs. Miles Per Gallon",
x = "Weight (1000 lbs)",
y = "Miles Per Gallon",
color = "Cylinders"
)
Miles Per Gallon by row index across all car models.
data_mtcars_line <- data_mtcars %>% mutate(index = row_number())
ggplot(data_mtcars_line, aes(x = index, y = mpg)) +
geom_line(color = "steelblue", linewidth = 1) +
geom_point(color = "steelblue") +
labs(
title = "Miles Per Gallon by Index",
x = "Index",
y = "Miles Per Gallon"
)
Average horsepower grouped by cylinder count.
hp_by_cyl <- data_mtcars %>%
group_by(cyl) %>%
summarize(avg_hp = mean(hp))
ggplot(hp_by_cyl, aes(y = cyl, x = avg_hp, fill = cyl)) +
geom_bar(stat = "identity", show.legend = FALSE) +
labs(
title = "Average Horsepower by Cylinder Count",
y = "Cylinder Count",
x = "Average Horsepower"
)
Average mpg, disp, hp, and wt grouped by cylinder count.
bar_data_mtcars <- data_mtcars %>%
group_by(cyl) %>%
summarize(
mpg = mean(mpg),
disp = mean(disp),
hp = mean(hp),
wt = mean(wt)
) %>%
pivot_longer(
cols = c("mpg", "disp", "hp", "wt"),
names_to = "Measurement",
values_to = "Average"
)
ggplot(bar_data_mtcars, aes(x = cyl, y = Average, fill = Measurement)) +
geom_bar(stat = "identity") +
labs(
title = "Average Measurements by Cylinder Count",
x = "Cylinder Count",
y = "Average Measurement",
fill = "Measurement"
)
This notebook demonstrates four core data visualization techniques in
R using the built-in mtcars dataset: scatter plots, line
graphs, and both horizontal and vertical stacked bar charts. Each chart
reveals a different aspect of the relationship between engine
characteristics and performance.