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.
ggplot(mtcars, aes(x = mpg)) +
geom_histogram(bins = 10, fill = "steelblue") +
labs(
title = "Distribution of Miles Per Gallon",
x = "Miles Per Gallon",
y = "Count"
)
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"
)
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"
)
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"
)
corrplot(
cor(mtcars),
method = "color",
type = "upper"
)
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"
)
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"
)
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)
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.