library(readxl)
library(dplyr)
library(lme4)
library(dplyr)
library(ggplot2)

##### Import the Khan_etal_2025_Nitrousoxide_Multispecies.xlsx and name it data####
setwd("D:/AliN2O") 
data <- read_excel("Khan_etal_2025_Nitrousoxide_Multispecies.xlsx")
head(data)
str(data)

####Divide the dataset into year 1 and year 2####
year1 <- data |>
  filter(Year == '1')

year2 <- data |>
  filter(Year == '2')

###Final model for nitrous oxide emissions in year 1####
modelLogN2O_Y1 <- lm(log(N2O) ~ Nsource * NLvl * Plantsp, data = year1)
summary(aov(modelLogN2O_Y1))

###Final model for nitrous oxide emissions in year 2###
modelLogN2O_Y2 <- lm(log(N2O) ~ Nsource * NLvl * Plantsp, data = year2)
summary(aov(modelLogN2O_Y2))

###Final model nitrogen yield scaled nitrous oxide emissions in year 1###
modelLogEI_Y1 <- lm(log(EI_NBIOM) ~ Nsource * NLvl * Plantsp, data = year1)
summary(aov(modelLogEI_Y1))

###Final model nitrogen yield scaled nitrous oxide emissions in year 2###
modelLogEI_Y2 <- lm(log(EI_NBIOM) ~ Nsource * NLvl * Plantsp, data = year2)
summary(aov(modelLogEI_Y2))


##############################
#Analysis of emission factors#
##############################


###Filter the data for nitrous oxide emissions factor to remove 0 kg N (Control)###
data_EF <- data |>
  filter(Nsource %in% c('Dig', 'Inorg'))

####Divide the dataset into year 1 and year 2####
EF_Year1 <- data_EF |>
  filter(Year == '1')

EF_Year2 <- data_EF |>
  filter(Year == '2')

###Convert the dataframes to numeric###
EF_Year1$EF <- as.numeric(EF_Year1$EF)

EF_Year2$EF <- as.numeric(EF_Year2$EF)

data_EF$EF <- as.numeric(data_EF$EF)

###Final model for nitrous oxide emissions factor in year 1###
modelLogEF_Y1 <- lm(log(EF) ~ Nsource * NLvl * Plantsp, data = EF_Year1)
summary(aov(modelLogEF_Y1))

###As there were some negative values in the dataset, a co-efficient was added to log-transform the data###
c_value<- abs(min(EF_Year2$EF, na.rm = TRUE)) + 1

###Final model for nitrous oxide emissions factor in year 2###
modelLogEF_Y2 <- lm(log(EF + c_value) ~ Nsource * NLvl * Plantsp, data = EF_Year2)
summary(aov(modelLogEF_Y2))

###Make an average of emissions factor for year 1 and 2 and construct a new column 'EF_avg' with values###
EF_avg <- data_EF %>%
  group_by(Plot) %>%
  mutate(EF_avg = mean(EF, na.rm = TRUE)) %>% 
  filter(Year == 1) 

###Due to some negative values, a co-efficient was added to log-transform the values###
c_value <- abs(min(EF_avg$EF_avg, na.rm = TRUE)) + 1

###Final model for 2-years average emissions factor###
modelLogEF_avg <- lm(log(EF_avg + c_value) ~ Nsource * NLvl * Plantsp, data = EF_avg)
summary(aov(modelLogEF_avg))



##########################################################################
######Below is the code for constructing graphs in Fig. 3 and Fig. 4######
##########################################################################


###Note: All graphs should be viewed through the zoom function in Rstudio and use 2nd screen for best viewing experience###

###Change the N application rates to Zero, Low and High###
data <- data %>%
  mutate(
    NLvl = recode(NLvl,
                  "ZeroN" = "Zero",
                  "LowN"  = "Low",
                  "HighN" = "High"),
    NLvl = factor(NLvl, levels = c("Zero", "Low", "High"))
  )

