Creating Scatter Plot: MPG vs. Horsepower
ggplot(mtcars, aes(x = hp, y = mpg, color = factor(cyl), shape = factor(cyl))) +
geom_point(size = 4, alpha = 0.8) +
labs(
title = "Miles Per Gallon vs Horsepower",
subtitle = "Relationship between fuel efficiency and engine power",
x = "Horsepower (hp)",
y = "Miles Per Gallon (mpg)",
caption = "Data: mtcars dataset (1974 Motor Trend magazine)",
color = "Cylinders",
shape = "Cylinders"
) +
scale_x_continuous(breaks = seq(0, 350, by = 50)) +
scale_y_continuous(breaks = seq(0, 40, by = 5)) +
theme_minimal() +
theme(
plot.title = element_text(size = 16, face = "bold"),
plot.subtitle = element_text(size = 12),
axis.title.x = element_text(size = 12),
axis.title.y = element_text(size = 12),
legend.position = "bottom"
)
