> library(ggplot2)
> library(dplyr)
> library(scales)  # Load scales package
> library(gridExtra)
> 
> data$End_week_date <- as.Date(data$End_week_date)
> print("Converted End_week_date to Date format")
[1] "Converted End_week_date to Date format"
> 
> harvest_periods <- New_data %>%
+   filter(Crop_Progression > 0)  %>%
+   group_by(Year) %>% 
+   summarise(start = min(End_week_date), end = max(End_week_date))
> print("Harvest periods calculated")
[1] "Harvest periods calculated"
> print(head(harvest_periods))  # Display first few rows of harvest_periods
# A tibble: 6 × 3
   Year start      end       
  <dbl> <date>     <date>    
1  2005 2005-09-23 2005-11-11
2  2006 2006-09-15 2006-11-24
3  2007 2007-09-21 2007-11-23
4  2008 2008-09-26 2008-11-28
5  2009 2009-10-02 2009-12-25
6  2010 2010-09-17 2010-11-05
> 
> harvest_periods$start <- as.Date(harvest_periods$start)
> harvest_periods$end <- as.Date(harvest_periods$end)
> 
> print("Start and end dates in harvest periods converted to Date format")
[1] "Start and end dates in harvest periods converted to Date format"
> 
> # Function to create plots for specified date ranges
> create_basis_plot <- function(data, start_date, end_date, harvest_periods) {
+   print(paste("Creating basis plot for range:", start_date, "to", end_date))
+   
+   # Filter data based on the provided date range
+   filtered_data <- data %>%
+     filter(End_week_date >= as.Date(start_date) & End_week_date < as.Date(end_date))
+   print(paste("Filtered data for the specified date range:", nrow(filtered_data), "rows"))
+   
+   # Define date_breaks within the function
+   date_breaks <- seq(from = as.Date("2005-01-01"), to = as.Date("2025-01-01"), by = "1 year")
+   print(paste("Date breaks for x-axis:", paste(date_breaks, collapse = ", ")))
+   
+   # Create the plot
+   p <- ggplot(filtered_data) +
+     geom_line(aes(x = End_week_date, y = DEC_BASIS, color = "DEC_BASIS"), size = 0.5) +
+     geom_line(aes(x = End_week_date, y = PEO_BASIS, color = "PEO_BASIS"), size = 0.5) +
+     geom_line(aes(x = End_week_date, y = ESL_BASIS, color = "ESL_BASIS"), size = 0.5) +
+     geom_rect(data = harvest_periods %>% 
+                 filter(Year >= as.numeric(format(as.Date(start_date), "%Y")) & 
+                          Year < as.numeric(format(as.Date(end_date), "%Y"))),
+               aes(xmin = start, xmax = end, ymin = -Inf, ymax = Inf),
+               fill = "darkgrey", alpha = 0.3) +
+     labs(title = "Basis", x = "Date", y = "Basis Value") +
+     scale_color_manual(values = c("DEC_BASIS" = "darkblue", 
+                                   "PEO_BASIS" = "darkgreen", 
+                                   "ESL_BASIS" = "darkred")) +
+     guides(color = "none") +  # Remove legend
+     scale_y_continuous(breaks = scales::pretty_breaks(n = 10)) +
+     scale_x_date(
+       breaks = date_breaks,
+       labels = scales::date_format("%Y"),
+       limits = c(min(filtered_data$End_week_date), max(filtered_data$End_week_date))  # Set limits based on data
+     ) +
+     theme_minimal(base_size = 10) +
+     theme(
+       axis.text.x = element_text(angle = 45, hjust = 1, size = 14),
+       axis.text.y = element_text(size = 14),
+       axis.title.x = element_text(size = 16),
+       axis.title.y = element_text(size = 16),
+       plot.title = element_text(size = 18, face = "bold")
+     )
+   
+   print("Basis plot created")
+   return(p)
+ }
> 
> # Function to create futures price plots for specified date ranges
> create_futures_plot <- function(data, start_date, end_date, harvest_periods) {
+   print(paste("Creating futures plot for range:", start_date, "to", end_date))
+   
+   # Filter data based on the provided date range
+   filtered_data <- data %>%
+     filter(End_week_date >= as.Date(start_date) & End_week_date < as.Date(end_date))
+   print(paste("Filtered data for the specified date range:", nrow(filtered_data), "rows"))
+   
+   # Define date_breaks within the function
+   date_breaks <- seq(from = as.Date("2005-01-01"), to = as.Date("2025-01-01"), by = "1 year")
+   print(paste("Date breaks for x-axis:", paste(date_breaks, collapse = ", ")))
+   
+   # Create the plot
+   p <- ggplot(filtered_data) +
+     geom_line(aes(x = End_week_date, y = FUT_1, color = "FUT_1"), size = 0.5) +
+     geom_line(aes(x = End_week_date, y = FUT_2, color = "FUT_2"), size = 0.5) +
+     geom_rect(data = harvest_periods %>% 
+                 filter(Year >= as.numeric(format(as.Date(start_date), "%Y")) & 
+                          Year < as.numeric(format(as.Date(end_date), "%Y"))),
+               aes(xmin = start, xmax = end, ymin = -Inf, ymax = Inf),
+               fill = "darkgrey", alpha = 0.3) +
+     labs(title = "Futures Prices", x = "Date", y = "Price") +
+     scale_color_manual(values = c("FUT_1" = "darkviolet", "FUT_2" = "darkturquoise")) +
+     guides(color = "none") +  # Remove legend
+     scale_y_continuous(breaks = scales::pretty_breaks(n = 10)) +
+     scale_x_date(
+       breaks = date_breaks, 
+       labels = scales::date_format("%Y"),
+       limits = c(min(filtered_data$End_week_date), max(filtered_data$End_week_date))  # Set limits based on data
+     ) +
+     theme_minimal(base_size = 10) +
+     theme(
+       axis.text.x = element_text(angle = 45, hjust = 1, size = 14),
+       axis.text.y = element_text(size = 14),
+       axis.title.x = element_text(size = 16),
+       axis.title.y = element_text(size = 16),
+       plot.title = element_text(size = 18, face = "bold")
+     )
+   
+   print("Futures plot created")
+   return(p)
+ }
> 
> # Example usage of the function
> print("Creating plots for 2005-2010")
[1] "Creating plots for 2005-2010"
> p5 <- create_basis_plot(data, "2005-01-01", "2025-01-01", harvest_periods)
[1] "Creating basis plot for range: 2005-01-01 to 2025-01-01"
[1] "Filtered data for the specified date range: 943 rows"
[1] "Date breaks for x-axis: 2005-01-01, 2006-01-01, 2007-01-01, 2008-01-01, 2009-01-01, 2010-01-01, 2011-01-01, 2012-01-01, 2013-01-01, 2014-01-01, 2015-01-01, 2016-01-01, 2017-01-01, 2018-01-01, 2019-01-01, 2020-01-01, 2021-01-01, 2022-01-01, 2023-01-01, 2024-01-01, 2025-01-01"
[1] "Basis plot created"
> print("Basis plot created: p5")
[1] "Basis plot created: p5"
> 
> p5_future <- create_futures_plot(data, "2005-01-01", "2025-01-01", harvest_periods)
[1] "Creating futures plot for range: 2005-01-01 to 2025-01-01"
[1] "Filtered data for the specified date range: 943 rows"
[1] "Date breaks for x-axis: 2005-01-01, 2006-01-01, 2007-01-01, 2008-01-01, 2009-01-01, 2010-01-01, 2011-01-01, 2012-01-01, 2013-01-01, 2014-01-01, 2015-01-01, 2016-01-01, 2017-01-01, 2018-01-01, 2019-01-01, 2020-01-01, 2021-01-01, 2022-01-01, 2023-01-01, 2024-01-01, 2025-01-01"
[1] "Futures plot created"
> print("Futures plot created: p5_future")
[1] "Futures plot created: p5_future"
> 
> print("Created all individual plots")
[1] "Created all individual plots"
> 
> # Create a combined legend
> legend <- ggplot() +
+   scale_color_manual(name = "Legend",
+                      values = c("DEC_BASIS" = "darkblue", 
+                                 "PEO_BASIS" = "darkgreen", 
+                                 "ESL_BASIS" = "darkred",
+                                 "FUT_1" = "darkviolet",
+                                 "FUT_2" = "darkturquoise"),
+                      breaks = c("FUT_1", "FUT_2", "DEC_BASIS", "PEO_BASIS", "ESL_BASIS")) +
+   geom_line(aes(x = 1, y = 1, color = "DEC_BASIS"), size = 5) +
+   geom_line(aes(x = 1, y = 2, color = "PEO_BASIS"), size = 5) +
+   geom_line(aes(x = 1, y = 3, color = "ESL_BASIS"), size = 5) +
+   geom_line(aes(x = 1, y = 4, color = "FUT_1"), size = 5) +
+   geom_line(aes(x = 1, y = 5, color = "FUT_2"), size = 5) +
+   theme_void() +
+   theme(legend.position = "bottom")
> 
> print("Legend created")
[1] "Legend created"
> 
> # Combine all plots and the new legend with an explicit layout
> grid.arrange(
+   p5_future, 
+   p5,
+   legend,
+   nrow = 3,
+   top = "Time Series (2005-2025)",
+   heights = c(1, 1, 0.2)  # Adjust height for the legend if needed
+ )
`geom_line()`: Each group consists of only one
observation.
ℹ Do you need to adjust the group aesthetic?
`geom_line()`: Each group consists of only one
observation.
ℹ Do you need to adjust the group aesthetic?
`geom_line()`: Each group consists of only one
observation.
ℹ Do you need to adjust the group aesthetic?
`geom_line()`: Each group consists of only one
observation.
ℹ Do you need to adjust the group aesthetic?
`geom_line()`: Each group consists of only one
observation.
ℹ Do you need to adjust the group aesthetic?
> print("All plots combined and displayed")
[1] "All plots combined and displayed"
>