###Filter the data for year1 and year 2###
Graph_data_Y1 <- data[data$Year == 1, ]
Graph_data_Y2 <- data[data$Year == 2, ]

###Define the levels for grassland communities in the dataset###
Graph_data_Y1$Plantsp <- factor(Graph_data_Y1$Plantsp, levels=c("PRG", "PRG+WC", "PRG+RC", "Mix"))

Graph_data_Y2$Plantsp <- factor(Graph_data_Y2$Plantsp, levels=c("PRG", "PRG+WC", "PRG+RC", "Mix"))
###check the levels###
with(Graph_data_Y1, levels(Plantsp))

with(Graph_data_Y2, levels(Plantsp))

###Define the levels for N application rates in the dataset####

Graph_data_Y1$NLvl <- factor(Graph_data_Y1$NLvl, levels=c("Zero", "Low", "High"))

Graph_data_Y2$NLvl <- factor(Graph_data_Y2$NLvl, levels=c("Zero", "Low", "High"))

###check the levels###
with(Graph_data_Y1, levels(NLvl))

with(Graph_data_Y2, levels(NLvl))

###Take the means and standard error for the treatment groups for N2O emissions###
plot_data_N2O_Y1 <- Graph_data_Y1 %>%  
  group_by(Nsource, NLvl, Plantsp) %>% 
  # There are replicates so taking their mean 
  summarise(Resp = mean(N2O), SE = mean(N2O_SE)) %>% 
  # Arrange the plot in correct order
  arrange(desc(Nsource)) %>% 
  ungroup() %>% 
  slice(1:4, 13:20, 5:12) %>% 
  # Create a dummy variable to be shown on X-axis
  # and a variable which holds the value for the colour of the bar
  mutate(# This fct_inorder ensures the bars show up in the same order as in the data
    Nsource = forcats::fct_inorder(Nsource),
    NLvl = forcats::fct_inorder(as.character(NLvl)),
    # These are the bar positions that have been custom-defined
    x_var = c(2,4,6,8, 11,13,15,17, 20,22,24,26, 29,31,33,35, 38,40,42,44))
    
# See data now has values for response, and automatically 

print(plot_data_N2O_Y1)

#####################################################
###Construct bar graph for N2O emissions in year 1###
#####################################################

