title: “Exploring Vehicle Performance and Fuel Efficiency” author: “Niveditha Kamath” output: html_document

knitr::opts_chunk$set(echo = TRUE) library(ggplot2) library(dplyr) library(plotly) library(corrplot)

Introduction

This project analyzes the mtcars dataset to understand the relationship between fuel efficiency and vehicle characteristics. The dataset contains information about 32 automobiles and several performance variables such as miles per gallon, horsepower, weight, cylinders, and transmission type.

Visualization 1: Distribution of MPG

ggplot(mtcars, aes(x = mpg)) +
  geom_histogram(bins = 10, fill = "steelblue") +
  labs(
    title = "Distribution of Miles Per Gallon",
    x = "Miles Per Gallon",
    y = "Count"
  )

Visualization 2: Average MPG by Cylinders

mtcars %>%
  group_by(cyl) %>%
  summarise(avg_mpg = mean(mpg)) %>%
  ggplot(aes(x = factor(cyl), y = avg_mpg)) +
  geom_col(fill = "darkgreen") +
  labs(
    title = "Average MPG by Number of Cylinders",
    x = "Cylinders",
    y = "Average MPG"
  )

Visualization 3: Weight vs MPG

ggplot(mtcars,
       aes(x = wt,
           y = mpg,
           color = factor(cyl))) +
  geom_point(size = 3) +
  labs(
    title = "Vehicle Weight vs Fuel Efficiency",
    x = "Weight (1000 lbs)",
    y = "Miles Per Gallon",
    color = "Cylinders"
  )

Visualization 4: Box Plot of MPG by Cylinders

ggplot(mtcars,
       aes(x = factor(cyl),
           y = mpg,
           fill = factor(cyl))) +
  geom_boxplot() +
  labs(
    title = "MPG Distribution by Cylinder Count",
    x = "Cylinders",
    y = "MPG",
    fill = "Cylinders"
  )

Visualization 5: Correlation Heatmap

corrplot(
  cor(mtcars),
  method = "color",
  type = "upper"
)

Visualization 6: Horsepower vs MPG

ggplot(mtcars,
       aes(x = hp,
           y = mpg)) +
  geom_point(color = "red") +
  geom_smooth(method = "lm") +
  labs(
    title = "Horsepower and Fuel Efficiency",
    x = "Horsepower",
    y = "Miles Per Gallon"
  )

Visualization 7: Transmission Type Distribution

mtcars$Transmission <- ifelse(mtcars$am == 1,
                              "Manual",
                              "Automatic")

ggplot(mtcars,
       aes(x = Transmission,
           fill = Transmission)) +
  geom_bar() +
  labs(
    title = "Distribution of Transmission Types",
    x = "Transmission Type",
    y = "Count"
  )

Visualization 8: Interactive Plotly Visualization

p <- ggplot(
  mtcars,
  aes(
    x = wt,
    y = mpg,
    color = factor(cyl)
  )
) +
  geom_point(size = 3) +
  labs(
    title = "Interactive Weight vs MPG",
    x = "Weight",
    y = "MPG",
    color = "Cylinders"
  )

ggplotly(p)

Conclusion

The analysis shows that vehicle weight, horsepower, and cylinder count have a significant impact on fuel efficiency. Vehicles with lower weight and fewer cylinders generally achieve higher miles per gallon. The interactive visualization further demonstrates the relationship between weight and fuel efficiency across different cylinder categories.