# Load necessary libraries
library(ggplot2)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
# Load the mtcars dataset
data(mtcars)
# Histogram of MPG
ggplot(mtcars, aes(x = mpg)) +
geom_histogram(binwidth = 3, fill = "steelblue", color = "black") +
labs(title = "Distribution of Miles Per Gallon",
subtitle = "Histogram of MPG in the mtcars dataset",
x = "Miles Per Gallon (mpg)",
y = "Count",
caption = "Data Source: mtcars") +
theme_minimal()

# Scatter plot of HP vs MPG
ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point(color = "darkred", size = 3) +
geom_smooth(method = "lm", color = "blue", se = FALSE) +
labs(title = "Horsepower vs. Miles Per Gallon",
subtitle = "Scatter plot showing the relationship between horsepower and fuel efficiency",
x = "Horsepower (hp)",
y = "Miles Per Gallon (mpg)",
caption = "Data Source: mtcars") +
theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'

# Pie chart of cylinder distribution
cyl_data <- mtcars %>%
count(cyl) %>%
mutate(percentage = n / sum(n) * 100,
cyl_label = paste0(cyl, " Cyl (", round(percentage, 1), "%)"))
ggplot(cyl_data, aes(x = "", y = n, fill = factor(cyl))) +
geom_bar(stat = "identity", width = 1, color = "white") +
coord_polar(theta = "y") +
labs(title = "Car Distribution by Number of Cylinders",
subtitle = "Proportion of cars in mtcars dataset based on cylinder count",
fill = "Cylinders",
caption = "Data Source: mtcars") +
theme_minimal() +
theme(axis.text.x = element_blank(), axis.ticks = element_blank())