barchart_N2O_Y1 <- barchart <- ggplot(data = plot_data_N2O_Y1, aes(x = x_var, y = Resp))+
  # Add bars on the plot
  geom_col(aes(group = Plantsp, fill = "grey"
  ), width = 0.5)+
  #errorbars
  geom_errorbar(aes(ymin = Resp-SE, ymax = Resp+SE), stat = "identity", width = 0.2, position = position_dodge(0.5), color = "black") +
  # Colour the bars
  scale_fill_identity()+
  # Theme (for the white background)
  theme_bw(26) +
  # Separate the bars for the three NSources
  facet_grid(~Nsource, scales = "free_x", space = "free_x", switch = "x") +
  # Adjust specific elements of plot
  theme(# Rotate the x-axis labels
    axis.text.x = element_text(angle = 60, vjust = 1, hjust = 1),
    # Decrease space between the three panels
    panel.spacing = unit(0.5, "lines"),
    # Removing some of the grey lines in background
    panel.grid.major.x = element_blank(),
    panel.grid.minor.y = element_blank(),
    # Remove border of panels
    panel.border = element_blank(),
    # Adjust aesthetics of panel labels
    strip.placement = "outside",
    strip.background = element_rect(fill = NA, colour = "NA"),
    strip.text = element_text(size = 0, margin = unit(c(20, 10, 5, 5), "pt")))+
  # Add the labels for the bars
  scale_x_continuous(breaks = plot_data_N2O_Y1$x_var,
                     labels = plot_data_N2O_Y1$Plantsp,
                     minor_breaks = c(9.5, 18.5, 27.5, 36.5)) +
  # Add the Nlvl labels 
  # Change the value of y (to adjust the position of the labels)
  # Increasing y will move the label up while decreasing will take it down
  geom_text(data = data.frame(x = c(5, 14, 23, 32, 41),
                              y = c(-800),
                              Plantsp = "NA",
                              label = c("Zero", "Low", "High", "Low", "High"),
                              Nsource = forcats::fct_inorder(c("None", "Dig", "Dig", "Inorg", "Inorg"))),
            aes(x = x, y = y, label = label), size = 7)+
  # Add the NSource labels 
  # Change the value of y (to adjust the position of the labels)
  # Increasing y will move the label up while decreasing will take it down
  geom_text(data = data.frame(x = c(5, 18.5, 36.5),
                              y = c(-1000),
                              Plantsp = "NA",
                              label = c("None", "Digestate", "Inorganic - Calcium ammonium nitrate (CAN)"),
                              Nsource = forcats::fct_inorder(c("None", "Dig", "Inorg"))),
            aes(x = x, y = y, label = label), size = 7)+
  # Add the line over Nlvl label
  # Change the value of y and yend (to adjust the position of the line)
  # Increasing y will move the line up while decreasing will take it down
  geom_segment(data = data.frame(x = c(2, 11, 20, 29, 37),
                                 y = c(-875),
                                 xend = c(8, 17, 26, 35, 44),
                                 yend = c(-875),
                                 Plantsp = "NA",
                                 Nsource = forcats::fct_inorder(c("None", "Dig", "Dig", "Inorg", "Inorg"))),
               aes(x = x, y = y, xend = xend, yend = yend),
               linewidth = 1)+
  # Add the line over NSource label
  # Change the value of y and yend (to adjust the position of the line)
  # Increasing y will move the line up while decreasing will take it down
  geom_segment(data = data.frame(x = c(2, 11, 29),
                                 y = c(-1075),
                                 xend = c(8, 26, 44),
                                 yend = c(-1075),
                                 Plantsp = "NA",
                                 Nsource = forcats::fct_inorder(c("None", "Dig", "Inorg"))),
               aes(x = x, y = y, xend = xend, yend = yend),
               linewidth = 1)+
  # Do not update this line
  coord_cartesian(ylim = c(0, 2700), expand = TRUE, clip = "off") +
  scale_y_continuous(breaks = seq(0, 2700, by = 900)) +
  # Labels of the X and Y axes
  labs(x = "", y = expression(paste("Nitrous oxide emissions (g ", " ", N[2]*O, "-N ha"^{-1}, " y"^{-1}, ")")))+
  theme(
    axis.title.y = element_text(hjust = 0.75) # Adjust vjust to move it down
  )

print(barchart_N2O_Y1)


#######################################################
####Construct bar graph for N2O emissions in year 2####
#######################################################

###Take mean and standard errors for N2O emissions in year 2###
plot_data_N2O_Y2 <- Graph_data_Y2 %>%  
  group_by(Nsource, NLvl, Plantsp) %>% 
  # There are replicates so taking their mean 
  summarise(Resp = mean(N2O), SE = mean(N2O_SE)) %>% 
  # Arrange the plot in correct order
  arrange(desc(Nsource)) %>% 
  ungroup() %>% 
  slice(1:4, 13:20, 5:12) %>% 
  # Create a dummy variable to be shown on X-axis
  # and a variable which holds the value for the colour of the bar
  mutate(# This fct_inorder ensures the bars show up in the same order as 
    # they are in the data
    Nsource = forcats::fct_inorder(Nsource),
    NLvl = forcats::fct_inorder(as.character(NLvl)),
    # These are the bar positions I have custom-defined
    x_var = c(2,4,6,8, 11,13,15,17, 20,22,24,26, 29,31,33,35, 38,40,42,44))
# See data now has values for response, and automatically 

print(plot_data_N2O_Y2)

