# Set the treatments
treatments <- c("Neem oil", "Mahogony oil", "Bio-Clean 5 SL", "Bio-action 1.5 SL",
"Biotrin 0.5 AS", "Spinomax 44.03 SC", "BiomaxM 1.2 EC", "Untreated control")
# Set the working directory
setwd("E:\\") # Make sure to use double backslashes or forward slashes
# Load necessary libraries
library(ggplot2)
library(readr)
library(dplyr)
library(tidyr)
# Load the data from the CSV file
file_path <- "RPB reduction over control 456.csv"
data <- read_csv(file_path)
# Define the error as 5% of the values
data <- data %>%
mutate(errors_24hrs = 0.05 * `After 24 hrs`,
errors_48hrs = 0.05 * `After 48 hrs`,
errors_72hrs = 0.05 * `After 72 hrs`)
# Convert data to long format for ggplot
data_long <- data %>%
pivot_longer(cols = c(`After 24 hrs`, `After 48 hrs`, `After 72 hrs`),
names_to = "Time",
values_to = "Reduction") %>%
mutate(Error = case_when(
Time == "After 24 hrs" ~ errors_24hrs,
Time == "After 48 hrs" ~ errors_48hrs,
Time == "After 72 hrs" ~ errors_72hrs
)) %>%
filter(!is.na(Reduction)) # Remove NA values
# Assign treatments to the data
data_long$Treatment <- factor(data_long$Treatment, levels = treatments)
# Create the bar plot with error bars and custom colors
p <- ggplot(data_long, aes(x = Treatment, y = Reduction, fill = Time)) +
geom_bar(position = position_dodge(width = .8), stat = "identity", width = .7) +
geom_errorbar(aes(ymin = Reduction - Error, ymax = Reduction + Error),
position = position_dodge(width = 0.8), width = 0.2, color =
"black") +
geom_text(aes(label = round(Reduction, 1)),
position = position_dodge(width = 0.8), vjust = -1.5, size = 3) +
labs(title = "Red Pumpkin Beetle Reduction Over Control",
x = "Biorational Insecticides", y = "Percent Reduction Over Control") +
theme_minimal(base_size = 11) +
theme(axis.text.x = element_text(angle = 35, hjust = .9, size = 10),
axis.text.y = element_text(size = 10),
plot.title = element_text(hjust = 0.5, face = "bold", size = 10), # Reduced
title size to 12
legend.position = "top",
legend.title = element_blank()) +
scale_fill_manual(values = c("After 24 hrs" = "brown4", "After 48 hrs" =
"darkblue", "After 72 hrs" = "chartreuse4"))
# Print the plot with custom colors
print(p)