subchunkify <- function(g, fig_height=7, fig_width=5) { g_deparsed <- paste0(deparse( function() {g} ), collapse = '') sub_chunk <- paste0(" `","``{r sub_chunk_", floor(runif(1) * 10000), ", fig.height=", fig_height, ", fig.width=", fig_width, ", echo=FALSE}", "\n(", g_deparsed , ")()", "\n`","`` ") cat(knitr::knit(text = knitr::knit_expand(text = sub_chunk), quiet = TRUE)) } create_CI_map <- function(pivot_raster, pivot_shape, pivot_spans, show_legend = F, legend_is_portrait = F, week, age, borders = FALSE){ map <- tm_shape(pivot_raster, unit = "m") + tm_raster(breaks = c(0,0.5,1,2,3,4,5,6,7,Inf), palette = "RdYlGn",legend.is.portrait = legend_is_portrait ,midpoint = NA) + tm_layout(main.title = paste0("\nMax CI week ", week,"\n", age, " weeks old"), main.title.size = 0.7, legend.show = show_legend) if (borders) { map <- map + tm_shape(pivot_shape) + tm_borders(lwd = 3) + tm_text("sub_field", size = 1/2) + tm_shape(pivot_spans) + tm_borders(lwd = 0.5, alpha = 0.5) } return(map) } create_CI_diff_map <- function(pivot_raster, pivot_shape, pivot_spans, show_legend = F, legend_is_portrait = F, week_1, week_2, age, borders = TRUE){ map <- tm_shape(pivot_raster, unit = "m") + tm_raster(breaks = c(-3,-2,-1,0,1,2,3), palette = "RdYlGn",legend.is.portrait = legend_is_portrait, midpoint = 0, title = "CI difference") + tm_layout(main.title = paste0("CI change week ", week_1, " - week ", week_2, "\n", age, " weeks old"), main.title.size = 0.7, legend.show = show_legend) if (borders) { map <- map + tm_shape(pivot_shape) + tm_borders(lwd = 3) + tm_text("sub_field", size = 1/2) + tm_shape(pivot_spans) + tm_borders(lwd = 0.5, alpha = 0.5) } return(map) } ci_plot <- function(pivotName){ # pivotName = "1.1" pivotShape <- AllPivots0 %>% terra::subset(field %in% pivotName) %>% st_transform(crs(CI)) age <- harvesting_data %>% dplyr::filter(field %in% pivotName) %>% sort("year") %>% tail(., 1) %>% dplyr::select(age) %>% unique() %>% pull() %>% round() AllPivots2 <- AllPivots0 %>% dplyr::filter(field %in% pivotName) singlePivot <- CI %>% crop(., pivotShape) %>% mask(., pivotShape) singlePivot_m1 <- CI_m1 %>% crop(., pivotShape) %>% mask(., pivotShape) singlePivot_m2 <- CI_m2 %>% crop(., pivotShape) %>% mask(., pivotShape) # singlePivot_m3 <- CI_m3 %>% crop(., pivotShape) %>% mask(., pivotShape) abs_CI_last_week <- last_week_dif_raster_abs %>% crop(., pivotShape) %>% mask(., pivotShape) abs_CI_three_week <- three_week_dif_raster_abs %>% crop(., pivotShape) %>% mask(., pivotShape) planting_date <- harvesting_data %>% dplyr::filter(field %in% pivotName) %>% ungroup() %>% dplyr::select(season_start) %>% unique() joined_spans2 <- AllPivots0 %>% st_transform(crs(pivotShape)) %>% dplyr::filter(field %in% pivotName) #%>% unique() %>% st_crop(., pivotShape) CImap_m2 <- create_CI_map(singlePivot_m2, AllPivots2, joined_spans2, show_legend= T, legend_is_portrait = T, week = week_minus_2, age = age -2, borders = borders) CImap_m1 <- create_CI_map(singlePivot_m1, AllPivots2, joined_spans2, show_legend= F, legend_is_portrait = F, week = week_minus_1, age = age -1, borders = borders) CImap <- create_CI_map(singlePivot, AllPivots2, joined_spans2, show_legend= F, legend_is_portrait = F, week = week, age = age, borders = borders) CI_max_abs_last_week <- create_CI_diff_map(abs_CI_last_week,AllPivots2, joined_spans2, show_legend = T, legend_is_portrait = T, week_1 = week, week_2 = week_minus_1, age = age, borders = borders) CI_max_abs_three_week <- create_CI_diff_map(abs_CI_three_week, AllPivots2, joined_spans2, show_legend = T, legend_is_portrait = T, week_1 = week, week_2 = week_minus_3, age = age, borders = borders) tst <- tmap_arrange(CImap_m2, CImap_m1, CImap,CI_max_abs_last_week, CI_max_abs_three_week, nrow = 1) cat(paste("## Field", pivotName, "-", age, "weeks after planting/harvest", "\n")) # cat("\n") # cat('

Pivot', pivotName, '- week', week, '-', age$Age, 'weeks after planting/harvest

') # cat(paste("# Pivot",pivots$pivot[i],"\n")) print(tst) } cum_ci_plot <- function(pivotName){ # pivotName = "3a13" data_ci <- CI_quadrant %>% filter(field == pivotName) if (nrow(data_ci) == 0) { return(cum_ci_plot2(pivotName)) # Return an empty data frame if no data is found } data_ci2 <- data_ci %>% mutate(CI_rate = cumulative_CI/DOY, week = week(Date))%>% group_by(field) %>% mutate(mean_rolling10 = rollapplyr(CI_rate , width = 10, FUN = mean, partial = TRUE)) date_preperation_perfect_pivot <- data_ci2 %>% group_by(season) %>% summarise(min_date = min(Date), max_date = max(Date), days = max_date - min_date) unique_seasons <- unique(date_preperation_perfect_pivot$season) g <- ggplot(data= data_ci2 %>% filter(season %in% unique_seasons)) + facet_wrap(~season, scales = "free_x") + geom_line( aes(Date, mean_rolling10, col = sub_field, group = sub_field)) + labs(title = paste("14 day rolling MEAN CI rate - Pivot ", pivotName), color = "Field name")+ scale_x_date(date_breaks = "1 month", date_labels = "%m-%Y") + theme_minimal() + theme(axis.text.x = element_text(angle = 60, hjust = 1), legend.justification=c(1,0), legend.position = c(1, 0), legend.title = element_text(size = 8), legend.text = element_text(size = 8)) + guides(color = guide_legend(nrow = 2, byrow = TRUE)) subchunkify(g, 3.2, 10) } cum_ci_plot <- function(pivotName, plot_type = "value", facet_on = TRUE) { # pivotName = "3a13" data_ci <- CI_quadrant %>% filter(field == pivotName) if (nrow(data_ci) == 0) { return(cum_ci_plot2(pivotName)) # Return an empty data frame if no data is found } data_ci2 <- data_ci %>% mutate(CI_rate = cumulative_CI / DOY, week = week(Date)) %>% group_by(field) %>% mutate(mean_CIrate_rolling_10_days = rollapplyr(CI_rate, width = 10, FUN = mean, partial = TRUE), mean_rolling_10_days = rollapplyr(value, width = 10, FUN = mean, partial = TRUE)) data_ci2 <- data_ci2 %>% mutate(season = as.factor(season)) date_preperation_perfect_pivot <- data_ci2 %>% group_by(season) %>% summarise(min_date = min(Date), max_date = max(Date), days = max_date - min_date) unique_seasons <- sort(unique(date_preperation_perfect_pivot$season), decreasing = TRUE)[1:3] # Determine the y aesthetic based on the plot type y_aesthetic <- switch(plot_type, "CI_rate" = "mean_CIrate_rolling_10_days", "cumulative_CI" = "cumulative_CI", "value" = "mean_rolling_10_days") y_label <- switch(plot_type, "CI_rate" = "10-Day Rolling Mean CI Rate (cumulative CI / age)", "cumulative_CI" = "Cumulative CI", "value" = "10-Day Rolling Mean CI") if (facet_on) { g <- ggplot(data = data_ci2 %>% filter(season %in% unique_seasons)) + facet_wrap(~season, scales = "free_x") + geom_line(aes_string(x = "Date", y = y_aesthetic, col = "sub_field", group = "sub_field")) + labs(title = paste("Plot of", y_label, "for Pivot", pivotName), color = "Field Name", y = y_label) + scale_x_date(date_breaks = "1 month", date_labels = "%m-%Y") + theme_minimal() + theme(axis.text.x = element_text(angle = 60, hjust = 1), legend.justification = c(1, 0), legend.position = c(1, 0), legend.title = element_text(size = 8), legend.text = element_text(size = 8)) + guides(color = guide_legend(nrow = 2, byrow = TRUE)) } else { g <- ggplot(data = data_ci2 %>% filter(season %in% unique_seasons)) + geom_line(aes_string(x = "DOY", y = y_aesthetic, col = "season", group = "season")) + labs(title = paste("Plot of", y_label, "for Pivot", pivotName), color = "Season", y = y_label, x = "Age of Crop (Days)") + theme_minimal() + theme(axis.text.x = element_text(angle = 60, hjust = 1), legend.justification = c(1, 0), legend.position = c(1, 0), legend.title = element_text(size = 8), legend.text = element_text(size = 8)) + guides(color = guide_legend(nrow = 2, byrow = TRUE)) } subchunkify(g, 3.2, 10) } cum_ci_plot2 <- function(pivotName){ end_date <- Sys.Date() start_date <- end_date %m-% months(11) # 11 months ago from end_date date_seq <- seq.Date(from = start_date, to = end_date, by = "month") midpoint_date <- start_date + (end_date - start_date) / 2 g <- ggplot() + scale_x_date(limits = c(start_date, end_date), date_breaks = "1 month", date_labels = "%m-%Y") + scale_y_continuous(limits = c(0, 4)) + labs(title = paste("14 day rolling MEAN CI rate - Field ", pivotName), x = "Date", y = "CI Rate") + theme(axis.text.x = element_text(angle = 60, hjust = 1), legend.justification = c(1, 0), legend.position = c(1, 0), legend.title = element_text(size = 8), legend.text = element_text(size = 8)) + annotate("text", x = midpoint_date, y = 2, label = "No data available", size = 6, hjust = 0.5) subchunkify(g, 3.2, 10) }