# Load required library
library(ggplot2)
# -----------------------------
# 1. Import Dataset
# -----------------------------
# The built-in 'mtcars' dataset is used (no need to import externally)
data <- mtcars
# View first few rows
head(data)
# Check structure of the dataset
str(data)
# Summary statistics
summary(data)
# Convert 'cyl' (cylinders) into a categorical variable
data$cyl <- as.factor(data$cyl)
# -----------------------------
# 2. Scatter Plot: Horsepower vs. MPG
# -----------------------------
# This scatter plot shows the relationship between Horsepower (hp) and Miles Per
Gallon (mpg)
ggplot(data, aes(x = hp, y = mpg)) +
geom_point(color = "blue") +
ggtitle("Scatter Plot: Horsepower vs. MPG") +
xlab("Horsepower (hp)") +
ylab("Miles Per Gallon (mpg)") +
theme_minimal()
# -----------------------------
# 3. Histogram: Distribution of MPG
# -----------------------------
# A histogram helps to see how MPG values are distributed in the dataset
ggplot(data, aes(x = mpg)) +
geom_histogram(binwidth = 2, fill = "green", color = "black") +
ggtitle("Histogram: Distribution of MPG") +
xlab("Miles Per Gallon (MPG)") +
ylab("Frequency") +
theme_minimal()
# -----------------------------
# 4. Bar Chart: Count of Cylinders
# -----------------------------
# A bar chart to visualize the number of cars for each cylinder type
ggplot(data, aes(x = cyl, fill = cyl)) +
geom_bar() +
ggtitle("Bar Chart: Count of Cars by Cylinder Type") +
xlab("Number of Cylinders") +
ylab("Count of Cars") +
theme_minimal()
# -----------------------------
# 5. Box Plot: MPG for Different Cylinders
# -----------------------------
# A box plot to compare MPG across different cylinder types
ggplot(data, aes(x = cyl, y = mpg, fill = cyl)) +
geom_boxplot() +
ggtitle("Box Plot: MPG by Cylinder Type") +
xlab("Number of Cylinders") +
ylab("Miles Per Gallon (MPG)") +
theme_minimal()