From edd4ba6babd86114016174120b6e237a3aa88cf6 Mon Sep 17 00:00:00 2001 From: Mengqi Zhao Date: Thu, 4 Aug 2022 10:54:34 -0700 Subject: [PATCH] Update segment diagnostic figures Update to gradient color by year --- R/diagnostics.R | 66 ++++++++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/R/diagnostics.R b/R/diagnostics.R index 6cdac3d..8bafc3e 100644 --- a/R/diagnostics.R +++ b/R/diagnostics.R @@ -99,65 +99,69 @@ diagnostics <- function(hdcd = tibble::tibble(), print(paste0("Diagnostic figure saved as ", filename_diagnostics_i)) } - # Combined years free scale - if(T) { + # combined years with color gradients with free scale + if(T){ data_plot <- hdcd_comb_diagnostics %>% dplyr::mutate(segment = factor(segment, levels = segment_levels)) + n_color <- length(unique(hdcd_comb_diagnostics$year)) + pal_hd <- colorRampPalette(RColorBrewer::brewer.pal(9, 'YlOrRd')) + pal_cd <- colorRampPalette(RColorBrewer::brewer.pal(9, 'YlGnBu')) + pal <- c(rev(pal_hd(n_color)), rev(pal_cd(n_color))) + + filename_diagnostics_i <- + paste0(folder_diagnostics, "/", basename(gsub(".csv", "", filename)), "_allYears_gradient_freeScale_", name_append,".png") + ggplot2::ggplot(data = data_plot) + - ggplot2::aes(x = segment, y = value, - group = interaction(year, heatcool), - color = interaction(heatcool)) + - ggplot2::geom_line() + + ggplot2::geom_line(ggplot2::aes(x = segment, y = value, + group = interaction(year, heatcool), + color = interaction(year, heatcool))) + ggplot2::facet_wrap(subRegion ~ ., scales = "free_y") + ggplot2::ggtitle(paste0("HDCD WRF to GCAM ")) + - ggplot2::scale_color_manual(values = c("heat" = "firebrick", - "cool" = "dodgerblue")) + + ggplot2::ylab('Degree-Hours') + + ggplot2::scale_color_manual(values = pal, + guide = ggplot2::guide_legend(title = 'HDCD (All Years)')) + ggplot2::scale_x_discrete(drop = FALSE) + - ggplot2::guides(color = ggplot2::guide_legend(title = 'heatcool')) + ggplot2::theme_bw() + ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 90, - vjust = 0.5), - legend.position = 'right') - - filename_diagnostics_i <- - paste0(folder_diagnostics, "/", basename(gsub(".csv", "", filename)), "_allYears_freeScale", name_append, '.png') + vjust = 0.5)) - # save plot ggplot2::ggsave(filename = filename_diagnostics_i, width = 25, height = 15) - print(paste0("Diagnostic figure saved as ", filename_diagnostics_i)) } - # Combined years fixed scale - if(T) { + # combined years with color gradients with fixed scale + if(T){ data_plot <- hdcd_comb_diagnostics %>% dplyr::mutate(segment = factor(segment, levels = segment_levels)) + n_color <- length(unique(hdcd_comb_diagnostics$year)) + pal_hd <- colorRampPalette(RColorBrewer::brewer.pal(9, 'YlOrRd')) + pal_cd <- colorRampPalette(RColorBrewer::brewer.pal(9, 'YlGnBu')) + pal <- c(rev(pal_hd(n_color)), rev(pal_cd(n_color))) + + filename_diagnostics_i <- + paste0(folder_diagnostics, "/", basename(gsub(".csv", "", filename)), "_allYears_gradient_fixedScale_", name_append,".png") + ggplot2::ggplot(data = data_plot) + - ggplot2::aes(x = segment, y = value, - group = interaction(year, heatcool), - color = interaction(heatcool)) + - ggplot2::geom_line() + + ggplot2::geom_line(ggplot2::aes(x = segment, y = value, + group = interaction(year, heatcool), + color = interaction(year, heatcool))) + ggplot2::facet_wrap(subRegion ~ ., scales = "fixed") + ggplot2::ggtitle(paste0("HDCD WRF to GCAM ")) + - ggplot2::scale_color_manual(values = c("heat" = "firebrick", "cool" = - "dodgerblue")) + + ggplot2::ylab('Degree-Hours') + + ggplot2::scale_color_manual(values = pal, + guide = ggplot2::guide_legend(title = 'HDCD (All Years)')) + ggplot2::scale_x_discrete(drop = FALSE) + - ggplot2::guides(color = ggplot2::guide_legend(title = 'heatcool')) + ggplot2::theme_bw() + ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 90, vjust = 0.5)) - filename_diagnostics_i <- - paste0(folder_diagnostics, "/", basename(gsub(".csv", "", filename)), "_allYears_fixedScale",name_append,".png") - - ggplot2::ggsave(filename = filename_diagnostics_i, + ggplot2::ggsave(filename = filename_diagnostics_i, width = 25, - height = 15) # save plot - + height = 15) print(paste0("Diagnostic figure saved as ", filename_diagnostics_i)) }