Skip to content

Commit

Permalink
added high and low attr, and focus_year par change
Browse files Browse the repository at this point in the history
  • Loading branch information
jenast committed Jul 5, 2024
1 parent 2efe817 commit 978e429
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
29 changes: 20 additions & 9 deletions R/plot_climate_comparison.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#'
#' @param climate_data A tibble of climate data (typically from get_climate_data())
#' @param variable Which variable to choose. "temperature"(default), "precipitation", or "snow_depth"
#' @param focus_year Focus on "latest" year (default) or optional year as numerical value (e.g. 2021)
#' @param focal_year Focus on "latest" year (default) or optional year as numerical value (e.g. 2021)
#' @param clip_to_1990 Logical, should historical records be clipped to >1990.
#' @param y_high_limit The y-axis higher limit of the plot.
#' @param y_low_limit The y-axix lower limit of the plot
Expand Down Expand Up @@ -32,6 +32,7 @@ plot_climate_comparison <- function(climate_data = NULL,
variable = c("temperature",
"precipitation",
"snow_depth"),
focal_year = "latest",
focus_year = "latest",
clip_to_1990 = TRUE,
y_high_limit = 60,
Expand All @@ -55,7 +56,13 @@ plot_climate_comparison <- function(climate_data = NULL,
#Preliminaries
##define some functions to avoid no non-missing errors

if(focus_year <= 1990 & clip_to_1990){ stop("Can't clip to 1990 if focus_year < 1990")}
if (!missing("focus_year")){
warning("focus_year deprecated, use focal_year instead.
The parameter focal_year is set equal the parameter focus_year")
focal_year <- focus_year
}

if(focal_year <= 1990 & clip_to_1990){ stop("Can't clip to 1990 if focal_year < 1990")}


my_min <- function(x, ...) {if (length(x)>0) min(x, ...) else Inf}
Expand Down Expand Up @@ -180,14 +187,14 @@ plot_climate_comparison <- function(climate_data = NULL,
dplyr::summarise(max(year)) %>%
dplyr::pull()

if(focus_year == "latest"){
focus_year <- latest_year
if(focal_year == "latest"){
focal_year <- latest_year
}


#Set up average and present data
past <- climate_data %>%
dplyr::filter(year != focus_year) %>%
dplyr::filter(year != focal_year) %>%
dplyr::group_by(new_day) %>%
dplyr:: mutate(upper = my_max(!!variable, na.rm = TRUE), # identify max value for each day
lower = min(!!variable, na.rm = TRUE), # identify min value for each day
Expand All @@ -203,7 +210,7 @@ plot_climate_comparison <- function(climate_data = NULL,
dplyr::pull()

present <- climate_data %>%
dplyr::filter(year == focus_year)
dplyr::filter(year == focal_year)

past_lows <- past %>%
dplyr::group_by(new_day) %>%
Expand Down Expand Up @@ -366,6 +373,7 @@ plot_climate_comparison <- function(climate_data = NULL,

}

}
low_annot_coord <- present_lows %>%
#filter(!!variable == suppressWarnings(min(!!variable, na.rm = TRUE))) %>%
dplyr::slice(1) %>%
Expand All @@ -376,7 +384,7 @@ plot_climate_comparison <- function(climate_data = NULL,

no_low_days <- nrow(present_lows)


if(annotate_plot){
#Displace low annot cord if the first coldest day is to late to fit the text
if(no_low_days > 0){

Expand Down Expand Up @@ -431,7 +439,7 @@ plot_climate_comparison <- function(climate_data = NULL,

if(main_title){
p <- p +
ggtitle(paste(placename, text_table[[language]][1], focus_year, sep = "")) +
ggtitle(paste(placename, text_table[[language]][1], focal_year, sep = "")) +
theme(plot.title = element_text(face = "bold", hjust = .012, vjust = .8, colour = "#3C3C3C", size = 20)) +
annotate("text",
x = 14,
Expand Down Expand Up @@ -537,7 +545,7 @@ plot_climate_comparison <- function(climate_data = NULL,
annotate("text",
x = legend_pos$x - 10,
y = legend_pos$y + 9.75,
label = paste0(focus_year, stringr::str_to_sentence(legend_table[[language]][[legend_variable]])),
label = paste0(focal_year, stringr::str_to_sentence(legend_table[[language]][[legend_variable]])),
size = 2,
colour = "gray30",
hjust = 1,
Expand All @@ -559,6 +567,9 @@ plot_climate_comparison <- function(climate_data = NULL,
hjust = 0,
vjust = 0)

attr(p, "no_low_days") <- no_low_days
attr(p, "no_high_days") <- no_high_days

suppressWarnings(return(p))

invisible(Sys.setlocale(category = "LC_TIME", old_LC_TIME))
Expand Down
3 changes: 2 additions & 1 deletion man/plot_climate_comparison.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 978e429

Please sign in to comment.