###Construct bargraph for N2O emissions in year 2###
barchart_N2O_Y2 <- barchart <- ggplot(data = plot_data_N2O_Y2, aes(x = x_var, y = Resp))+
  # Add bars on the plot
  geom_col(aes(group = Plantsp, fill = "grey"
  ), width = 0.5)+
  #errorbars
  geom_errorbar(aes(ymin = Resp-SE, ymax = Resp+SE), stat = "identity", width = 0.2, position = position_dodge(0.5), color = "black") +
  # Colour the bars
  scale_fill_identity()+
  # Theme (for the white background)
  theme_bw(26) +
  # Separate the bars for the three NSources
  facet_grid(~Nsource, scales = "free_x", space = "free_x", switch = "x") +
  # Adjust specific elements of plot
  theme(# Rotate the x-axis labels
    axis.text.x = element_text(angle = 60, vjust = 1, hjust = 1),
    # Decrease space between the three panels
    panel.spacing = unit(0.5, "lines"),
    # Removing some of the grey lines in background
    panel.grid.major.x = element_blank(),
    panel.grid.minor.y = element_blank(),
    # Remove border of panels
    panel.border = element_blank(),
    # Adjust aesthetics of panel labels
    strip.placement = "outside",
    strip.background = element_rect(fill = NA, colour = "NA"),
    strip.text = element_text(size = 0, margin = unit(c(20, 10, 5, 5), "pt")))+
  # Add the labels for the bars
  scale_x_continuous(breaks = plot_data_N2O_Y2$x_var,
                     labels = plot_data_N2O_Y2$Plantsp,
                     minor_breaks = c(9.5, 18.5, 27.5, 36.5)) +
  # Add the Nlvl labels 
  # Change the value of y (to adjust the position of the labels)
  # Increasing y will move the label up while decreasing will take it down
  geom_text(data = data.frame(x = c(5, 14, 23, 32, 41),
                              y = c(-800),
                              Plantsp = "NA",
                              label = c("Zero", "Low", "High", "Low", "High"),
                              Nsource = forcats::fct_inorder(c("None", "Dig", "Dig", "Inorg", "Inorg"))),
            aes(x = x, y = y, label = label), size = 7)+
  # Add the NSource labels 
  # Change the value of y (to adjust the position of the labels)
  # Increasing y will move the label up while decreasing will take it down
  geom_text(data = data.frame(x = c(5, 18.5, 36.5),
                              y = c(-1000),
                              Plantsp = "NA",
                              label = c("None", "Digestate", "Inorganic - Calcium ammonium nitrate (CAN)"),
                              Nsource = forcats::fct_inorder(c("None", "Dig", "Inorg"))),
            aes(x = x, y = y, label = label), size = 7)+
  # Add the line over Nlvl label
  # Change the value of y and yend (to adjust the position of the line)
  # Increasing y will move the line up while decreasing will take it down
  geom_segment(data = data.frame(x = c(2, 11, 20, 29, 37),
                                 y = c(-875),
                                 xend = c(8, 17, 26, 35, 44),
                                 yend = c(-875),
                                 Plantsp = "NA",
                                 Nsource = forcats::fct_inorder(c("None", "Dig", "Dig", "Inorg", "Inorg"))),
               aes(x = x, y = y, xend = xend, yend = yend),
               linewidth = 1)+
  # Add the line over NSource label
  # Change the value of y and yend (to adjust the position of the line)
  # Increasing y will move the line up while decreasing will take it down
  geom_segment(data = data.frame(x = c(2, 11, 29),
                                 y = c(-1075),
                                 xend = c(8, 26, 44),
                                 yend = c(-1075),
                                 Plantsp = "NA",
                                 Nsource = forcats::fct_inorder(c("None", "Dig", "Inorg"))),
               aes(x = x, y = y, xend = xend, yend = yend),
               linewidth = 1)+
  # Do not update this line
  coord_cartesian(ylim = c(0, 2700), expand = TRUE, clip = "off") +
  scale_y_continuous(breaks = seq(0, 2700, by = 900)) +
  # Labels of the X and Y axes
  labs(x = "", y = expression(paste("Nitrous oxide emissions (g ", " ", N[2]*O, "-N ha"^{-1}, " y"^{-1}, ")")))+
  theme(
    axis.title.y = element_text(hjust = 0.75) # Adjust vjust to move it down
  )
