0% found this document useful (0 votes)
37 views2 pages

R Code Final Code For Group Bar

R code Final code for group bar

Uploaded by

Arghya Paul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views2 pages

R Code Final Code For Group Bar

R code Final code for group bar

Uploaded by

Arghya Paul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

# Load necessary libraries

library(ggplot2)
library(readr)
library(dplyr)
library(tidyr)

# 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")

# Load the data from the CSV file


file_path <- "Natural enemy [Link]"
data <- read_csv(file_path)

# Check column names


print(colnames(data))

# Select relevant columns and replace non-numeric values with NA


data <- data %>% select(Treatment, `Lady bird beetle`, `Black ant`, `Spider`)
data[, 2:4] <- lapply(data[, 2:4], function(x) ifelse([Link]([Link](x)), NA,
[Link](x)))

# Define the error as 5% of the values


data <- data %>%
mutate(errors_ladybird = 0.05 * `Lady bird beetle`,
errors_blackant = 0.05 * `Black ant`,
errors_spider = 0.05 * `Spider`)

# Convert data to long format for ggplot


data_long <- data %>%
pivot_longer(cols = c(`Lady bird beetle`, `Black ant`, `Spider`),
names_to = "NaturalEnemy",
values_to = "Reduction") %>%
mutate(Error = case_when(
NaturalEnemy == "Lady bird beetle" ~ errors_ladybird,
NaturalEnemy == "Black ant" ~ errors_blackant,
NaturalEnemy == "Spider" ~ errors_spider
)) %>%
filter(![Link](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, custom colors, and borders
p <- ggplot(data_long, aes(x = Treatment, y = Reduction, fill = NaturalEnemy)) +
geom_bar(position = position_dodge(width = 0.8), stat = "identity", width = 0.7,
colour = "black", alpha=0.5) + # Add borders to bars
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.6, size = 2.3) +
labs(title = "Natural Enemy Reduction over Control 1st, 2nd and 3r Spider",
x = "Biorational Insecticides", y = "Percent Reduction Over Control") +
theme_minimal(base_size = 11) +
theme([Link].x = element_text(angle = 45, hjust = 1, size = 9),
[Link].y = element_text(size = 10),
[Link] = element_text(hjust = 0.5, face = "bold", size = 10), # Reduced
title size to 10
[Link] = "top",
[Link] = element_blank()) +
scale_fill_manual(values = c("Lady bird beetle" = "orange1", "Black ant" =
"red2", "Spider" = "cyan3"))

# Print the plot with custom colors and borders


print(p)

You might also like