# Load required 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
# Summarize data to calculate mean and standard error
data_summary <- mtcars %>%
group_by(cyl) %>%
summarise(
mean_mpg = mean(mpg),
se_mpg = sd(mpg) / sqrt(n())
)
# Create a bar plot with error bars
ggplot(data_summary, aes(x = factor(cyl), y = mean_mpg)) +
geom_bar(stat = "identity", fill = "skyblue", color = "black", width = 0.6) + # Bar plot
geom_errorbar(aes(ymin = mean_mpg - se_mpg, ymax = mean_mpg + se_mpg),
width = 0.2, color = "red") + # Error bars
labs(title = "Bar Plot of Mean MPG by Cylinders",
x = "Number of Cylinders",
y = "Mean MPG") +
theme_minimal() # Aesthetic improvements
