Module 8. Assignment
For this week's lab, I used R's built-in mtcars dataset to look into how weight and horsepower of a car affect its gas mileage (MPG). I did both association and regression studies, and both showed strong negative relationships: cars that are larger and more powerful get worse gas mileage.
I used ggplot2 to make a scatter plot with a regression line and a segmented comparison that puts both factors next to each other. With Facets, it was easy to compare two relationships without all the extra stuff. Few told me to use bland colors, little ink, and clear labels, which made it easy to read and understand the falling trends.
R code
mtcars
# Use cor() to compute correlation matrices.
cor_matrix <- cor(mtcars[, c("mpg", "wt", "hp", "disp")])
round(cor_matrix, 2)
m_mpg_wt <- lm(mpg ~ wt, data = mtcars)
summary(m_mpg_wt)
library(ggplot2)
library(tidyr)
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point(color = "steelblue", size = 3) + # scatter
stat_smooth(method = "lm", color = "red", se = TRUE) + # regression line
labs(title = "MPG vs Weight",
x = "Weight (1000 lbs)",
y = "Miles per Gallon (MPG)") +
theme_minimal()
mtcars_long <- mtcars |>
dplyr::select(mpg, wt, hp) |>
pivot_longer(cols = c(wt, hp), names_to = "variable", values_to = "value")
ggplot(mtcars_long, aes(x = value, y = mpg)) +
geom_point(color = "darkorange", size = 2) +
stat_smooth(method = "lm", color = "red", se = TRUE) +
facet_wrap(~ variable, scales = "free_x") +
labs(title = "MPG Compared to Weight and Horsepower",
x = "Predictor Value",
y = "Miles per Gallon (MPG)") +
theme_bw()
I was surprised by how clearly the results showed trends since this was the first time I had used R to do correlation and regression. I saw that the MPG went down as the car's weight or horsepower went up. It was easy to see that because both of the red regression lines went down, showing a strong negative link.
How did your use of grid layout or facets enhance interpretation?
The facet structure really helped me get a better sense of the similarity. Instead of putting everything on one chart, it was easier to look at two easy plots next to each other. The fact that each panel showed how a different variable affected MPG made it a lot easier for me to understand at first.
In your opinion, how do Few’s recommendations help or hinder your design choices?
Few's advice to keep images clean and simple really did help a lot. Since I'm still learning, my graphs looked better and were easier to understand when I used fewer colors and a clear background. It also helped me keep my mind on the facts instead of getting sidetracked by the details of the design
Comments
Post a Comment