CODE
Explanation of Code Components and Interpretation
ggplot(data = mpg, aes(x = displ, y = hwy, color = drv))
Initializes the plot, specifying the mpg dataset.
aes() (aesthetic mappings) defines how variables are mapped to visual properties:
x = displ: Engine displacement is mapped to the horizontal axis.
y = hwy: Highway miles per gallon is mapped to the vertical axis.
color = drv: Drivetrain type (front-wheel, rear-wheel, or 4-wheel drive) is mapped to the color of the points and lines, allowing for differentiation between drivetrain types.
geom_point(alpha = 0.6)
This layer adds individual data points for each car model.
alpha = 0.6 sets the transparency of the points, which is useful for visualizing dense areas of data without overplotting obscuring individual points.
geom_smooth(method = "lm", se = FALSE)
This critical layer adds smoothed conditional means (trend lines) to the plot for each drivetrain type (because color = drv is specified in the global aes()).
method = "lm": Specifies that a linear model (straight line) should be used to fit the trend for each group. Other methods like "loess" (local regression) could be used for non-linear trends.
se = FALSE: This argument prevents the display of the standard error around the trend line. Standard error bands typically show the uncertainty or confidence interval of the estimated trend. By setting se = FALSE, these bands are removed, which can make the graph cleaner and easier to compare multiple trend lines, especially when the primary focus is the general relationship rather than the statistical precision of each line.
labs(...)
Customizes the plot title, axis labels, and legend title for better readability.
title = "Highway Fuel Economy vs. Engine Size by Drivetrain Type"
x = "Engine Displacement (liters)"
y = "Highway Miles Per Gallon (MPG)"
color = "Drivetrain"
scale_color_discrete(labels = c("4-wheel drive", "Front-wheel drive", "Rear-wheel drive"))
Improves the legend labels, replacing the default "4", "f", "r" with more descriptive text.
theme_minimal()
Applies a clean, minimalist theme to the plot, removing unnecessary visual clutter like grey backgrounds and heavy grid lines.