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)


The analysis showed that cars with more weight, horsepower, or engine size tend to get lower miles per gallon. In other words, heavier and more powerful cars use more fuel. Laying out the three scatterplots side by side made it easier to compare these patterns and see how each factor affects fuel efficiency in a similar way. I kept the design simple by using clear labels, neutral colors, and a clean layout so the focus stayed on the data. Following Few’s design tips helped make the charts easy to read and understand without any distractions.

Comments

Popular posts from this blog

Final Project LIS4317

Module 11 assignment