print(barchart_N2O_Y2)


##############################################################################
####Construct bar graph for N2O emissions scaled by nitrogen yield, Year 1 ###
##############################################################################

plot_data_EI_Y1 <- Graph_data_Y1 %>%  
  group_by(Nsource, NLvl, Plantsp) %>% 
  # There are replicates so taking their mean 
  summarise(Resp = mean(EI_NBIOM), SE = mean(EI_NBIOM_SE)) %>% 
  # Arrange the plot in correct order
  arrange(desc(Nsource)) %>% 
  ungroup() %>% 
  slice(1:4, 13:20, 5:12) %>% 
  # Create a dummy variable to be shown on X-axis
  # and a variable which holds the value for the colour of the bar
  mutate(# This fct_inorder ensures the bars show up in the same order as 
    # they are in the data
    Nsource = forcats::fct_inorder(Nsource),
    NLvl = forcats::fct_inorder(as.character(NLvl)),
    # These are the bar positions I have custom-defined
    x_var = c(2,4,6,8, 11,13,15,17, 20,22,24,26, 29,31,33,35, 38,40,42,44))
    
# See data now has values for response, and automatically 

print(plot_data_EI_Y1)

###Construct bargraph for emissions intensity in year 1###
barchart_EI_Y1 <- barchart <- ggplot(data = plot_data_EI_Y1, aes(x = x_var, y = Resp))+
  # Add bars on the plot
  geom_col(aes(group = Plantsp, fill = "grey"
  ), width = 0.5)+
  #errorbars
  geom_errorbar(aes(ymin = Resp-SE, ymax = Resp+SE), stat = "identity", width = 0.2, position = position_dodge(0.5), color = "black") +
  # Colour the bars
  scale_fill_identity()+
  # Theme (for the white background)
  theme_bw(26) +
  # Separate the bars for the three NSources
  facet_grid(~Nsource, scales = "free_x", space = "free_x", switch = "x") +
  # Adjust specific elements of plot
  theme(# Rotate the x-axis labels
    axis.text.x = element_text(angle = 60, vjust = 1, hjust = 1),
    # Decrease space between the three panels
    panel.spacing = unit(0.5, "lines"),
    # Removing some of the grey lines in background
    panel.grid.major.x = element_blank(),
    panel.grid.minor.y = element_blank(),
    # Remove border of panels
    panel.border = element_blank(),
    # Adjust aesthetics of panel labels
    strip.placement = "outside",
    strip.background = element_rect(fill = NA, colour = "NA"),
    strip.text = element_text(size = 0, margin = unit(c(20, 10, 5, 5), "pt")))+
  # Add the labels for the bars
  scale_x_continuous(breaks = plot_data_EI_Y1$x_var,
                     labels = plot_data_EI_Y1$Plantsp,
                     minor_breaks = c(9.5, 18.5, 27.5, 36.5)) +
  # Add the Nlvl labels 
  # Change the value of y (to adjust the position of the labels)
  # Increasing y will move the label up while decreasing will take it down
  geom_text(data = data.frame(x = c(5, 14, 23, 32, 41),
                              y = c(-2.75),
                              Plantsp = "NA",
                              label = c("Zero", "Low", "High", "Low", "High"),
                              Nsource = forcats::fct_inorder(c("None", "Dig", "Dig", "Inorg", "Inorg"))),
            aes(x = x, y = y, label = label), size = 7)+
  # Add the NSource labels 
  # Change the value of y (to adjust the position of the labels)
  # Increasing y will move the label up while decreasing will take it down
  geom_text(data = data.frame(x = c(5, 18.5, 36.5),
                              y = c(-3.25),
                              Plantsp = "NA",
                              label = c("None", "Digestate", "Inorganic - Calcium ammonium nitrate (CAN)"),
                              Nsource = forcats::fct_inorder(c("None", "Dig", "Inorg"))),
            aes(x = x, y = y, label = label), size = 7)+
  # Add the line over Nlvl label
  # Change the value of y and yend (to adjust the position of the line)
  # Increasing y will move the line up while decreasing will take it down
  geom_segment(data = data.frame(x = c(2, 11, 20, 29, 37),
                                 y = c(-3),
                                 xend = c(8, 17, 26, 35, 44),
                                 yend = c(-3),
                                 Plantsp = "NA",
                                 Nsource = forcats::fct_inorder(c("None", "Dig", "Dig", "Inorg", "Inorg"))),
               aes(x = x, y = y, xend = xend, yend = yend),
               linewidth = 1)+
  # Add the line over NSource label
  # Change the value of y and yend (to adjust the position of the line)
  # Increasing y will move the line up while decreasing will take it down
  geom_segment(data = data.frame(x = c(2, 11, 29),
                                 y = c(-3.5),
                                 xend = c(8, 26, 44),
                                 yend = c(-3.5),
                                 Plantsp = "NA",
                                 Nsource = forcats::fct_inorder(c("None", "Dig", "Inorg"))),
               aes(x = x, y = y, xend = xend, yend = yend),
               linewidth = 1)+
  # Do not update this line
  coord_cartesian(ylim = c(0, 10), expand = TRUE, clip = "off") +
  scale_y_continuous(breaks = seq(0, 10, by = 2)) +
  # Labels of the X and Y axes
  labs(x = "", y = expression(paste("Emissions intensity (g ", " ", N[2]*O, "-N ha"^{-1}, " y"^{-1},"/NY kg"^{-1}, " ha"^{-1}, " y"^{-1}, ")")))+
  theme(
    axis.title.y = element_text(hjust = 0.95) # Adjust vjust to move it down
  )
