Module # 8 Correlation Analysis and ggplot2
# Step 1: Load Libraries
library(ggplot2)
library(gridExtra)
# Step 2: Load Dataset
data(mtcars)
head(mtcars)
# Step 3: Explore Relationships (Correlation)
# Compute correlations between MPG and other numeric variables
cor_matrix <- cor(mtcars)
print(cor_matrix)
# Look specifically at correlation between mpg and wt
cor_mpg_wt <- cor(mtcars$mpg, mtcars$wt)
cat("Correlation between MPG and Weight:", cor_mpg_wt, "\n")
# Step 4: Build a Linear Regression Model
model <- lm(mpg ~ wt, data = mtcars)
summary(model)
# Step 5: Visualizations
# A. Simple regression plot for MPG vs Weight
p1 <- ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point(color = "steelblue", size = 3) +
stat_smooth(method = "lm", se = TRUE, color = "darkred", linewidth = 1) +
labs(title = "Relationship Between Weight and MPG",
x = "Weight (1000 lbs)",
y = "Miles per Gallon (MPG)") +
theme_minimal(base_size = 14)
# B. Regression plot for MPG vs Horsepower
p2 <- ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point(color = "steelblue", size = 3) +
stat_smooth(method = "lm", se = TRUE, color = "darkred", linewidth = 1) +
labs(title = "Relationship Between Horsepower and MPG",
x = "Horsepower",
y = "Miles per Gallon (MPG)") +
theme_minimal(base_size = 14)
# C. Regression plot for MPG vs Displacement
p3 <- ggplot(mtcars, aes(x = disp, y = mpg)) +
geom_point(color = "steelblue", size = 3) +
stat_smooth(method = "lm", se = TRUE, color = "darkred", linewidth = 1) +
labs(title = "Relationship Between Displacement and MPG",
x = "Displacement (cu.in.)",
y = "Miles per Gallon (MPG)") +
theme_minimal(base_size = 14)
# Step 6: Arrange Multiple Plots in a Grid Layout
grid.arrange(p1, p2, p3, ncol = 3)
Comments
Post a Comment