mtcarshead(mtcars)
mtcarstail(mtcars)
# Create the histogram
my_plot <- ggplot(mtcars, aes(x = mpg)) +
geom_histogram(binwidth = 5, fill = "skyblue", color = "black") +
xlab("Miles/Gallon") +
ylab("Number of Cars") +
ggtitle("Histogram of Fuel Efficiency (mpg)")
my_plot
The histogram displays the distribution of fuel efficiency (miles per
gallon) for various cars in the mtcars dataset.
# Convert cylinder count to ordered factor
mtcars <- mutate(mtcars, cyl = factor(cyl, ordered = TRUE, levels = c(4, 6, 8)))
# Create the boxplot
my_boxplot <- ggplot(mtcars, aes(x = cyl, y = mpg)) +
geom_boxplot(fill = "orange", color = "darkred") +
xlab("Cylinders") +
ylab("Miles per Gallon") +
ggtitle("Boxplot of mpg by Cylinder Count")
my_boxplot
The boxplot compares fuel efficiency across cars with different cylinder counts:
Conclusion: Fuel efficiency decreases with an increase in cylinder count, revealing the trade-off between engine power and mpg.