print(barchart_EI_Y1)




##############################################################################
####Construct bar graph for N2O emissions scaled by nitrogen yield, Year 2 ###
##############################################################################


plot_data_EI_Y2 <- Graph_data_Y2 %>%  
  group_by(Nsource, NLvl, Plantsp) %>% 
  # There are replicates so taking their mean 
  summarise(Resp = mean(EI_NBIOM), SE = mean(EI_NBIOM_SE)) %>% 
  # Arrange the plot in correct order
  arrange(desc(Nsource)) %>% 
  ungroup() %>% 
  slice(1:4, 13:20, 5:12) %>% 
  # Create a dummy variable to be shown on X-axis
  # and a variable which holds the value for the colour of the bar
  mutate(# This fct_inorder ensures the bars show up in the same order as 
    # they are in the data
    Nsource = forcats::fct_inorder(Nsource),
    NLvl = forcats::fct_inorder(as.character(NLvl)),
    # These are the bar positions I have custom-defined
    x_var = c(2,4,6,8, 11,13,15,17, 20,22,24,26, 29,31,33,35, 38,40,42,44))
# See data now has values for response, and automatically 
print(plot_data_EI_Y2)

###Construct bargraph for emissions intensity in year 2###
barchart_EI_Y2 <- barchart <- ggplot(data = plot_data_EI_Y2, aes(x = x_var, y = Resp))+
  # Add bars on the plot
  geom_col(aes(group = Plantsp, fill = "grey"
  ), width = 0.5)+
  #errorbars
  geom_errorbar(aes(ymin = Resp-SE, ymax = Resp+SE), stat = "identity", width = 0.2, position = position_dodge(0.5), color = "black") +
  # Colour the bars
  scale_fill_identity()+
  # Theme (for the white background)
  theme_bw(26) +
  # Separate the bars for the three NSources
  facet_grid(~Nsource, scales = "free_x", space = "free_x", switch = "x") +
  # Adjust specific elements of plot
  theme(# Rotate the x-axis labels
    axis.text.x = element_text(angle = 60, vjust = 1, hjust = 1),
    # Decrease space between the three panels
    panel.spacing = unit(0.5, "lines"),
    # Removing some of the grey lines in background
    panel.grid.major.x = element_blank(),
    panel.grid.minor.y = element_blank(),
    # Remove border of panels
    panel.border = element_blank(),
    # Adjust aesthetics of panel labels
    strip.placement = "outside",
    strip.background = element_rect(fill = NA, colour = "NA"),
    strip.text = element_text(size = 0, margin = unit(c(20, 10, 5, 5), "pt")))+
  # Add the labels for the bars
  scale_x_continuous(breaks = plot_data_EI_Y2$x_var,
                     labels = plot_data_EI_Y2$Plantsp,
                     minor_breaks = c(9.5, 18.5, 27.5, 36.5)) +
  # Add the Nlvl labels 
  # Change the value of y (to adjust the position of the labels)
  # Increasing y will move the label up while decreasing will take it down
  geom_text(data = data.frame(x = c(5, 14, 23, 32, 41),
                              y = c(-2.75),
                              Plantsp = "NA",
                              label = c("Zero", "Low", "High", "Low", "High"),
                              Nsource = forcats::fct_inorder(c("None", "Dig", "Dig", "Inorg", "Inorg"))),
            aes(x = x, y = y, label = label), size = 7)+
  # Add the NSource labels 
  # Change the value of y (to adjust the position of the labels)
  # Increasing y will move the label up while decreasing will take it down
  geom_text(data = data.frame(x = c(5, 18.5, 36.5),
                              y = c(-3.25),
                              Plantsp = "NA",
                              label = c("None", "Digestate", "Inorganic - Calcium ammonium nitrate (CAN)"),
                              Nsource = forcats::fct_inorder(c("None", "Dig", "Inorg"))),
            aes(x = x, y = y, label = label), size = 7)+
  # Add the line over Nlvl label
  # Change the value of y and yend (to adjust the position of the line)
  # Increasing y will move the line up while decreasing will take it down
  geom_segment(data = data.frame(x = c(2, 11, 20, 29, 37),
                                 y = c(-3),
                                 xend = c(8, 17, 26, 35, 44),
                                 yend = c(-3),
                                 Plantsp = "NA",
                                 Nsource = forcats::fct_inorder(c("None", "Dig", "Dig", "Inorg", "Inorg"))),
               aes(x = x, y = y, xend = xend, yend = yend),
               linewidth = 1)+
  # Add the line over NSource label
  # Change the value of y and yend (to adjust the position of the line)
  # Increasing y will move the line up while decreasing will take it down
  geom_segment(data = data.frame(x = c(2, 11, 29),
                                 y = c(-3.5),
                                 xend = c(8, 26, 44),
                                 yend = c(-3.5),
                                 Plantsp = "NA",
                                 Nsource = forcats::fct_inorder(c("None", "Dig", "Inorg"))),
               aes(x = x, y = y, xend = xend, yend = yend),
               linewidth = 1)+
  # Do not update this line
  coord_cartesian(ylim = c(0, 10), expand = TRUE, clip = "off") +
  scale_y_continuous(breaks = seq(0, 10, by = 2)) +
  # Labels of the X and Y axes
  labs(x = "", y = expression(paste("Emissions intensity (g ", " ", N[2]*O, "-N ha"^{-1}, " y"^{-1},"/NY kg"^{-1}, " ha"^{-1}, " y"^{-1}, ")")))+
  theme(
    axis.title.y = element_text(hjust = 0.95) # Adjust vjust to move it down
  )
print(barchart_EI_Y2)
