# ----------------------------------- # STEP 1: Create Dataset # ----------------------------------- # Example: Plant growth (cm) under 3 fertilizers fertilizer <- rep(c("A", "B", "C"), each = 10) height <- c( 15,16,14,15,17,16,15,14,16,15, # A 18,19,17,18,20,19,18,17,19,18, # B 12,13,11,12,14,13,12,11,13,12 # C ) data <- data.frame(fertilizer, height) # View dataset print(data) # ----------------------------------- # STEP 2: Install & Load Libraries # ----------------------------------- install.packages("dplyr") # Run only once install.packages("ggplot2") # Run only once library(dplyr) library(ggplot2) # ----------------------------------- # STEP 3: Calculate Mean & SD # ----------------------------------- summary_data <- data %>% group_by(fertilizer) %>% summarise( mean_height = mean(height), sd_height = sd(height) ) print(summary_data) # ----------------------------------- # STEP 4: Create Bar Chart with SD # ----------------------------------- plot <- ggplot(summary_data, aes(x = fertilizer, y = mean_height, fill = fertilizer)) + # Bars with BLACK BORDER geom_bar( stat = "identity", width = 0.6, color = "black" ) + # Add Standard Deviation (Error Bars) geom_errorbar( aes( ymin = mean_height - sd_height, ymax = mean_height + sd_height ), width = 0.2, linewidth = 0.8 ) + # Labels labs( title = "Bar Chart with Standard Deviation", x = "Fertilizer Type", y = "Mean Plant Height (cm)" ) + # Theme and centered title theme_minimal() + theme( legend.position = "none", plot.title = element_text(hjust = 0.5) ) + # Color palette scale_fill_brewer(palette = "Set2") # Display plot print(plot)