From 76534edfcc8a03b0f6d98a9162c17d70e6dd4ead Mon Sep 17 00:00:00 2001 From: "PIKACCOUNTS\\tonnru" Date: Wed, 20 Dec 2023 17:41:53 +0100 Subject: [PATCH 01/24] Initial commit --- R/plotPercentiles.R | 3 + comp_plot_for_ngfs_magicc_data.Rmd | 381 +++++++++++++++++++++++++++++ 2 files changed, 384 insertions(+) create mode 100644 R/plotPercentiles.R create mode 100644 comp_plot_for_ngfs_magicc_data.Rmd diff --git a/R/plotPercentiles.R b/R/plotPercentiles.R new file mode 100644 index 0000000..af983ab --- /dev/null +++ b/R/plotPercentiles.R @@ -0,0 +1,3 @@ +plotWhatever <- function(df) { + print("Hello World") +} \ No newline at end of file diff --git a/comp_plot_for_ngfs_magicc_data.Rmd b/comp_plot_for_ngfs_magicc_data.Rmd new file mode 100644 index 0000000..d51ac6d --- /dev/null +++ b/comp_plot_for_ngfs_magicc_data.Rmd @@ -0,0 +1,381 @@ +--- +title: "Improve cs2 climate plots of temperature & concentration distributions from MAGICC" +output: + html_document: + theme: paper + toc: true + toc_float: + collapsed: false + toc_depth: 2 + code_folding: hide + df_print: kable + html_notebook: + code_folding: hide + pdf_document: default +--- + + +```{r message=FALSE, warning=FALSE} +library(ggplot2) +library(dplyr) +library(tidyr) +library(quitte) +library(readxl) +``` + +# Get meta data and have a look + +Currently uses XLSX, use `quitte` instead. Goal is to have a quitte Object + +```{r, fig.width=5, fig.height=5, message = FALSE, warning = FALSE} +these_please <- c( + "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|5.0th Percentile", + "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|33.0th Percentile", + "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|50.0th Percentile", + "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|67.0th Percentile", + "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|95.0th Percentile" +) + +ngfs_data <- readxl::read_xlsx( + "~/../lab/misc_data/ngfs-magicc-data-from-csv-v2.xlsx", + sheet = "ngfs-magicc-data" +) %>% + #select(starts_with(c("Variable", "Unit", "1", "2"))) %>% + filter(grepl("Percentile", Variable)) %>% # Works + mutate_at(vars(starts_with(c("1", "2"))), as.double) + +ngfs_data # Should be quitte +foo <- reshape2::melt(ngfs_data, id.vars = seq(1:5), variable.name = "period") +print(foo) +bar <- as.quitte(foo %>% filter(Variable %in% these_please)) # Filter for appropriate variables +#plotWhatever(bar) # Should return facet_wrap plot +#mip::plotPercentile(bar) ## +``` +Need to collect all the values for each year + +```{r} +these_please <- c( + "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|5.0th Percentile", + "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|33.0th Percentile", + "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|50.0th Percentile", + "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|67.0th Percentile", + "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|95.0th Percentile" +) + +percentile_from_variables <- function(variables) { + return (stringr::str_split_i(variables, pattern = "\\|", i=-1)) +} + +df <- ngfs_data %>% + #filter(Variable %in% these_please) %>% + pivot_longer(starts_with(c("1", "2")), names_to = "Year", values_to = "Value") %>% + pivot_wider(names_from = "Variable", values_from = "Value") %>% + rename_with(percentile_from_variables, starts_with("AR6 climate diagnostics")) %>% + mutate_at(vars(c("Year")), as.integer) +df +``` + +```{r} +baz <- bar %>% + mutate( + "Percentile"=stringr::str_split_i(variable, pattern = "\\|", i=-1), + "variable"=gsub("\\|[^\\|]+$", "", variable) + ) +print(baz) + +ggplot() + + geom_line( + #data = filter(baz, variable == "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3"), + #data = baz, + data = baz, + aes(x = period, y = value, color=Percentile) + ) + +facet_wrap(~variable, scales = "free") +``` + + +```{r} + +p <- ggplot() + + geom_line( + data = df, + aes(x = Year, y = get("50.0th Percentile"), group=1) + ) + + geom_ribbon( + data = df, + aes(x = Year, ymin = get("33.0th Percentile"), ymax = get("67.0th Percentile")), + fill = "#68788a", + alpha = 0.5 + ) + + geom_ribbon( + data = df, + aes(x = Year, ymin = get("5.0th Percentile"), ymax = get("95.0th Percentile")), + fill = "#68788a", + alpha = 0.2 + ) + # geom_ribbon( + # data = df, + # aes(x = period, ymin = lower, ymax = upper), + # fill = "#68788a", + # alpha = 0.2 + # ) + +print(p) +``` +# Multiple scenario dataset + +Variables are doublicates in this dataset + +```{r} +ngfs_scenarios <- readxl::read_xlsx( + "~/../lab/misc_data/ngfs-magicc-data-from-csv-v3-extended-scenarios.xlsx", + sheet = "ngfs-magicc-data" +) %>% + select(starts_with(c("Scenario", "Variable", "Unit", "1", "2"))) %>% + filter(grepl("Percentile", Variable)) %>% # Works + mutate_at(vars(starts_with(c("1", "2"))), as.double) +``` + +Restructure data like before + +```{r} + +these_please <- c( + "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|5.0th Percentile", + "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|33.0th Percentile", + "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|50.0th Percentile", + "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|67.0th Percentile", + "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|95.0th Percentile" +) + +percentile_from_variables <- function(variables) { + return (stringr::str_split_i(variables, pattern = "\\|", i=-1)) +} + +df <- ngfs_scenarios %>% + filter(Variable %in% these_please) %>% + pivot_longer(starts_with(c("1", "2")), names_to = "Year", values_to = "Value") %>% + pivot_wider(names_from = "Variable", values_from = "Value") %>% + rename_with(percentile_from_variables, starts_with("AR6 climate diagnostics")) %>% + mutate_at(vars(c("Year")), as.integer) +df + +ngfs_scenarios +``` + +## Combine mutiple plots into `facet_wrap` + +```{r} +p <- ggplot() + + geom_line( + data = df, + aes(x = Year, y = get("50.0th Percentile"), group=1) + ) + + geom_ribbon( + data = df, + aes(x = Year, ymin = get("33.0th Percentile"), ymax = get("67.0th Percentile")), + fill = "#68788a", + alpha = 0.5 + ) + + geom_ribbon( + data = df, + aes(x = Year, ymin = get("5.0th Percentile"), ymax = get("95.0th Percentile")), + fill = "#68788a", + alpha = 0.2 + ) + # geom_ribbon( + # data = df, + # aes(x = period, ymin = lower, ymax = upper), + # fill = "#68788a", + # alpha = 0.2 + # ) + +#complete <- p + facet_wrap(~class, ncol = 3) +#complete <- p + facet_wrap(c("a", "b", "c"), ncol = 3) +complete <- p + facet_wrap() + +print(complete) +# print(p) +``` + +```{r} +knitr::knit_exit() +``` + +# Playground + +## Learning `facet_wrap` + +```{r} +library(ggplot2) +mpg2 <- subset(mpg, cyl != 5 & drv %in% c("4", "f") & class != "2seater") +print(mpg2) + +base <- ggplot(mpg2, aes(displ, hwy)) + + #geom_blank() + # Plots nothing + geom_point() + # Plots points + xlab(NULL) + + ylab(NULL) + +# base + facet_wrap(~class, ncol = 3) +base + facet_wrap("class", ncol = 3) +#base + facet_wrap(var(class), ncol = 3) # Doesnt work +``` + +## Restructuring + +```{r} +pnl <- tibble( + x = 1:4, + a = c(1, 1,0, 0), + b = c(0, 1, 1, 1), + y1 = rnorm(4), + y2 = rnorm(4), + z1 = rep(3, 4), + z2 = rep(-2, 4), +) +print(pnl) + +foo <- pnl %>% pivot_longer( + cols = !c(x, a, b), + names_to = c(".value", "time"), + names_pattern = "(.)(.)" +) +print(foo) +``` + +## Miscellaneous + +```{r} +#endsWith(x = ngfs_meta[, ncol(ngfs_meta)], suffix = "Percentile") +#ngfs_meta[, ncol(ngfs_meta)] %>% select(ends_with("Percentile")) # Does not work +#ngfs_meta[, ncol(ngfs_meta)] %>% filter(grepl("Percentile", ngfs_meta[, ncol(ngfs_meta)])) # Does not work +#ngfs_meta[, ncol(ngfs_meta)] %>% filter(grepl("Percentile", colnames(ngfs_meta)[ncol(ngfs_meta)])) # Does not work +#ngfs_meta[, ncol(ngfs_meta)] %>% filter(grepl("Percentile", "Variable_7")) # Does not work +#grepl("Percentile", colnames(ngfs_meta)[ncol(ngfs_meta)]) # Same as above +#ngfs_meta[, ncol(ngfs_meta)] %>% filter(grepl("Percentile", Variable_7)) # Works +#ngfs_meta %>% filter(grepl("Percentile", Variable_7)) # Works +#ngfs_meta[, ncol(ngfs_meta)] %>% filter(grepl("Percentile", as.name(tail(names(ngfs_meta), 1)))) # Does not work +#ngfs_meta[, ncol(ngfs_meta)] %>% filter(grepl("Percentile", as.symbol(tail(names(ngfs_meta), 1)))) # Does not work +#ngfs_meta[, ncol(ngfs_meta)] %>% filter(grepl("Percentile", as.symbol(tail(names(.), 1)))) # Does not work +#ngfs_meta[, ncol(ngfs_meta)] %>% filter() +#colnames(ngfs_meta)[ncol(ngfs_meta)] +#as.name(tail(names(ngfs_meta), 1)) +#as.symbol(tail(names(ngfs_meta), 1)) +``` + +```{r, fig.width=5, fig.height=5, message = FALSE, warning = FALSE} +#ngfs_meta %>% drop_na(contains("Variable")) +#ngfs_meta %>% filter(last_col() != "95.0th Percentile") +ngfs_meta %>% filter(ngfs_meta[, ncol(ngfs_meta)] == "95.0th Percentile") +``` + +```{r, fig.width=5, fig.height=5, message = FALSE, warning = FALSE} +ngfs_data <- readxl::read_xlsx( + "~/../lab/misc_data/ngfs-magicc-data-from-csv.xlsx", + sheet = "ngfs-magicc-data" +) %>% + select(starts_with(c("Variable", "1", "2"))) %>% + separate_wider_delim("Variable", delim = "|", names_sep = "_", too_few = "align_end") #%>% + #drop_na(x, any_of(vars)) + #separate_wider_delim(cols = c("Variable"), delim="|") + #data.frame(do.call('rbind', strsplit(as.character(ngfs_data$Variable), '|', fixed=TRUE))) %>% + #drop("Variable") + +#within(ngfs_meta, SOMETHING<-data.frame(do.call('rbind', strsplit(as.character(ngfs_meta$Variable), '|', fixed=TRUE)))) + +ngfs_data +``` + +## Concatenate string vectors and constants + +```{r} +a <- 1:10 +print(a) +b <- 11:20 +print(b) +c <- c(a,b) +print(c) +d <- c(a,b, 5) +print(d) + +print("String vectors") + +a <- c("loooooooooooooooooooooooooooooooooooooooooooooooooooooooooong String", "bar") +print(a) +b <- c("baz") +print(b) +c <- c(a,b) +print(c) +d <- c(a,b, ".values") +print(d) +``` + +## Split `Variable` into dedicated columns +```{r, fig.width=5, fig.height=5, message = FALSE, warning = FALSE} +data.frame(do.call('rbind', strsplit(as.character(ngfs_data$Variable), '|', fixed=TRUE))) +``` + +## Basic stuff +```{r} +c("X1", "X2", "X3", "X4", "X5", "X6", "X7") +c("X1", "X2", "X3", "X4", "X5") +``` + +```{r, fig.width=5, fig.height=5, message = FALSE, warning = FALSE} +#ngfs_meta |> separate_wider_delim("Variable", delim = "|", names_sep = "_") +ngfs_meta |> separate_wider_delim("Variable", delim = "|", names_sep = "_", names = c("X1", "X2", "X3", "X4", "X5", "X6", "X7"), too_few = "align_start") +#as.data.frame(ngfs_meta) |> separate_longer_delim("Variable", delim = "|") +#as.data.frame(ngfs_meta) |> separate_wider_delim("Variable", delim = "|", names = c("X1", "X2", "X3", "X4", "X5", "X6", "X7")) +#data.frame(do.call('rbind', strsplit(as.character(ngfs_data$Variable), '|', fixed=TRUE))) +``` + +# Graveyard + +```{r} +these_please <- c( + "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|5.0th Percentile", + "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|33.0th Percentile", + "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|50.0th Percentile", + "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|67.0th Percentile", + "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|95.0th Percentile" +) + +percentile_from_variables <- function(variables) { + return (stringr::str_split_i(variables, pattern = "\\|", i=-1)) +} + +df <- ngfs_data %>% + filter(Variable %in% these_please) %>% + # pivot_longer(starts_with(c("1", "2"))) + # pivot_longer(starts_with(c("1", "2")), names_to = these_please, names_sep = " ", values_to = "year") #%>% + # pivot_longer_spec(starts_with(c("1", "2")), names_to = these_please, names_sep = " ", values_to = "year") #%>% + # pivot_longer_spec(starts_with(c("1", "2")), names_to = c(".value", these_please), names_sep = " ", values_to = "year") #%>% + #pivot_longer(starts_with(c("1", "2")), names_to = c(".value", these_please), names_pattern = "(.)(.)(.)") + pivot_longer(starts_with(c("1", "2")), names_to = "Year", values_to = "Value") %>% + #gather("year", "value", 3:ncol(ngfs_data), convert = TRUE) #%>% + #gather("year", c("value_1", "value_2"), 3:ncol(ngfs_data)) #%>% + #gather("year", "value", 3:ncol(ngfs_data), convert = as.integer) %>% + #mutate_at(vars(c("year")), as.integer) + pivot_wider(names_from = "Variable", values_from = "Value") %>% + rename_with(percentile_from_variables, starts_with("AR6 climate diagnostics")) %>% + mutate_at(vars(c("Year")), as.integer) +df +``` + +```{r, fig.width=5, fig.height=5, message = FALSE, warning = FALSE} +ngfs_data <- readxl::read_xlsx( + "~/../lab/misc_data/ngfs-magicc-data-from-csv-v2.xlsx", + sheet = "ngfs-magicc-data" +) %>% + select(starts_with(c("Variable", "Unit", "1", "2"))) %>% + #filter(grepl("Percentile", Variable)) %>% # Works + filter(grepl("Percentile", Variable)) %>% # Works + #separate_wider_delim("Variable", delim = "|", names_sep = "_", too_few = "align_end") #%>% + #gather("year", "value", 3:ncol(ngfs_meta)) + mutate_at(vars(starts_with(c("1", "2"))), as.double) + +#within(ngfs_meta, SOMETHING<-data.frame(do.call('rbind', strsplit(as.character(ngfs_meta$Variable), '|', fixed=TRUE)))) + +ngfs_data +``` From 9d1ee96de2542dd39e4a3454ef4bf643694f0788 Mon Sep 17 00:00:00 2001 From: "PIKACCOUNTS\\tonnru" Date: Thu, 21 Dec 2023 14:57:50 +0100 Subject: [PATCH 02/24] Cleanup + facet_wrap for multiple variables completed --- comp_plot_for_ngfs_magicc_data.Rmd | 249 +++++++++++++++-------------- 1 file changed, 131 insertions(+), 118 deletions(-) diff --git a/comp_plot_for_ngfs_magicc_data.Rmd b/comp_plot_for_ngfs_magicc_data.Rmd index d51ac6d..8401eb2 100644 --- a/comp_plot_for_ngfs_magicc_data.Rmd +++ b/comp_plot_for_ngfs_magicc_data.Rmd @@ -23,180 +23,193 @@ library(quitte) library(readxl) ``` -# Get meta data and have a look +# Data! -Currently uses XLSX, use `quitte` instead. Goal is to have a quitte Object +Currently uses XLSX, unsure if this stays use `quitte` instead. Goal is to have a quitte Object. We need to do some more work before reshaping the dset and creating a quitte object from. This seems to depend on the XLSX itself and therefore other methods for reading it might produce different results. However, the most important part is to select for the variable to be displayed later on -```{r, fig.width=5, fig.height=5, message = FALSE, warning = FALSE} +```{r} these_please <- c( + # CO2 concentrations "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|5.0th Percentile", "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|33.0th Percentile", "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|50.0th Percentile", "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|67.0th Percentile", - "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|95.0th Percentile" + "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|95.0th Percentile", + # NOTE: Theres not 5th Percentile on Surface Temperatures + "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|10.0th Percentile", + "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|33.0th Percentile", + "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|50.0th Percentile", + "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|67.0th Percentile", + "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|90.0th Percentile", + # CH4 Fluxes + "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|5.0th Percentile", + "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|33.0th Percentile", + "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|50.0th Percentile", + "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|67.0th Percentile", + "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|95.0th Percentile" ) -ngfs_data <- readxl::read_xlsx( +ngfs_xlsx <- readxl::read_xlsx( "~/../lab/misc_data/ngfs-magicc-data-from-csv-v2.xlsx", sheet = "ngfs-magicc-data" ) %>% - #select(starts_with(c("Variable", "Unit", "1", "2"))) %>% - filter(grepl("Percentile", Variable)) %>% # Works - mutate_at(vars(starts_with(c("1", "2"))), as.double) + # Cast yearly columns as doubles + mutate_at(vars(starts_with(c("1", "2"))), as.double) %>% + # Exclude rows whose "Variable" column does not end in Percentile, because + # these do not contain numerical data in the columns for each year + #filter(grepl("Percentile", Variable)) %>% + # Take only those variables that shall be plotted later on + filter(Variable %in% these_please) + +ngfs_xlsx +``` -ngfs_data # Should be quitte -foo <- reshape2::melt(ngfs_data, id.vars = seq(1:5), variable.name = "period") -print(foo) -bar <- as.quitte(foo %>% filter(Variable %in% these_please)) # Filter for appropriate variables -#plotWhatever(bar) # Should return facet_wrap plot -#mip::plotPercentile(bar) ## +Now for the fun part: Reshaping the whole thang into a `quitte` object. Year columns need to become lines here. Can be done with `reshape2::melt`: + +```{r} +reshape2::melt( + ngfs_xlsx, + #id.vars = seq(1:5), + c("Model", "Scenario", "Region", "Variable", "Unit"), + variable.name = "period" +) ``` -Need to collect all the values for each year + +Or with `dplyr::pivot_longer`: ```{r} -these_please <- c( - "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|5.0th Percentile", - "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|33.0th Percentile", - "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|50.0th Percentile", - "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|67.0th Percentile", - "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|95.0th Percentile" +pivot_longer(ngfs_xlsx, starts_with(c("1", "2")), names_to = "period", values_to = "value") +``` + +Finally, create a `quitte` object (which is basically a list with specified columns) + +```{r} +ngfs_data <- as.quitte(ngfs_xlsx %>% + pivot_longer( + starts_with(c("1", "2")), + names_to = "period", + values_to = "value" + ) ) -percentile_from_variables <- function(variables) { - return (stringr::str_split_i(variables, pattern = "\\|", i=-1)) -} +ngfs_data <- as.quitte(ngfs_xlsx %>% + reshape2::melt( + c("Model", "Scenario", "Region", "Variable", "Unit"), + variable.name = "period" + ) +) -df <- ngfs_data %>% - #filter(Variable %in% these_please) %>% - pivot_longer(starts_with(c("1", "2")), names_to = "Year", values_to = "Value") %>% - pivot_wider(names_from = "Variable", values_from = "Value") %>% - rename_with(percentile_from_variables, starts_with("AR6 climate diagnostics")) %>% - mutate_at(vars(c("Year")), as.integer) -df +print(ngfs_data) ``` +By the way: + +```{r} +print(typeof(ngfs_data)) +``` + +Our `quitte` object is now available an ready to be passed to the plotting function. + +# Plotting! + +Write a function that returns a `facet_wrap` + ```{r} -baz <- bar %>% +plotPercentiles <- function(quitte_frame) { + print("Hello!") +} +#mip::plotPercentile(bar) ## +ngfs_data +``` +Need to collect all the values for each year + +```{r} +df <- ngfs_data %>% mutate( - "Percentile"=stringr::str_split_i(variable, pattern = "\\|", i=-1), - "variable"=gsub("\\|[^\\|]+$", "", variable) - ) -print(baz) + "percentile" = stringr::str_extract( variable, "[^\\|]+?$"), + "variable" = gsub("\\|[^\\|]+$", "", variable) + ) %>% + pivot_wider( + names_from = "percentile", + values_from = "value" + ) + +# colnames(df) +df +``` +```{r} ggplot() + geom_line( #data = filter(baz, variable == "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3"), #data = baz, - data = baz, - aes(x = period, y = value, color=Percentile) - ) + -facet_wrap(~variable, scales = "free") + #data = df, + data = filter(df, variable == "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3"), + aes(x = period, y = get("33.0th Percentile"), color=Percentile) + ) +#facet_wrap(~variable, scales = "free") ``` - ```{r} p <- ggplot() + geom_line( - data = df, - aes(x = Year, y = get("50.0th Percentile"), group=1) + # data = df, + data = filter(df, variable == "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3"), + aes(x = period, y = get("50.0th Percentile"), group=1) ) + geom_ribbon( - data = df, - aes(x = Year, ymin = get("33.0th Percentile"), ymax = get("67.0th Percentile")), + data = filter(df, variable == "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3"), + aes(x = period, ymin = get("33.0th Percentile"), ymax = get("67.0th Percentile")), fill = "#68788a", alpha = 0.5 ) + geom_ribbon( - data = df, - aes(x = Year, ymin = get("5.0th Percentile"), ymax = get("95.0th Percentile")), + data = filter(df, variable == "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3"), + aes(x = period, ymin = get("5.0th Percentile"), ymax = get("95.0th Percentile")), fill = "#68788a", alpha = 0.2 ) - # geom_ribbon( - # data = df, - # aes(x = period, ymin = lower, ymax = upper), - # fill = "#68788a", - # alpha = 0.2 - # ) print(p) ``` -# Multiple scenario dataset - -Variables are doublicates in this dataset +# Case: Multiple variables ```{r} -ngfs_scenarios <- readxl::read_xlsx( - "~/../lab/misc_data/ngfs-magicc-data-from-csv-v3-extended-scenarios.xlsx", - sheet = "ngfs-magicc-data" -) %>% - select(starts_with(c("Scenario", "Variable", "Unit", "1", "2"))) %>% - filter(grepl("Percentile", Variable)) %>% # Works - mutate_at(vars(starts_with(c("1", "2"))), as.double) +unique(df$variable) ``` - -Restructure data like before - ```{r} - -these_please <- c( - "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|5.0th Percentile", - "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|33.0th Percentile", - "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|50.0th Percentile", - "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|67.0th Percentile", - "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|95.0th Percentile" -) - -percentile_from_variables <- function(variables) { - return (stringr::str_split_i(variables, pattern = "\\|", i=-1)) +p <- ggplot() + +for (variable in unique(df$variable)) { + p <- p + + geom_line( + # data = df, + data = filter(df, variable == variable), + aes(x = period, y = get("50.0th Percentile"), group=1) + ) + + geom_ribbon( + data = filter(df, variable == variable), + aes(x = period, ymin = get("33.0th Percentile"), ymax = get("67.0th Percentile")), + fill = "#68788a", + alpha = 0.5 + ) + + geom_ribbon( + data = filter(df, variable == variable), + aes(x = period, ymin = get("5.0th Percentile"), ymax = get("95.0th Percentile")), + fill = "#68788a", + alpha = 0.2 + ) } -df <- ngfs_scenarios %>% - filter(Variable %in% these_please) %>% - pivot_longer(starts_with(c("1", "2")), names_to = "Year", values_to = "Value") %>% - pivot_wider(names_from = "Variable", values_from = "Value") %>% - rename_with(percentile_from_variables, starts_with("AR6 climate diagnostics")) %>% - mutate_at(vars(c("Year")), as.integer) -df +p <- p + facet_wrap(vars(variable), scales = "free") -ngfs_scenarios +print(p) ``` -## Combine mutiple plots into `facet_wrap` +# Multiple scenario dataset -```{r} -p <- ggplot() + - geom_line( - data = df, - aes(x = Year, y = get("50.0th Percentile"), group=1) - ) + - geom_ribbon( - data = df, - aes(x = Year, ymin = get("33.0th Percentile"), ymax = get("67.0th Percentile")), - fill = "#68788a", - alpha = 0.5 - ) + - geom_ribbon( - data = df, - aes(x = Year, ymin = get("5.0th Percentile"), ymax = get("95.0th Percentile")), - fill = "#68788a", - alpha = 0.2 - ) - # geom_ribbon( - # data = df, - # aes(x = period, ymin = lower, ymax = upper), - # fill = "#68788a", - # alpha = 0.2 - # ) - -#complete <- p + facet_wrap(~class, ncol = 3) -#complete <- p + facet_wrap(c("a", "b", "c"), ncol = 3) -complete <- p + facet_wrap() - -print(complete) -# print(p) -``` +Variables are now dublicates in this dset ```{r} knitr::knit_exit() From 634d627a5ca5110daf6bc6e321192d393168074e Mon Sep 17 00:00:00 2001 From: "PIKACCOUNTS\\tonnru" Date: Thu, 21 Dec 2023 17:22:48 +0100 Subject: [PATCH 03/24] Multiple scenarios/variables use case completed --- comp_plot_for_ngfs_magicc_data.Rmd | 140 ++++++++++++++++++++++++++++- 1 file changed, 139 insertions(+), 1 deletion(-) diff --git a/comp_plot_for_ngfs_magicc_data.Rmd b/comp_plot_for_ngfs_magicc_data.Rmd index 8401eb2..2a0b7c1 100644 --- a/comp_plot_for_ngfs_magicc_data.Rmd +++ b/comp_plot_for_ngfs_magicc_data.Rmd @@ -209,7 +209,145 @@ print(p) # Multiple scenario dataset -Variables are now dublicates in this dset +In this dset, variables are now dublicates that belong to different scenarios. Again + +0a) Read in data + +```{r} +these_please <- c( + # CO2 concentrations + "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|5.0th Percentile", + "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|33.0th Percentile", + "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|50.0th Percentile", + "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|67.0th Percentile", + "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|95.0th Percentile" #, + # NOTE: Theres not 5th Percentile on Surface Temperatures + # "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|10.0th Percentile", + # "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|33.0th Percentile", + # "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|50.0th Percentile", + # "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|67.0th Percentile", + # "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|90.0th Percentile", + # CH4 Fluxes + # "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|5.0th Percentile", + # "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|33.0th Percentile", + # "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|50.0th Percentile", + # "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|67.0th Percentile", + # "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|95.0th Percentile" +) + +ngfs_xlsx_2_scenarios <- readxl::read_xlsx( + "~/../lab/misc_data/ngfs-magicc-data-from-csv-v3-extended-scenarios.xlsx", + sheet = "ngfs-magicc-data" +) %>% + # Cast yearly columns as doubles + mutate_at(vars(starts_with(c("1", "2"))), as.double) %>% + # Exclude rows whose "Variable" column does not end in Percentile, because + # these do not contain numerical data in the columns for each year + #filter(grepl("Percentile", Variable)) %>% + # Take only those variables that shall be plotted later on + filter(Variable %in% these_please) + +ngfs_xlsx_2_scenarios +``` + +```{r} +unique(ngfs_xlsx_2_scenarios$Scenario) +``` + +0b) Transform to `quitte` object + +```{r} +ngfs_data_2_scenarios <- as.quitte(ngfs_xlsx_2_scenarios %>% + reshape2::melt( + c("Model", "Scenario", "Region", "Variable", "Unit"), + variable.name = "period" + ) +) + +# print(ngfs_data_2_scenarios) +unique(ngfs_data_2_scenarios$scenario) +``` + +1) Transform data + +```{r} +df_2 <- ngfs_data_2_scenarios %>% + # group_by(scenario) %>% + mutate( + "percentile" = stringr::str_extract( variable, "[^\\|]+?$"), + "variable" = gsub("\\|[^\\|]+$", "", variable) + ) %>% + pivot_wider( + names_from = "percentile", + values_from = "value" + ) + +# colnames(df_2) +# df_2 +unique(df_2$scenario) +``` + +```{r} +p <- ggplot() + +for (variable in unique(df_2$variable)) { + for (scenario in unique(df_2$scenario)) { + data <- filter(df_2, variable == variable & scenario == scenario) + labels + # print(unique(data$unit)) + p <- p + + geom_line( + data = data, aes(x = period, y = get("50.0th Percentile"), group=1) + ) + + geom_ribbon( + data = data, aes(x = period, ymin = get("33.0th Percentile"), ymax = get("67.0th Percentile")), + fill = "#68788a", + alpha = 0.5 + ) + + geom_ribbon( + data = data, aes(x = period, ymin = get("5.0th Percentile"), ymax = get("95.0th Percentile")), + fill = "#68788a", + alpha = 0.2 + ) + } +} + +# p <- p + facet_wrap(vars(variable), scales = "free", ncol = length(unique(df_2$scenario))) +# p <- p + facet_wrap(vars(variable), scales = "free") +# p <- p + facet_wrap(vars(scenario, variable), scales = "free") +# p <- p + facet_wrap(vars(scenario, variable), scales = "free", +# strip.position = "left", +# # strip.position = NULL, +# labeller = as_labeller(c(A = "Currents (A)", V = "Voltage (V)"))) + +# ylab(NULL) #+ +# theme(strip.background = element_blank(), +# strip.placement = "outside") +# p <- p + facet_wrap(vars(scenario, variable), scales = "free") + +# Using facet grid +p <- p + facet_grid(variable ~ scenario, scales = "free_y") + + theme( + axis.title.x = element_blank() + ) + ylab(unique(df_2$unit)) +# + +# p <- p + +# facet_wrap( +# vars(scenario, variable), +# scales = "free_y") + +# theme( +# axis.title.x = element_blank(), +# axis.title.y = element_blank() +# ) + +print(p) +``` +```{r} +unique(data$unit) +``` + + +# The End ```{r} knitr::knit_exit() From 57c067b4974b7bde9c25bf9957e18ece83a33f31 Mon Sep 17 00:00:00 2001 From: "PIKACCOUNTS\\tonnru" Date: Fri, 22 Dec 2023 17:07:49 +0100 Subject: [PATCH 04/24] Basic functionality achieved, moved code to lib --- R/plotPercentiles.R | 90 +++++++++- comp_plot_for_ngfs_magicc_data.Rmd | 275 ++++++++++++++++++++++++++--- 2 files changed, 333 insertions(+), 32 deletions(-) diff --git a/R/plotPercentiles.R b/R/plotPercentiles.R index af983ab..17f2037 100644 --- a/R/plotPercentiles.R +++ b/R/plotPercentiles.R @@ -1,3 +1,87 @@ -plotWhatever <- function(df) { - print("Hello World") -} \ No newline at end of file +#' Scenario comparison plots + +#' @author Tonn Rueter +#' @param quitte_df `quitte` style data frame containing all variables for each scenario +#' @param scenarios Character vector containing the names of the desired scenarios. If none is provided, all scenarios will be displayed +#' @param variables Character vector containing the names of the desired variables. If none is provided, all variables will be displayed +#' @importFrom dplyr filter mutate +#' @importFrom reshape2 melt +#' @importFrom stringr str_extract str_glue +#' @export +plotPercentiles <- function(quitte_frame, scenarios=NULL, variables=NULL) { + + # In the quitte data frame all perenctiles are given as individual variables + # Manipulate input data frame such that all percentiles of a given quantity + # are transformed to individual columns + df <- quitte_frame %>% + mutate( + "percentile" = stringr::str_extract(variable, "[^\\|]+?$"), + "variable" = gsub("\\|[^\\|]+$", "", variable) + ) %>% + pivot_wider( + names_from = "percentile", + values_from = "value" + ) + + # Check which function parameters have been provided and default to unique + # values from the data frame in case none have + these_scenarios <- if (is.null(scenarios)) unique(df$scenario) else scenarios + these_variables <- if (is.null(variables)) unique(df$variable) else variables + + # Set up the plot + p <- ggplot() + + # Fill plot by filtering for the requested variables and scenarios + for (this_variable in these_variables) { + for (this_scenario in these_scenarios) { + data <- filter(df, variable == this_variable & scenario == this_scenario) + p <- p + + geom_line( + data = data, aes(x = period, y = get("50.0th Percentile")) + ) + + geom_ribbon( + data = data, aes(x = period, ymin = get("33.0th Percentile"), ymax = get("67.0th Percentile")), + fill = "#68788a", alpha = 0.5 + ) + + geom_ribbon( + data = data, aes(x = period, ymin = get("5.0th Percentile"), ymax = get("95.0th Percentile")), + fill = "#68788a", alpha = 0.2 + ) + } + } + + # Depeding on the function parameters, plots need to be arranged + if (length(these_scenarios) == 1) { + # Plots all parameters for a given scenario. Y-axes need to be independent + p <- p + + facet_wrap( + vars(variable), + scales = "free_y") + + theme( + axis.title.x = element_blank()) + + ylab( + unique(df$unit)) + } else if (length(these_variables) == 1) { + # Plots a given parameter for all scenarios. Lock y-axes to improve comparison + p <- p + + facet_wrap( + vars(scenario)) + + theme( + axis.title.x = element_blank()) + + ylab( + unique(df$unit)) + } + else { + # Using facet grid when multiple variables in multiple scenarios are compared + p <- p + + facet_grid( + variable ~ scenario, + scales = "free_y") + + theme( + axis.title.x = element_blank()) + + ylab( + unique(df$unit)) + } + + return(p) +} diff --git a/comp_plot_for_ngfs_magicc_data.Rmd b/comp_plot_for_ngfs_magicc_data.Rmd index 2a0b7c1..0dde9e6 100644 --- a/comp_plot_for_ngfs_magicc_data.Rmd +++ b/comp_plot_for_ngfs_magicc_data.Rmd @@ -115,9 +115,6 @@ Our `quitte` object is now available an ready to be passed to the plotting funct Write a function that returns a `facet_wrap` ```{r} -plotPercentiles <- function(quitte_frame) { - print("Hello!") -} #mip::plotPercentile(bar) ## ngfs_data ``` @@ -138,18 +135,6 @@ df <- ngfs_data %>% df ``` -```{r} -ggplot() + - geom_line( - #data = filter(baz, variable == "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3"), - #data = baz, - #data = df, - data = filter(df, variable == "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3"), - aes(x = period, y = get("33.0th Percentile"), color=Percentile) - ) -#facet_wrap(~variable, scales = "free") -``` - ```{r} p <- ggplot() + @@ -220,19 +205,19 @@ these_please <- c( "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|33.0th Percentile", "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|50.0th Percentile", "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|67.0th Percentile", - "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|95.0th Percentile" #, + "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|95.0th Percentile", # NOTE: Theres not 5th Percentile on Surface Temperatures - # "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|10.0th Percentile", - # "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|33.0th Percentile", - # "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|50.0th Percentile", - # "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|67.0th Percentile", - # "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|90.0th Percentile", + "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|10.0th Percentile", + "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|33.0th Percentile", + "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|50.0th Percentile", + "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|67.0th Percentile", + "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|90.0th Percentile", # CH4 Fluxes - # "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|5.0th Percentile", - # "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|33.0th Percentile", - # "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|50.0th Percentile", - # "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|67.0th Percentile", - # "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|95.0th Percentile" + "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|5.0th Percentile", + "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|33.0th Percentile", + "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|50.0th Percentile", + "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|67.0th Percentile", + "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|95.0th Percentile" ) ngfs_xlsx_2_scenarios <- readxl::read_xlsx( @@ -272,7 +257,6 @@ unique(ngfs_data_2_scenarios$scenario) ```{r} df_2 <- ngfs_data_2_scenarios %>% - # group_by(scenario) %>% mutate( "percentile" = stringr::str_extract( variable, "[^\\|]+?$"), "variable" = gsub("\\|[^\\|]+$", "", variable) @@ -293,8 +277,6 @@ p <- ggplot() for (variable in unique(df_2$variable)) { for (scenario in unique(df_2$scenario)) { data <- filter(df_2, variable == variable & scenario == scenario) - labels - # print(unique(data$unit)) p <- p + geom_line( data = data, aes(x = period, y = get("50.0th Percentile"), group=1) @@ -345,7 +327,229 @@ print(p) ```{r} unique(data$unit) ``` +# Functionalize! +```{r} +plotPercentiles <- function(quitte_frame, scenarios=NULL, variables=NULL, percentiles=NULL) { + + # TODO: Test for error cases (eg. check if scenarios are actually present in the data) + + # TODO: How to provide percentiles? + # - NULL: Use 33, 66, 5, 95 as default + # - SAME: List of length 2 or length 4, eg [33, 66, 10, 90] + # - DIFF: List of Lists of length 2 or length 4, eg [33, 66, 10, 90] + if (is.null(percentiles)) { + these_percentiles <- c(5, 95, 33, 67) + # } else if (length(percentiles)) { + # these_percentiles <- percentiles + } else { + + } + # + # format_str <- "$[.1f]{which}th Percentile" + # these_percentiles <- ifelse(is.null(percentiles), NULL, stringr::str_interp(format_str, percentiles)) + # + inner_min <- stringr::str_interp(format_str, list(which=as.double(these_percentiles[1]))) + inner_max <- stringr::str_interp(format_str, list(which=as.double(these_percentiles[2]))) + outer_min <- stringr::str_interp(format_str, list(which=as.double(these_percentiles[3]))) + outer_max <- stringr::str_interp(format_str, list(which=as.double(these_percentiles[4]))) + + # print(scenarios) + # print(variables) + # print("--- LENGTHS ---") + # print(length(scenarios)) + # print(length(variables)) + # print("--- FOR scenarios ---") + # for (foo in scenarios) {print(foo)} + # print("--- FOR variables ---") + # for (bar in variables) {print(bar)} + + df <- quitte_frame %>% + mutate( + "percentile" = stringr::str_extract(variable, "[^\\|]+?$"), + "variable" = gsub("\\|[^\\|]+$", "", variable) + ) %>% + pivot_wider( + names_from = "percentile", + values_from = "value" + ) + + these_scenarios <- if (is.null(scenarios)) unique(df$scenario) else scenarios + these_variables <- if (is.null(variables)) unique(df$variable) else variables + + # these_scenarios <- ifelse(is.null(scenarios), as.character(unique(df$scenario)), sapply(scenarios, function(i) i)) + # these_variables <- ifelse(is.null(variables), unique(df$variable), sapply(variables, function(i) i)) + # these_scenarios <- ifelse(is.null(scenarios), as.character(unique(df$scenario)), scenarios) + # these_variables <- ifelse(is.null(variables), unique(df$variable), variables) + + # if (is.null(scenarios)) + # these_scenarios <- unique(df$scenario) + # } else { + # these_scenarios <- scenarios + # } + # if (is.null(variables)) { + # these_variables <- unique(df$variable) + # } else { + # these_variables <- variables + # } + + # print("--- FOR these_scenarios ---") + # for (foo in these_scenarios) {print(foo)} + # print("--- FOR these_variables ---") + # for (bar in these_variables) {print(bar)} + + # if (is.null(scenarios) & is.null(variables)) { + # these_scenarios <- unique(df$scenario) + # these_variables <- unique(df$variable) + # } else if (!is.null(scenarios)) { + # these_scenarios <- scenarios + # these_variables <- unique(df$variable) + # } else if (!is.null(variables)) { + # these_scenarios <- unique(df$scenario) + # these_variables <- variables + # } else { + # these_scenarios <- scenarios + # these_variables <- variables + # } + + # print("--- CONTENT ---") + # print(these_scenarios) + # print(these_variables) + # print("--- LENGTHS ---") + # print(length(these_scenarios)) + # print(length(these_variables)) + # print("--- TYPES ---") + # print(typeof(these_scenarios)) + # print(typeof(these_variables)) + # print("-------------") + + print("--- WHICH ---") + print(paste0("these_scenarios: ", these_scenarios)) + print(paste0("these_variables: ", these_variables)) + + print("--- PERCENTILES ---") + print(paste0("outer_max: ", outer_max)) + print(paste0("inner_max: ", inner_max)) + print(paste0("inner_min: ", inner_min)) + print(paste0("outer_min: ", outer_min)) + + p <- ggplot() + + print("--- PLOT ---") + for (this_variable in these_variables) { + print(this_variable) + for (this_scenario in these_scenarios) { + print(this_scenario) + # print(paste0(variable, scenario)) + data <- filter(df, variable == this_variable & scenario == this_scenario) + print(data) + p <- p + + geom_line( + # data = data, aes(x = period, y = get("50.0th Percentile"), group=1) + data = data, aes(x = period, y = get("50.0th Percentile")) + ) + + geom_ribbon( + # data = data, aes(x = period, ymin = get(inner_min), ymax = get(inner_max)), + data = data, aes(x = period, ymin = get("33.0th Percentile"), ymax = get("67.0th Percentile")), + fill = "#68788a", alpha = 0.5 + ) + + geom_ribbon( + # data = data, aes(x = period, ymin = get(outer_min), ymax = get(outer_max)), + data = data, aes(x = period, ymin = get("5.0th Percentile"), ymax = get("95.0th Percentile")), + fill = "#68788a", alpha = 0.2 + ) + } + } + + # if (length(scenarios) == 1) { + if (length(these_scenarios) == 1) { + p <- p + + facet_wrap( + vars(variable), + scales = "free_y") + + theme( + axis.title.x = element_blank() + ) + + ylab( + unique(df$unit)) + # } else if (!is.null(variables)) { + } else if (length(these_variables) == 1) { + p <- p + + facet_wrap( + vars(scenario)) + + theme( + axis.title.x = element_blank() + ) + + ylab( + unique(df$unit)) + } + else { + # Using facet grid + p <- p + + facet_grid( + variable ~ scenario, + scales = "free_y") + + theme( + axis.title.x = element_blank() + ) + + ylab( + unique(df$unit)) + } + + # p <- p + + # facet_grid( + # variable ~ scenario, + # scales = "free_y") + + # theme( + # axis.title.x = element_blank() + # ) + + # ylab( + # unique(df$unit)) + + return(p) +} + +# print(plotPercentiles(ngfs_data_2_scenarios)) +# print(plotPercentiles(ngfs_data_2_scenarios, which = "scenarios")) +# print(plotPercentiles(ngfs_data_2_scenarios, which = "variables")) +# print(plotPercentiles(ngfs_data_2_scenarios, which = "both")) +# try(print(plotPercentiles(ngfs_data_2_scenarios, which = "asd"))) + +# print(plotPercentiles(ngfs_data_2_scenarios, variables = c("AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3"))) +print(plotPercentiles(ngfs_data_2_scenarios, scenarios = c("d_another"))) + +# print(plotPercentiles(ngfs_data_2_scenarios, scenarios = c("d_another", "d_delfrag"))) +# print(plotPercentiles(ngfs_data_2_scenarios)) +# print(plotPercentiles(ngfs_data_2_scenarios, scenarios = c("d_another"), variables = c("AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3"))) +# print(plotPercentiles(ngfs_data_2_scenarios, scenarios = c("d_another"), variables = c("AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3", "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3"))) +# print(plotPercentiles(ngfs_data_2_scenarios, variables = c("AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3", "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3"))) +# print(plotPercentiles(ngfs_data_2_scenarios, scenarios = c("d_another", "d_delfrag"), variables = c("AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3", "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3"))) + +``` + + + +```{r} +filter(df, variable == "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3" & scenario == scenario) +``` + + +## Function tests + +Must use pre-processed `ngfs_data_2_scenarios` + +```{r} +# print(plotPercentiles(ngfs_data_2_scenarios)) +# print(plotPercentiles(ngfs_data_2_scenarios, which = "scenarios")) +# print(plotPercentiles(ngfs_data_2_scenarios, which = "variables")) +# print(plotPercentiles(ngfs_data_2_scenarios, which = "both")) +# try(print(plotPercentiles(ngfs_data_2_scenarios, which = "asd"))) + +print(plotPercentiles(ngfs_data_2_scenarios, scenarios = c("d_another", "d_delfrag"))) +# print(plotPercentiles(ngfs_data_2_scenarios, variables = c("AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3"))) +# print(plotPercentiles(ngfs_data_2_scenarios)) + +``` # The End @@ -355,6 +559,19 @@ knitr::knit_exit() # Playground + +```{r} +# print(ngfs_data_2_scenarios) + +which_example = "variables=AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3" +#which_example = "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|33.0th Percentile" +# which_example = "variables=AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3,AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|90.0th Percentile" + +print(stringr::str_split(gsub(pattern = "variables\\=", x = which_example, replacement = ""), pattern = ",")) + + +``` + ## Learning `facet_wrap` ```{r} From 32fe57bb207d333963a63932d8140deb02cd181a Mon Sep 17 00:00:00 2001 From: "PIKACCOUNTS\\tonnru" Date: Wed, 3 Jan 2024 16:51:58 +0100 Subject: [PATCH 05/24] Added sanity checks --- R/plotPercentiles.R | 65 +++++++++++++++++++++++++++++++++------------ 1 file changed, 48 insertions(+), 17 deletions(-) diff --git a/R/plotPercentiles.R b/R/plotPercentiles.R index 17f2037..456b02a 100644 --- a/R/plotPercentiles.R +++ b/R/plotPercentiles.R @@ -1,19 +1,23 @@ -#' Scenario comparison plots +#' Comparison plots show user selected variables obtained from different scenario +#' runs #' @author Tonn Rueter -#' @param quitte_df `quitte` style data frame containing all variables for each scenario +#' @param df `quitte` style data frame containing all variables for each scenario #' @param scenarios Character vector containing the names of the desired scenarios. If none is provided, all scenarios will be displayed #' @param variables Character vector containing the names of the desired variables. If none is provided, all variables will be displayed #' @importFrom dplyr filter mutate #' @importFrom reshape2 melt -#' @importFrom stringr str_extract str_glue +#' @importFrom stringr str_extract #' @export -plotPercentiles <- function(quitte_frame, scenarios=NULL, variables=NULL) { +plotPercentiles <- function(df, scenarios=NULL, variables=NULL) { # In the quitte data frame all perenctiles are given as individual variables # Manipulate input data frame such that all percentiles of a given quantity - # are transformed to individual columns - df <- quitte_frame %>% + # are transformed to individual columns. Variable names in the quitte data + # frame follow the format "Any|Variable|5.0th Percentile". The regular + # expressions below divide the variable name into the prefix and the + # percentile specifier + data <- df %>% mutate( "percentile" = stringr::str_extract(variable, "[^\\|]+?$"), "variable" = gsub("\\|[^\\|]+$", "", variable) @@ -23,10 +27,26 @@ plotPercentiles <- function(quitte_frame, scenarios=NULL, variables=NULL) { values_from = "value" ) + # Check which scenarios/variabes are available + unique_scenarios <- unique(data$scenario) + unique_variables <- unique(data$variable) + # Check which function parameters have been provided and default to unique # values from the data frame in case none have - these_scenarios <- if (is.null(scenarios)) unique(df$scenario) else scenarios - these_variables <- if (is.null(variables)) unique(df$variable) else variables + these_scenarios <- if (is.null(scenarios)) { + unique_scenarios + } else if (all_items_available(scenarios, unique_scenarios, warn=TRUE)) { + scenarios + } else { + stop("Provided scenario is missing in data") + } + these_variables <- if (is.null(variables)) { + unique_variables + } else if (all_items_available(variables, unique_variables, warn=TRUE)) { + variables + } else { + stop("Provided variable is missing in data") + } # Set up the plot p <- ggplot() @@ -34,33 +54,34 @@ plotPercentiles <- function(quitte_frame, scenarios=NULL, variables=NULL) { # Fill plot by filtering for the requested variables and scenarios for (this_variable in these_variables) { for (this_scenario in these_scenarios) { - data <- filter(df, variable == this_variable & scenario == this_scenario) + plot_data <- filter(data, variable == this_variable & scenario == this_scenario) p <- p + geom_line( - data = data, aes(x = period, y = get("50.0th Percentile")) + data = plot_data, aes(x = period, y = get("50.0th Percentile")) ) + geom_ribbon( - data = data, aes(x = period, ymin = get("33.0th Percentile"), ymax = get("67.0th Percentile")), + data = plot_data, aes(x = period, ymin = get("33.0th Percentile"), ymax = get("67.0th Percentile")), fill = "#68788a", alpha = 0.5 ) + geom_ribbon( - data = data, aes(x = period, ymin = get("5.0th Percentile"), ymax = get("95.0th Percentile")), + data = plot_data, aes(x = period, ymin = get("5.0th Percentile"), ymax = get("95.0th Percentile")), fill = "#68788a", alpha = 0.2 ) } } - # Depeding on the function parameters, plots need to be arranged + # Depending on the function parameters, plots need to be arranged if (length(these_scenarios) == 1) { # Plots all parameters for a given scenario. Y-axes need to be independent p <- p + facet_wrap( vars(variable), - scales = "free_y") + + scales = "free_y", + ncol = 1) + theme( axis.title.x = element_blank()) + ylab( - unique(df$unit)) + unique(data$unit)) } else if (length(these_variables) == 1) { # Plots a given parameter for all scenarios. Lock y-axes to improve comparison p <- p + @@ -69,7 +90,7 @@ plotPercentiles <- function(quitte_frame, scenarios=NULL, variables=NULL) { theme( axis.title.x = element_blank()) + ylab( - unique(df$unit)) + unique(data$unit)) } else { # Using facet grid when multiple variables in multiple scenarios are compared @@ -80,8 +101,18 @@ plotPercentiles <- function(quitte_frame, scenarios=NULL, variables=NULL) { theme( axis.title.x = element_blank()) + ylab( - unique(df$unit)) + unique(data$unit)) } return(p) } + +all_items_available <- function(selection, available, warn = FALSE) { + for (item in selection) { + if (!(item %in% available)) { + if (warn) warning(paste0("'", item, "' missing in available data")) + return(FALSE) + } + } + return(TRUE) +} From 48132e36c89eb6547ee18e09197b296a9338f61a Mon Sep 17 00:00:00 2001 From: "PIKACCOUNTS\\tonnru" Date: Wed, 3 Jan 2024 16:52:33 +0100 Subject: [PATCH 06/24] Changes after reevaluation --- comp_plot_for_ngfs_magicc_data.Rmd | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/comp_plot_for_ngfs_magicc_data.Rmd b/comp_plot_for_ngfs_magicc_data.Rmd index 0dde9e6..266cb05 100644 --- a/comp_plot_for_ngfs_magicc_data.Rmd +++ b/comp_plot_for_ngfs_magicc_data.Rmd @@ -655,6 +655,21 @@ ngfs_data <- readxl::read_xlsx( ngfs_data ``` +## Check element of vector against other vector + +```{r} +foo <- c("1", "2", "7") +bar <- c("1", "2", "3", "4", "5", "6") + +for (item in foo) { + if (!(item %in% bar)) print(paste(item, "missing", sep = " ")) +} +``` + +```{r} +item %in% foo +``` + ## Concatenate string vectors and constants ```{r} From 21caed2cb29042de0da5e049d690580e214b113c Mon Sep 17 00:00:00 2001 From: "PIKACCOUNTS\\tonnru" Date: Wed, 3 Jan 2024 16:54:42 +0100 Subject: [PATCH 07/24] Test cases for plotPercentiles --- tests_plotPercentiles.Rmd | 152 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 tests_plotPercentiles.Rmd diff --git a/tests_plotPercentiles.Rmd b/tests_plotPercentiles.Rmd new file mode 100644 index 0000000..ef4f443 --- /dev/null +++ b/tests_plotPercentiles.Rmd @@ -0,0 +1,152 @@ +--- +title: "Set of tests for `plotPercentiles.R`" +output: + html_document: + theme: paper + toc: true + toc_float: + collapsed: false + toc_depth: 2 + code_folding: hide + df_print: kable + html_notebook: + code_folding: hide + pdf_document: default +--- + + +```{r message=FALSE, warning=FALSE} +library(ggplot2) +library(dplyr) +library(tidyr) +library(quitte) +library(readxl) +``` + +# Read data + +```{r} +these_please <- c( + # CO2 concentrations + "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|5.0th Percentile", + "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|33.0th Percentile", + "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|50.0th Percentile", + "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|67.0th Percentile", + "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|95.0th Percentile", + # NOTE: Theres not 5th Percentile on Surface Temperatures + "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|10.0th Percentile", + "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|33.0th Percentile", + "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|50.0th Percentile", + "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|67.0th Percentile", + "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|90.0th Percentile", + # CH4 Fluxes + "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|5.0th Percentile", + "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|33.0th Percentile", + "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|50.0th Percentile", + "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|67.0th Percentile", + "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|95.0th Percentile" +) + +ngfs_data <- readxl::read_xlsx( + # "~/../lab/misc_data/ngfs-magicc-data-from-csv-v2.xlsx", + "~/../lab/misc_data/ngfs-magicc-data-from-csv-v3-extended-scenarios.xlsx", + sheet = "ngfs-magicc-data" + ) %>% + # Cast yearly columns as doubles + mutate_at(vars(starts_with(c("1", "2"))), as.double) %>% + # Exclude rows whose "Variable" column does not end in Percentile, because + # these do not contain numerical data in the columns for each year + #filter(grepl("Percentile", Variable)) %>% + # Take only those variables that shall be plotted later on + filter(Variable %in% these_please) %>% + reshape2::melt( + c("Model", "Scenario", "Region", "Variable", "Unit"), + variable.name = "period" + ) %>% + as.quitte() + +print(ngfs_data) +``` + +## Test function + +Source package + +```{r} +# library("plotPercentiles", lib.loc = "C:/Users/tonnru/lab/mip/R") +source("./R/plotPercentiles.R") +``` + +Check out different scenarios/variables + +```{r} +print(unique(ngfs_data$scenario)) +print(unique(ngfs_data$variable)) +``` +### Sanity checks + +Function is supposed to fail here + +```{r} +try(print(plotPercentiles(ngfs_data, scenarios = "blerk"))) +try(print(plotPercentiles(ngfs_data, variables = "blork"))) +``` +### One variable, all scenarios + +```{r} +print(plotPercentiles(ngfs_data, variables = c("AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3"))) +# print(plotPercentiles(ngfs_data, scenarios = c("d_another", "d_delfrag"))) +# print(plotPercentiles(ngfs_data)) +``` +### Single variable, single scenario + +```{r} +print(plotPercentiles(ngfs_data, variables = c("AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3"), scenarios = c("d_delfrag"))) +``` +### Multiple variables, single scenario + +```{r} +print(plotPercentiles( + ngfs_data, + variables = c( + "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3", + "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3" + ), + scenarios = c("d_delfrag")) +) +``` + +### Multiple variables, single scenario + +```{r} +print(plotPercentiles(ngfs_data, scenarios = c("d_delfrag"))) +``` + +### Multiple variables, multiple scenarios + +```{r} +print(plotPercentiles(ngfs_data, scenarios = c("d_another", "d_delfrag"))) +``` + + +### Multiple variables, multiple scenarios + +```{r} +print(plotPercentiles( + ngfs_data, + variables = c( + "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3", + "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3" + ), + scenarios = c( + "d_another", + "d_delfrag" + ) +)) +``` + +### All variables, all scenarios (default call) + +```{r} +print(plotPercentiles(ngfs_data)) +``` From 0dd2c0e952b4070dafb810df32c2ae156437ffbb Mon Sep 17 00:00:00 2001 From: "PIKACCOUNTS\\tonnru" Date: Fri, 5 Jan 2024 13:33:40 +0100 Subject: [PATCH 08/24] Added stringr to imports --- DESCRIPTION | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 79fd123..d24c5d4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -36,14 +36,15 @@ Imports: rlang, shiny, tidyr, - trafficlight -Suggests: + trafficlight, + stringr +Suggests: covr, gdxrrw, knitr, rmarkdown, testthat -VignetteBuilder: +VignetteBuilder: knitr Encoding: UTF-8 LazyData: yes From 3c2606a211279f483a80a431d3299e864ab0e318 Mon Sep 17 00:00:00 2001 From: "PIKACCOUNTS\\tonnru" Date: Fri, 5 Jan 2024 13:34:42 +0100 Subject: [PATCH 09/24] Make linter happy --- R/plotPercentiles.R | 77 +++++++++++++++++++-------------------------- 1 file changed, 32 insertions(+), 45 deletions(-) diff --git a/R/plotPercentiles.R b/R/plotPercentiles.R index 456b02a..bf6c56a 100644 --- a/R/plotPercentiles.R +++ b/R/plotPercentiles.R @@ -3,13 +3,13 @@ #' @author Tonn Rueter #' @param df `quitte` style data frame containing all variables for each scenario -#' @param scenarios Character vector containing the names of the desired scenarios. If none is provided, all scenarios will be displayed -#' @param variables Character vector containing the names of the desired variables. If none is provided, all variables will be displayed +#' @param scenarios Character vector contains names of the desired scenarios. If none, all scenarios will be displayed +#' @param variables Character vector contains names of the desired variables. If none, all variables will be displayed #' @importFrom dplyr filter mutate #' @importFrom reshape2 melt #' @importFrom stringr str_extract #' @export -plotPercentiles <- function(df, scenarios=NULL, variables=NULL) { +plotPercentiles <- function(df, scenarios = NULL, variables = NULL) { # In the quitte data frame all perenctiles are given as individual variables # Manipulate input data frame such that all percentiles of a given quantity @@ -19,8 +19,8 @@ plotPercentiles <- function(df, scenarios=NULL, variables=NULL) { # percentile specifier data <- df %>% mutate( - "percentile" = stringr::str_extract(variable, "[^\\|]+?$"), - "variable" = gsub("\\|[^\\|]+$", "", variable) + "percentile" = stringr::str_extract(.data$variable, "[^\\|]+?$"), + "variable" = gsub("\\|[^\\|]+$", "", .data$variable) ) %>% pivot_wider( names_from = "percentile", @@ -28,21 +28,21 @@ plotPercentiles <- function(df, scenarios=NULL, variables=NULL) { ) # Check which scenarios/variabes are available - unique_scenarios <- unique(data$scenario) - unique_variables <- unique(data$variable) + uniqueScenarios <- unique(data$scenario) + uniqueVariables <- unique(data$variable) # Check which function parameters have been provided and default to unique # values from the data frame in case none have - these_scenarios <- if (is.null(scenarios)) { - unique_scenarios - } else if (all_items_available(scenarios, unique_scenarios, warn=TRUE)) { + theseScenarios <- if (is.null(scenarios)) { + uniqueScenarios + } else if (allItemsAvailable(scenarios, uniqueScenarios, warn = TRUE)) { scenarios } else { stop("Provided scenario is missing in data") } - these_variables <- if (is.null(variables)) { - unique_variables - } else if (all_items_available(variables, unique_variables, warn=TRUE)) { + theseVariables <- if (is.null(variables)) { + uniqueVariables + } else if (allItemsAvailable(variables, uniqueVariables, warn = TRUE)) { variables } else { stop("Provided variable is missing in data") @@ -52,62 +52,49 @@ plotPercentiles <- function(df, scenarios=NULL, variables=NULL) { p <- ggplot() # Fill plot by filtering for the requested variables and scenarios - for (this_variable in these_variables) { - for (this_scenario in these_scenarios) { - plot_data <- filter(data, variable == this_variable & scenario == this_scenario) + for (thisVariable in theseVariables) { + for (thisScenario in theseScenarios) { + plotData <- filter(data, .data$variable == thisVariable & .data$scenario == thisScenario) p <- p + geom_line( - data = plot_data, aes(x = period, y = get("50.0th Percentile")) + data = plotData, aes(x = .data$period, y = get("50.0th Percentile")) ) + geom_ribbon( - data = plot_data, aes(x = period, ymin = get("33.0th Percentile"), ymax = get("67.0th Percentile")), + data = plotData, aes(x = .data$period, ymin = get("33.0th Percentile"), ymax = get("67.0th Percentile")), fill = "#68788a", alpha = 0.5 ) + geom_ribbon( - data = plot_data, aes(x = period, ymin = get("5.0th Percentile"), ymax = get("95.0th Percentile")), + data = plotData, aes(x = .data$period, ymin = get("5.0th Percentile"), ymax = get("95.0th Percentile")), fill = "#68788a", alpha = 0.2 ) } } # Depending on the function parameters, plots need to be arranged - if (length(these_scenarios) == 1) { + if (length(theseScenarios) == 1) { # Plots all parameters for a given scenario. Y-axes need to be independent p <- p + - facet_wrap( - vars(variable), - scales = "free_y", - ncol = 1) + - theme( - axis.title.x = element_blank()) + - ylab( - unique(data$unit)) - } else if (length(these_variables) == 1) { + facet_wrap(vars(.data$variable), scales = "free_y", ncol = 1) + + theme(axis.title.x = element_blank()) + + ylab(unique(data$unit)) + } else if (length(theseVariables) == 1) { # Plots a given parameter for all scenarios. Lock y-axes to improve comparison p <- p + - facet_wrap( - vars(scenario)) + - theme( - axis.title.x = element_blank()) + - ylab( - unique(data$unit)) - } - else { + facet_wrap(vars(.data$scenario)) + + theme(axis.title.x = element_blank()) + + ylab(unique(data$unit)) + } else { # Using facet grid when multiple variables in multiple scenarios are compared p <- p + - facet_grid( - variable ~ scenario, - scales = "free_y") + - theme( - axis.title.x = element_blank()) + - ylab( - unique(data$unit)) + facet_grid(.data$variable ~ .data$scenario, scales = "free_y") + + theme(axis.title.x = element_blank()) + + ylab(unique(data$unit)) } return(p) } -all_items_available <- function(selection, available, warn = FALSE) { +allItemsAvailable <- function(selection, available, warn = FALSE) { for (item in selection) { if (!(item %in% available)) { if (warn) warning(paste0("'", item, "' missing in available data")) From d61c5757427286691c4c0df10ac514448f4c1ed7 Mon Sep 17 00:00:00 2001 From: "PIKACCOUNTS\\tonnru" Date: Fri, 5 Jan 2024 14:48:45 +0100 Subject: [PATCH 10/24] Removed covr from Suggests since it is not used anywhere --- DESCRIPTION | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index d24c5d4..f4139be 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Type: Package Package: mip Title: Comparison of multi-model runs -Version: 0.147.0 -Date: 2023-12-04 +Version: 0.148.0 +Date: 2024-01-05 Authors@R: c( person("David", "Klein", , "dklein@pik-potsdam.de", role = c("aut", "cre")), person("Jan Philipp", "Dietrich", , "dietrich@pik-potsdam.de", role = "aut"), @@ -39,7 +39,6 @@ Imports: trafficlight, stringr Suggests: - covr, gdxrrw, knitr, rmarkdown, From 0b923e13199c7f73195fa0c2898ff815aaaf36b4 Mon Sep 17 00:00:00 2001 From: "PIKACCOUNTS\\tonnru" Date: Fri, 5 Jan 2024 14:49:27 +0100 Subject: [PATCH 11/24] Added Tonn as author --- DESCRIPTION | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index f4139be..0ab5a90 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -11,7 +11,8 @@ Authors@R: c( person("Miodrag", "Stevanovic", , "miodrag@pik-potsdam.de", role = "aut"), person("Stephen", "Wirth", , "wirth@pik-potsdam.de", role = "aut"), person("Pascal", "Führlich", , "pascal.fuehrlich@pik-potsdam.de", role = "aut"), - person("Oliver", "Richters", role = "aut") + person("Oliver", "Richters", role = "aut"), + person("Tonn", "Rueter", role = "aut") ) Description: Package contains generic functions to produce comparison plots of multi-model runs. From 356232474ca4122dcab6fce8dac17f3a6d4a491e Mon Sep 17 00:00:00 2001 From: "PIKACCOUNTS\\tonnru" Date: Fri, 5 Jan 2024 14:50:34 +0100 Subject: [PATCH 12/24] Removed files introduced during development --- comp_plot_for_ngfs_magicc_data.Rmd | 764 ----------------------------- tests_plotPercentiles.Rmd | 152 ------ 2 files changed, 916 deletions(-) delete mode 100644 comp_plot_for_ngfs_magicc_data.Rmd delete mode 100644 tests_plotPercentiles.Rmd diff --git a/comp_plot_for_ngfs_magicc_data.Rmd b/comp_plot_for_ngfs_magicc_data.Rmd deleted file mode 100644 index 266cb05..0000000 --- a/comp_plot_for_ngfs_magicc_data.Rmd +++ /dev/null @@ -1,764 +0,0 @@ ---- -title: "Improve cs2 climate plots of temperature & concentration distributions from MAGICC" -output: - html_document: - theme: paper - toc: true - toc_float: - collapsed: false - toc_depth: 2 - code_folding: hide - df_print: kable - html_notebook: - code_folding: hide - pdf_document: default ---- - - -```{r message=FALSE, warning=FALSE} -library(ggplot2) -library(dplyr) -library(tidyr) -library(quitte) -library(readxl) -``` - -# Data! - -Currently uses XLSX, unsure if this stays use `quitte` instead. Goal is to have a quitte Object. We need to do some more work before reshaping the dset and creating a quitte object from. This seems to depend on the XLSX itself and therefore other methods for reading it might produce different results. However, the most important part is to select for the variable to be displayed later on - -```{r} -these_please <- c( - # CO2 concentrations - "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|5.0th Percentile", - "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|33.0th Percentile", - "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|50.0th Percentile", - "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|67.0th Percentile", - "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|95.0th Percentile", - # NOTE: Theres not 5th Percentile on Surface Temperatures - "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|10.0th Percentile", - "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|33.0th Percentile", - "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|50.0th Percentile", - "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|67.0th Percentile", - "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|90.0th Percentile", - # CH4 Fluxes - "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|5.0th Percentile", - "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|33.0th Percentile", - "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|50.0th Percentile", - "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|67.0th Percentile", - "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|95.0th Percentile" -) - -ngfs_xlsx <- readxl::read_xlsx( - "~/../lab/misc_data/ngfs-magicc-data-from-csv-v2.xlsx", - sheet = "ngfs-magicc-data" -) %>% - # Cast yearly columns as doubles - mutate_at(vars(starts_with(c("1", "2"))), as.double) %>% - # Exclude rows whose "Variable" column does not end in Percentile, because - # these do not contain numerical data in the columns for each year - #filter(grepl("Percentile", Variable)) %>% - # Take only those variables that shall be plotted later on - filter(Variable %in% these_please) - -ngfs_xlsx -``` - -Now for the fun part: Reshaping the whole thang into a `quitte` object. Year columns need to become lines here. Can be done with `reshape2::melt`: - -```{r} -reshape2::melt( - ngfs_xlsx, - #id.vars = seq(1:5), - c("Model", "Scenario", "Region", "Variable", "Unit"), - variable.name = "period" -) -``` - -Or with `dplyr::pivot_longer`: - -```{r} -pivot_longer(ngfs_xlsx, starts_with(c("1", "2")), names_to = "period", values_to = "value") -``` - -Finally, create a `quitte` object (which is basically a list with specified columns) - -```{r} -ngfs_data <- as.quitte(ngfs_xlsx %>% - pivot_longer( - starts_with(c("1", "2")), - names_to = "period", - values_to = "value" - ) -) - -ngfs_data <- as.quitte(ngfs_xlsx %>% - reshape2::melt( - c("Model", "Scenario", "Region", "Variable", "Unit"), - variable.name = "period" - ) -) - -print(ngfs_data) -``` - -By the way: - -```{r} -print(typeof(ngfs_data)) -``` - -Our `quitte` object is now available an ready to be passed to the plotting function. - -# Plotting! - -Write a function that returns a `facet_wrap` - -```{r} -#mip::plotPercentile(bar) ## -ngfs_data -``` -Need to collect all the values for each year - -```{r} -df <- ngfs_data %>% - mutate( - "percentile" = stringr::str_extract( variable, "[^\\|]+?$"), - "variable" = gsub("\\|[^\\|]+$", "", variable) - ) %>% - pivot_wider( - names_from = "percentile", - values_from = "value" - ) - -# colnames(df) -df -``` - -```{r} - -p <- ggplot() + - geom_line( - # data = df, - data = filter(df, variable == "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3"), - aes(x = period, y = get("50.0th Percentile"), group=1) - ) + - geom_ribbon( - data = filter(df, variable == "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3"), - aes(x = period, ymin = get("33.0th Percentile"), ymax = get("67.0th Percentile")), - fill = "#68788a", - alpha = 0.5 - ) + - geom_ribbon( - data = filter(df, variable == "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3"), - aes(x = period, ymin = get("5.0th Percentile"), ymax = get("95.0th Percentile")), - fill = "#68788a", - alpha = 0.2 - ) - -print(p) -``` -# Case: Multiple variables - -```{r} -unique(df$variable) -``` -```{r} -p <- ggplot() - -for (variable in unique(df$variable)) { - p <- p + - geom_line( - # data = df, - data = filter(df, variable == variable), - aes(x = period, y = get("50.0th Percentile"), group=1) - ) + - geom_ribbon( - data = filter(df, variable == variable), - aes(x = period, ymin = get("33.0th Percentile"), ymax = get("67.0th Percentile")), - fill = "#68788a", - alpha = 0.5 - ) + - geom_ribbon( - data = filter(df, variable == variable), - aes(x = period, ymin = get("5.0th Percentile"), ymax = get("95.0th Percentile")), - fill = "#68788a", - alpha = 0.2 - ) -} - -p <- p + facet_wrap(vars(variable), scales = "free") - -print(p) -``` - -# Multiple scenario dataset - -In this dset, variables are now dublicates that belong to different scenarios. Again - -0a) Read in data - -```{r} -these_please <- c( - # CO2 concentrations - "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|5.0th Percentile", - "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|33.0th Percentile", - "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|50.0th Percentile", - "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|67.0th Percentile", - "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|95.0th Percentile", - # NOTE: Theres not 5th Percentile on Surface Temperatures - "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|10.0th Percentile", - "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|33.0th Percentile", - "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|50.0th Percentile", - "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|67.0th Percentile", - "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|90.0th Percentile", - # CH4 Fluxes - "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|5.0th Percentile", - "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|33.0th Percentile", - "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|50.0th Percentile", - "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|67.0th Percentile", - "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|95.0th Percentile" -) - -ngfs_xlsx_2_scenarios <- readxl::read_xlsx( - "~/../lab/misc_data/ngfs-magicc-data-from-csv-v3-extended-scenarios.xlsx", - sheet = "ngfs-magicc-data" -) %>% - # Cast yearly columns as doubles - mutate_at(vars(starts_with(c("1", "2"))), as.double) %>% - # Exclude rows whose "Variable" column does not end in Percentile, because - # these do not contain numerical data in the columns for each year - #filter(grepl("Percentile", Variable)) %>% - # Take only those variables that shall be plotted later on - filter(Variable %in% these_please) - -ngfs_xlsx_2_scenarios -``` - -```{r} -unique(ngfs_xlsx_2_scenarios$Scenario) -``` - -0b) Transform to `quitte` object - -```{r} -ngfs_data_2_scenarios <- as.quitte(ngfs_xlsx_2_scenarios %>% - reshape2::melt( - c("Model", "Scenario", "Region", "Variable", "Unit"), - variable.name = "period" - ) -) - -# print(ngfs_data_2_scenarios) -unique(ngfs_data_2_scenarios$scenario) -``` - -1) Transform data - -```{r} -df_2 <- ngfs_data_2_scenarios %>% - mutate( - "percentile" = stringr::str_extract( variable, "[^\\|]+?$"), - "variable" = gsub("\\|[^\\|]+$", "", variable) - ) %>% - pivot_wider( - names_from = "percentile", - values_from = "value" - ) - -# colnames(df_2) -# df_2 -unique(df_2$scenario) -``` - -```{r} -p <- ggplot() - -for (variable in unique(df_2$variable)) { - for (scenario in unique(df_2$scenario)) { - data <- filter(df_2, variable == variable & scenario == scenario) - p <- p + - geom_line( - data = data, aes(x = period, y = get("50.0th Percentile"), group=1) - ) + - geom_ribbon( - data = data, aes(x = period, ymin = get("33.0th Percentile"), ymax = get("67.0th Percentile")), - fill = "#68788a", - alpha = 0.5 - ) + - geom_ribbon( - data = data, aes(x = period, ymin = get("5.0th Percentile"), ymax = get("95.0th Percentile")), - fill = "#68788a", - alpha = 0.2 - ) - } -} - -# p <- p + facet_wrap(vars(variable), scales = "free", ncol = length(unique(df_2$scenario))) -# p <- p + facet_wrap(vars(variable), scales = "free") -# p <- p + facet_wrap(vars(scenario, variable), scales = "free") -# p <- p + facet_wrap(vars(scenario, variable), scales = "free", -# strip.position = "left", -# # strip.position = NULL, -# labeller = as_labeller(c(A = "Currents (A)", V = "Voltage (V)"))) + -# ylab(NULL) #+ -# theme(strip.background = element_blank(), -# strip.placement = "outside") -# p <- p + facet_wrap(vars(scenario, variable), scales = "free") - -# Using facet grid -p <- p + facet_grid(variable ~ scenario, scales = "free_y") + - theme( - axis.title.x = element_blank() - ) + ylab(unique(df_2$unit)) -# - -# p <- p + -# facet_wrap( -# vars(scenario, variable), -# scales = "free_y") + -# theme( -# axis.title.x = element_blank(), -# axis.title.y = element_blank() -# ) - -print(p) -``` -```{r} -unique(data$unit) -``` -# Functionalize! - -```{r} -plotPercentiles <- function(quitte_frame, scenarios=NULL, variables=NULL, percentiles=NULL) { - - # TODO: Test for error cases (eg. check if scenarios are actually present in the data) - - # TODO: How to provide percentiles? - # - NULL: Use 33, 66, 5, 95 as default - # - SAME: List of length 2 or length 4, eg [33, 66, 10, 90] - # - DIFF: List of Lists of length 2 or length 4, eg [33, 66, 10, 90] - if (is.null(percentiles)) { - these_percentiles <- c(5, 95, 33, 67) - # } else if (length(percentiles)) { - # these_percentiles <- percentiles - } else { - - } - # - # format_str <- "$[.1f]{which}th Percentile" - # these_percentiles <- ifelse(is.null(percentiles), NULL, stringr::str_interp(format_str, percentiles)) - # - inner_min <- stringr::str_interp(format_str, list(which=as.double(these_percentiles[1]))) - inner_max <- stringr::str_interp(format_str, list(which=as.double(these_percentiles[2]))) - outer_min <- stringr::str_interp(format_str, list(which=as.double(these_percentiles[3]))) - outer_max <- stringr::str_interp(format_str, list(which=as.double(these_percentiles[4]))) - - # print(scenarios) - # print(variables) - # print("--- LENGTHS ---") - # print(length(scenarios)) - # print(length(variables)) - # print("--- FOR scenarios ---") - # for (foo in scenarios) {print(foo)} - # print("--- FOR variables ---") - # for (bar in variables) {print(bar)} - - df <- quitte_frame %>% - mutate( - "percentile" = stringr::str_extract(variable, "[^\\|]+?$"), - "variable" = gsub("\\|[^\\|]+$", "", variable) - ) %>% - pivot_wider( - names_from = "percentile", - values_from = "value" - ) - - these_scenarios <- if (is.null(scenarios)) unique(df$scenario) else scenarios - these_variables <- if (is.null(variables)) unique(df$variable) else variables - - # these_scenarios <- ifelse(is.null(scenarios), as.character(unique(df$scenario)), sapply(scenarios, function(i) i)) - # these_variables <- ifelse(is.null(variables), unique(df$variable), sapply(variables, function(i) i)) - # these_scenarios <- ifelse(is.null(scenarios), as.character(unique(df$scenario)), scenarios) - # these_variables <- ifelse(is.null(variables), unique(df$variable), variables) - - # if (is.null(scenarios)) - # these_scenarios <- unique(df$scenario) - # } else { - # these_scenarios <- scenarios - # } - # if (is.null(variables)) { - # these_variables <- unique(df$variable) - # } else { - # these_variables <- variables - # } - - # print("--- FOR these_scenarios ---") - # for (foo in these_scenarios) {print(foo)} - # print("--- FOR these_variables ---") - # for (bar in these_variables) {print(bar)} - - # if (is.null(scenarios) & is.null(variables)) { - # these_scenarios <- unique(df$scenario) - # these_variables <- unique(df$variable) - # } else if (!is.null(scenarios)) { - # these_scenarios <- scenarios - # these_variables <- unique(df$variable) - # } else if (!is.null(variables)) { - # these_scenarios <- unique(df$scenario) - # these_variables <- variables - # } else { - # these_scenarios <- scenarios - # these_variables <- variables - # } - - # print("--- CONTENT ---") - # print(these_scenarios) - # print(these_variables) - # print("--- LENGTHS ---") - # print(length(these_scenarios)) - # print(length(these_variables)) - # print("--- TYPES ---") - # print(typeof(these_scenarios)) - # print(typeof(these_variables)) - # print("-------------") - - print("--- WHICH ---") - print(paste0("these_scenarios: ", these_scenarios)) - print(paste0("these_variables: ", these_variables)) - - print("--- PERCENTILES ---") - print(paste0("outer_max: ", outer_max)) - print(paste0("inner_max: ", inner_max)) - print(paste0("inner_min: ", inner_min)) - print(paste0("outer_min: ", outer_min)) - - p <- ggplot() - - print("--- PLOT ---") - for (this_variable in these_variables) { - print(this_variable) - for (this_scenario in these_scenarios) { - print(this_scenario) - # print(paste0(variable, scenario)) - data <- filter(df, variable == this_variable & scenario == this_scenario) - print(data) - p <- p + - geom_line( - # data = data, aes(x = period, y = get("50.0th Percentile"), group=1) - data = data, aes(x = period, y = get("50.0th Percentile")) - ) + - geom_ribbon( - # data = data, aes(x = period, ymin = get(inner_min), ymax = get(inner_max)), - data = data, aes(x = period, ymin = get("33.0th Percentile"), ymax = get("67.0th Percentile")), - fill = "#68788a", alpha = 0.5 - ) + - geom_ribbon( - # data = data, aes(x = period, ymin = get(outer_min), ymax = get(outer_max)), - data = data, aes(x = period, ymin = get("5.0th Percentile"), ymax = get("95.0th Percentile")), - fill = "#68788a", alpha = 0.2 - ) - } - } - - # if (length(scenarios) == 1) { - if (length(these_scenarios) == 1) { - p <- p + - facet_wrap( - vars(variable), - scales = "free_y") + - theme( - axis.title.x = element_blank() - ) + - ylab( - unique(df$unit)) - # } else if (!is.null(variables)) { - } else if (length(these_variables) == 1) { - p <- p + - facet_wrap( - vars(scenario)) + - theme( - axis.title.x = element_blank() - ) + - ylab( - unique(df$unit)) - } - else { - # Using facet grid - p <- p + - facet_grid( - variable ~ scenario, - scales = "free_y") + - theme( - axis.title.x = element_blank() - ) + - ylab( - unique(df$unit)) - } - - # p <- p + - # facet_grid( - # variable ~ scenario, - # scales = "free_y") + - # theme( - # axis.title.x = element_blank() - # ) + - # ylab( - # unique(df$unit)) - - return(p) -} - -# print(plotPercentiles(ngfs_data_2_scenarios)) -# print(plotPercentiles(ngfs_data_2_scenarios, which = "scenarios")) -# print(plotPercentiles(ngfs_data_2_scenarios, which = "variables")) -# print(plotPercentiles(ngfs_data_2_scenarios, which = "both")) -# try(print(plotPercentiles(ngfs_data_2_scenarios, which = "asd"))) - -# print(plotPercentiles(ngfs_data_2_scenarios, variables = c("AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3"))) -print(plotPercentiles(ngfs_data_2_scenarios, scenarios = c("d_another"))) - -# print(plotPercentiles(ngfs_data_2_scenarios, scenarios = c("d_another", "d_delfrag"))) -# print(plotPercentiles(ngfs_data_2_scenarios)) -# print(plotPercentiles(ngfs_data_2_scenarios, scenarios = c("d_another"), variables = c("AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3"))) -# print(plotPercentiles(ngfs_data_2_scenarios, scenarios = c("d_another"), variables = c("AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3", "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3"))) -# print(plotPercentiles(ngfs_data_2_scenarios, variables = c("AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3", "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3"))) -# print(plotPercentiles(ngfs_data_2_scenarios, scenarios = c("d_another", "d_delfrag"), variables = c("AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3", "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3"))) - -``` - - - -```{r} -filter(df, variable == "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3" & scenario == scenario) -``` - - -## Function tests - -Must use pre-processed `ngfs_data_2_scenarios` - -```{r} -# print(plotPercentiles(ngfs_data_2_scenarios)) -# print(plotPercentiles(ngfs_data_2_scenarios, which = "scenarios")) -# print(plotPercentiles(ngfs_data_2_scenarios, which = "variables")) -# print(plotPercentiles(ngfs_data_2_scenarios, which = "both")) -# try(print(plotPercentiles(ngfs_data_2_scenarios, which = "asd"))) - -print(plotPercentiles(ngfs_data_2_scenarios, scenarios = c("d_another", "d_delfrag"))) -# print(plotPercentiles(ngfs_data_2_scenarios, variables = c("AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3"))) -# print(plotPercentiles(ngfs_data_2_scenarios)) - -``` - -# The End - -```{r} -knitr::knit_exit() -``` - -# Playground - - -```{r} -# print(ngfs_data_2_scenarios) - -which_example = "variables=AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3" -#which_example = "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|33.0th Percentile" -# which_example = "variables=AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3,AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|90.0th Percentile" - -print(stringr::str_split(gsub(pattern = "variables\\=", x = which_example, replacement = ""), pattern = ",")) - - -``` - -## Learning `facet_wrap` - -```{r} -library(ggplot2) -mpg2 <- subset(mpg, cyl != 5 & drv %in% c("4", "f") & class != "2seater") -print(mpg2) - -base <- ggplot(mpg2, aes(displ, hwy)) + - #geom_blank() + # Plots nothing - geom_point() + # Plots points - xlab(NULL) + - ylab(NULL) - -# base + facet_wrap(~class, ncol = 3) -base + facet_wrap("class", ncol = 3) -#base + facet_wrap(var(class), ncol = 3) # Doesnt work -``` - -## Restructuring - -```{r} -pnl <- tibble( - x = 1:4, - a = c(1, 1,0, 0), - b = c(0, 1, 1, 1), - y1 = rnorm(4), - y2 = rnorm(4), - z1 = rep(3, 4), - z2 = rep(-2, 4), -) -print(pnl) - -foo <- pnl %>% pivot_longer( - cols = !c(x, a, b), - names_to = c(".value", "time"), - names_pattern = "(.)(.)" -) -print(foo) -``` - -## Miscellaneous - -```{r} -#endsWith(x = ngfs_meta[, ncol(ngfs_meta)], suffix = "Percentile") -#ngfs_meta[, ncol(ngfs_meta)] %>% select(ends_with("Percentile")) # Does not work -#ngfs_meta[, ncol(ngfs_meta)] %>% filter(grepl("Percentile", ngfs_meta[, ncol(ngfs_meta)])) # Does not work -#ngfs_meta[, ncol(ngfs_meta)] %>% filter(grepl("Percentile", colnames(ngfs_meta)[ncol(ngfs_meta)])) # Does not work -#ngfs_meta[, ncol(ngfs_meta)] %>% filter(grepl("Percentile", "Variable_7")) # Does not work -#grepl("Percentile", colnames(ngfs_meta)[ncol(ngfs_meta)]) # Same as above -#ngfs_meta[, ncol(ngfs_meta)] %>% filter(grepl("Percentile", Variable_7)) # Works -#ngfs_meta %>% filter(grepl("Percentile", Variable_7)) # Works -#ngfs_meta[, ncol(ngfs_meta)] %>% filter(grepl("Percentile", as.name(tail(names(ngfs_meta), 1)))) # Does not work -#ngfs_meta[, ncol(ngfs_meta)] %>% filter(grepl("Percentile", as.symbol(tail(names(ngfs_meta), 1)))) # Does not work -#ngfs_meta[, ncol(ngfs_meta)] %>% filter(grepl("Percentile", as.symbol(tail(names(.), 1)))) # Does not work -#ngfs_meta[, ncol(ngfs_meta)] %>% filter() -#colnames(ngfs_meta)[ncol(ngfs_meta)] -#as.name(tail(names(ngfs_meta), 1)) -#as.symbol(tail(names(ngfs_meta), 1)) -``` - -```{r, fig.width=5, fig.height=5, message = FALSE, warning = FALSE} -#ngfs_meta %>% drop_na(contains("Variable")) -#ngfs_meta %>% filter(last_col() != "95.0th Percentile") -ngfs_meta %>% filter(ngfs_meta[, ncol(ngfs_meta)] == "95.0th Percentile") -``` - -```{r, fig.width=5, fig.height=5, message = FALSE, warning = FALSE} -ngfs_data <- readxl::read_xlsx( - "~/../lab/misc_data/ngfs-magicc-data-from-csv.xlsx", - sheet = "ngfs-magicc-data" -) %>% - select(starts_with(c("Variable", "1", "2"))) %>% - separate_wider_delim("Variable", delim = "|", names_sep = "_", too_few = "align_end") #%>% - #drop_na(x, any_of(vars)) - #separate_wider_delim(cols = c("Variable"), delim="|") - #data.frame(do.call('rbind', strsplit(as.character(ngfs_data$Variable), '|', fixed=TRUE))) %>% - #drop("Variable") - -#within(ngfs_meta, SOMETHING<-data.frame(do.call('rbind', strsplit(as.character(ngfs_meta$Variable), '|', fixed=TRUE)))) - -ngfs_data -``` - -## Check element of vector against other vector - -```{r} -foo <- c("1", "2", "7") -bar <- c("1", "2", "3", "4", "5", "6") - -for (item in foo) { - if (!(item %in% bar)) print(paste(item, "missing", sep = " ")) -} -``` - -```{r} -item %in% foo -``` - -## Concatenate string vectors and constants - -```{r} -a <- 1:10 -print(a) -b <- 11:20 -print(b) -c <- c(a,b) -print(c) -d <- c(a,b, 5) -print(d) - -print("String vectors") - -a <- c("loooooooooooooooooooooooooooooooooooooooooooooooooooooooooong String", "bar") -print(a) -b <- c("baz") -print(b) -c <- c(a,b) -print(c) -d <- c(a,b, ".values") -print(d) -``` - -## Split `Variable` into dedicated columns -```{r, fig.width=5, fig.height=5, message = FALSE, warning = FALSE} -data.frame(do.call('rbind', strsplit(as.character(ngfs_data$Variable), '|', fixed=TRUE))) -``` - -## Basic stuff -```{r} -c("X1", "X2", "X3", "X4", "X5", "X6", "X7") -c("X1", "X2", "X3", "X4", "X5") -``` - -```{r, fig.width=5, fig.height=5, message = FALSE, warning = FALSE} -#ngfs_meta |> separate_wider_delim("Variable", delim = "|", names_sep = "_") -ngfs_meta |> separate_wider_delim("Variable", delim = "|", names_sep = "_", names = c("X1", "X2", "X3", "X4", "X5", "X6", "X7"), too_few = "align_start") -#as.data.frame(ngfs_meta) |> separate_longer_delim("Variable", delim = "|") -#as.data.frame(ngfs_meta) |> separate_wider_delim("Variable", delim = "|", names = c("X1", "X2", "X3", "X4", "X5", "X6", "X7")) -#data.frame(do.call('rbind', strsplit(as.character(ngfs_data$Variable), '|', fixed=TRUE))) -``` - -# Graveyard - -```{r} -these_please <- c( - "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|5.0th Percentile", - "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|33.0th Percentile", - "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|50.0th Percentile", - "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|67.0th Percentile", - "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|95.0th Percentile" -) - -percentile_from_variables <- function(variables) { - return (stringr::str_split_i(variables, pattern = "\\|", i=-1)) -} - -df <- ngfs_data %>% - filter(Variable %in% these_please) %>% - # pivot_longer(starts_with(c("1", "2"))) - # pivot_longer(starts_with(c("1", "2")), names_to = these_please, names_sep = " ", values_to = "year") #%>% - # pivot_longer_spec(starts_with(c("1", "2")), names_to = these_please, names_sep = " ", values_to = "year") #%>% - # pivot_longer_spec(starts_with(c("1", "2")), names_to = c(".value", these_please), names_sep = " ", values_to = "year") #%>% - #pivot_longer(starts_with(c("1", "2")), names_to = c(".value", these_please), names_pattern = "(.)(.)(.)") - pivot_longer(starts_with(c("1", "2")), names_to = "Year", values_to = "Value") %>% - #gather("year", "value", 3:ncol(ngfs_data), convert = TRUE) #%>% - #gather("year", c("value_1", "value_2"), 3:ncol(ngfs_data)) #%>% - #gather("year", "value", 3:ncol(ngfs_data), convert = as.integer) %>% - #mutate_at(vars(c("year")), as.integer) - pivot_wider(names_from = "Variable", values_from = "Value") %>% - rename_with(percentile_from_variables, starts_with("AR6 climate diagnostics")) %>% - mutate_at(vars(c("Year")), as.integer) -df -``` - -```{r, fig.width=5, fig.height=5, message = FALSE, warning = FALSE} -ngfs_data <- readxl::read_xlsx( - "~/../lab/misc_data/ngfs-magicc-data-from-csv-v2.xlsx", - sheet = "ngfs-magicc-data" -) %>% - select(starts_with(c("Variable", "Unit", "1", "2"))) %>% - #filter(grepl("Percentile", Variable)) %>% # Works - filter(grepl("Percentile", Variable)) %>% # Works - #separate_wider_delim("Variable", delim = "|", names_sep = "_", too_few = "align_end") #%>% - #gather("year", "value", 3:ncol(ngfs_meta)) - mutate_at(vars(starts_with(c("1", "2"))), as.double) - -#within(ngfs_meta, SOMETHING<-data.frame(do.call('rbind', strsplit(as.character(ngfs_meta$Variable), '|', fixed=TRUE)))) - -ngfs_data -``` diff --git a/tests_plotPercentiles.Rmd b/tests_plotPercentiles.Rmd deleted file mode 100644 index ef4f443..0000000 --- a/tests_plotPercentiles.Rmd +++ /dev/null @@ -1,152 +0,0 @@ ---- -title: "Set of tests for `plotPercentiles.R`" -output: - html_document: - theme: paper - toc: true - toc_float: - collapsed: false - toc_depth: 2 - code_folding: hide - df_print: kable - html_notebook: - code_folding: hide - pdf_document: default ---- - - -```{r message=FALSE, warning=FALSE} -library(ggplot2) -library(dplyr) -library(tidyr) -library(quitte) -library(readxl) -``` - -# Read data - -```{r} -these_please <- c( - # CO2 concentrations - "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|5.0th Percentile", - "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|33.0th Percentile", - "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|50.0th Percentile", - "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|67.0th Percentile", - "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3|95.0th Percentile", - # NOTE: Theres not 5th Percentile on Surface Temperatures - "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|10.0th Percentile", - "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|33.0th Percentile", - "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|50.0th Percentile", - "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|67.0th Percentile", - "AR6 climate diagnostics|Surface Temperature (GSAT)|MAGICCv7.5.3|90.0th Percentile", - # CH4 Fluxes - "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|5.0th Percentile", - "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|33.0th Percentile", - "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|50.0th Percentile", - "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|67.0th Percentile", - "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3|95.0th Percentile" -) - -ngfs_data <- readxl::read_xlsx( - # "~/../lab/misc_data/ngfs-magicc-data-from-csv-v2.xlsx", - "~/../lab/misc_data/ngfs-magicc-data-from-csv-v3-extended-scenarios.xlsx", - sheet = "ngfs-magicc-data" - ) %>% - # Cast yearly columns as doubles - mutate_at(vars(starts_with(c("1", "2"))), as.double) %>% - # Exclude rows whose "Variable" column does not end in Percentile, because - # these do not contain numerical data in the columns for each year - #filter(grepl("Percentile", Variable)) %>% - # Take only those variables that shall be plotted later on - filter(Variable %in% these_please) %>% - reshape2::melt( - c("Model", "Scenario", "Region", "Variable", "Unit"), - variable.name = "period" - ) %>% - as.quitte() - -print(ngfs_data) -``` - -## Test function - -Source package - -```{r} -# library("plotPercentiles", lib.loc = "C:/Users/tonnru/lab/mip/R") -source("./R/plotPercentiles.R") -``` - -Check out different scenarios/variables - -```{r} -print(unique(ngfs_data$scenario)) -print(unique(ngfs_data$variable)) -``` -### Sanity checks - -Function is supposed to fail here - -```{r} -try(print(plotPercentiles(ngfs_data, scenarios = "blerk"))) -try(print(plotPercentiles(ngfs_data, variables = "blork"))) -``` -### One variable, all scenarios - -```{r} -print(plotPercentiles(ngfs_data, variables = c("AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3"))) -# print(plotPercentiles(ngfs_data, scenarios = c("d_another", "d_delfrag"))) -# print(plotPercentiles(ngfs_data)) -``` -### Single variable, single scenario - -```{r} -print(plotPercentiles(ngfs_data, variables = c("AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3"), scenarios = c("d_delfrag"))) -``` -### Multiple variables, single scenario - -```{r} -print(plotPercentiles( - ngfs_data, - variables = c( - "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3", - "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3" - ), - scenarios = c("d_delfrag")) -) -``` - -### Multiple variables, single scenario - -```{r} -print(plotPercentiles(ngfs_data, scenarios = c("d_delfrag"))) -``` - -### Multiple variables, multiple scenarios - -```{r} -print(plotPercentiles(ngfs_data, scenarios = c("d_another", "d_delfrag"))) -``` - - -### Multiple variables, multiple scenarios - -```{r} -print(plotPercentiles( - ngfs_data, - variables = c( - "AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3", - "AR6 climate diagnostics|Net Land to Atmosphere Flux due to Permafrost|CH4|MAGICCv7.5.3" - ), - scenarios = c( - "d_another", - "d_delfrag" - ) -)) -``` - -### All variables, all scenarios (default call) - -```{r} -print(plotPercentiles(ngfs_data)) -``` From e8c30a338b6dbb3b6df91dc63e015855f28bab58 Mon Sep 17 00:00:00 2001 From: "PIKACCOUNTS\\tonnru" Date: Fri, 5 Jan 2024 14:51:36 +0100 Subject: [PATCH 13/24] Added importFrom pivot_wider due to linter complaints --- R/plotPercentiles.R | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/R/plotPercentiles.R b/R/plotPercentiles.R index bf6c56a..6a6d362 100644 --- a/R/plotPercentiles.R +++ b/R/plotPercentiles.R @@ -5,9 +5,11 @@ #' @param df `quitte` style data frame containing all variables for each scenario #' @param scenarios Character vector contains names of the desired scenarios. If none, all scenarios will be displayed #' @param variables Character vector contains names of the desired variables. If none, all variables will be displayed -#' @importFrom dplyr filter mutate +#' @importFrom dplyr filter mutate vars #' @importFrom reshape2 melt #' @importFrom stringr str_extract +#' @importFrom tidyr pivot_wider +#' @importFrom ggplot2 ggplot geom_line geom_ribbon facet_wrap facet_grid theme ylab #' @export plotPercentiles <- function(df, scenarios = NULL, variables = NULL) { From 82bfcc54cc5cb93cb6ab5a82c89f7a278fcfa7fc Mon Sep 17 00:00:00 2001 From: "PIKACCOUNTS\\tonnru" Date: Fri, 5 Jan 2024 14:56:18 +0100 Subject: [PATCH 14/24] Changes from build process --- CITATION.cff | 8 +++++--- DESCRIPTION | 2 +- NAMESPACE | 5 +++++ README.md | 8 ++++---- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/CITATION.cff b/CITATION.cff index 5ba6247..7e5f202 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -2,8 +2,8 @@ cff-version: 1.2.0 message: If you use this software, please cite it using the metadata from this file. type: software title: 'mip: Comparison of multi-model runs' -version: 0.147.0 -date-released: '2023-12-04' +version: 0.148.0 +date-released: '2024-01-05' abstract: Package contains generic functions to produce comparison plots of multi-model runs. authors: @@ -30,7 +30,9 @@ authors: email: pascal.fuehrlich@pik-potsdam.de - family-names: Richters given-names: Oliver +- family-names: Rüter + given-names: Tonn + email: tonn.rueter@pik-potsdam.de license: BSD-2-Clause repository-code: https://github.com/pik-piam/mip doi: 10.5281/zenodo.1158586 - diff --git a/DESCRIPTION b/DESCRIPTION index 0ab5a90..4e1d52a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -12,7 +12,7 @@ Authors@R: c( person("Stephen", "Wirth", , "wirth@pik-potsdam.de", role = "aut"), person("Pascal", "Führlich", , "pascal.fuehrlich@pik-potsdam.de", role = "aut"), person("Oliver", "Richters", role = "aut"), - person("Tonn", "Rueter", role = "aut") + person("Tonn", "Rüter", role = "aut") ) Description: Package contains generic functions to produce comparison plots of multi-model runs. diff --git a/NAMESPACE b/NAMESPACE index bb59d9c..cd0acc8 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -11,6 +11,7 @@ export(mipArea) export(mipBarYearData) export(mipIterations) export(mipLineHistorical) +export(plotPercentiles) export(plotstyle) export(plotstyle.add) export(scenTool) @@ -52,6 +53,7 @@ importFrom(dplyr,summarise) importFrom(dplyr,summarize) importFrom(dplyr,sym) importFrom(dplyr,ungroup) +importFrom(dplyr,vars) importFrom(ggplot2,"%+replace%") importFrom(ggplot2,aes) importFrom(ggplot2,aes_) @@ -72,6 +74,7 @@ importFrom(ggplot2,geom_col) importFrom(ggplot2,geom_hline) importFrom(ggplot2,geom_line) importFrom(ggplot2,geom_point) +importFrom(ggplot2,geom_ribbon) importFrom(ggplot2,geom_text) importFrom(ggplot2,geom_vline) importFrom(ggplot2,ggplot) @@ -191,8 +194,10 @@ importFrom(stats,median) importFrom(stats,na.omit) importFrom(stats,reshape) importFrom(stats,runif) +importFrom(stringr,str_extract) importFrom(tidyr,crossing) importFrom(tidyr,drop_na) +importFrom(tidyr,pivot_wider) importFrom(tidyr,unite) importFrom(tools,file_ext) importFrom(tools,file_path_sans_ext) diff --git a/README.md b/README.md index a6d923f..1aa15b4 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Comparison of multi-model runs -R package **mip**, version **0.147.0** +R package **mip**, version **0.148.0** [![CRAN status](https://www.r-pkg.org/badges/version/mip)](https://cran.r-project.org/package=mip) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.1158586.svg)](https://doi.org/10.5281/zenodo.1158586) [![R build status](https://github.com/pik-piam/mip/workflows/check/badge.svg)](https://github.com/pik-piam/mip/actions) [![codecov](https://codecov.io/gh/pik-piam/mip/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/mip) [![r-universe](https://pik-piam.r-universe.dev/badges/mip)](https://pik-piam.r-universe.dev/builds) @@ -47,7 +47,7 @@ In case of questions / problems please contact David Klein , R package version 0.147.0, . +Klein D, Dietrich J, Baumstark L, Humpenoeder F, Stevanovic M, Wirth S, Führlich P, Richters O (2024). _mip: Comparison of multi-model runs_. doi:10.5281/zenodo.1158586 , R package version 0.148.0, . A BibTeX entry for LaTeX users is @@ -55,8 +55,8 @@ A BibTeX entry for LaTeX users is @Manual{, title = {mip: Comparison of multi-model runs}, author = {David Klein and Jan Philipp Dietrich and Lavinia Baumstark and Florian Humpenoeder and Miodrag Stevanovic and Stephen Wirth and Pascal Führlich and Oliver Richters}, - year = {2023}, - note = {R package version 0.147.0}, + year = {2024}, + note = {R package version 0.148.0}, doi = {10.5281/zenodo.1158586}, url = {https://github.com/pik-piam/mip}, } From 7d66a3ce1804480096603df1951f6975f0bae455 Mon Sep 17 00:00:00 2001 From: "PIKACCOUNTS\\tonnru" Date: Fri, 5 Jan 2024 16:04:45 +0100 Subject: [PATCH 15/24] Re-ran build for github --- CITATION.cff | 1 + DESCRIPTION | 2 +- README.md | 4 ++-- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/CITATION.cff b/CITATION.cff index 7e5f202..19a3327 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -36,3 +36,4 @@ authors: license: BSD-2-Clause repository-code: https://github.com/pik-piam/mip doi: 10.5281/zenodo.1158586 + diff --git a/DESCRIPTION b/DESCRIPTION index 4e1d52a..d2fb468 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -12,7 +12,7 @@ Authors@R: c( person("Stephen", "Wirth", , "wirth@pik-potsdam.de", role = "aut"), person("Pascal", "Führlich", , "pascal.fuehrlich@pik-potsdam.de", role = "aut"), person("Oliver", "Richters", role = "aut"), - person("Tonn", "Rüter", role = "aut") + person("Tonn", "Rüter", , "tonn.rueter@pik-potsdam.de", role = "aut") ) Description: Package contains generic functions to produce comparison plots of multi-model runs. diff --git a/README.md b/README.md index 1aa15b4..a5200bb 100644 --- a/README.md +++ b/README.md @@ -47,14 +47,14 @@ In case of questions / problems please contact David Klein , R package version 0.148.0, . +Klein D, Dietrich J, Baumstark L, Humpenoeder F, Stevanovic M, Wirth S, Führlich P, Richters O, Rüter T (2024). _mip: Comparison of multi-model runs_. doi:10.5281/zenodo.1158586 , R package version 0.148.0, . A BibTeX entry for LaTeX users is ```latex @Manual{, title = {mip: Comparison of multi-model runs}, - author = {David Klein and Jan Philipp Dietrich and Lavinia Baumstark and Florian Humpenoeder and Miodrag Stevanovic and Stephen Wirth and Pascal Führlich and Oliver Richters}, + author = {David Klein and Jan Philipp Dietrich and Lavinia Baumstark and Florian Humpenoeder and Miodrag Stevanovic and Stephen Wirth and Pascal Führlich and Oliver Richters and Tonn Rüter}, year = {2024}, note = {R package version 0.148.0}, doi = {10.5281/zenodo.1158586}, From 65638fadd45e3bac10a11b982e53b2aee5b92f22 Mon Sep 17 00:00:00 2001 From: "PIKACCOUNTS\\tonnru" Date: Fri, 5 Jan 2024 16:25:18 +0100 Subject: [PATCH 16/24] Make github workflow happy --- .buildlibrary | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.buildlibrary b/.buildlibrary index ad4077b..e9a33a2 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '28951650' +ValidationKey: '29195960' AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' - 'Warning: namespace ''.*'' is not available and has been replaced' From 48bc69159e1d108ed049f0fe312cf729912309b2 Mon Sep 17 00:00:00 2001 From: "PIKACCOUNTS\\tonnru" Date: Mon, 8 Jan 2024 11:07:26 +0100 Subject: [PATCH 17/24] Man page for plotPercentile function --- man/plotPercentiles.Rd | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 man/plotPercentiles.Rd diff --git a/man/plotPercentiles.Rd b/man/plotPercentiles.Rd new file mode 100644 index 0000000..b6dd4d4 --- /dev/null +++ b/man/plotPercentiles.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/plotPercentiles.R +\name{plotPercentiles} +\alias{plotPercentiles} +\title{Comparison plots show user selected variables obtained from different scenario +runs} +\usage{ +plotPercentiles(df, scenarios = NULL, variables = NULL) +} +\arguments{ +\item{df}{`quitte` style data frame containing all variables for each scenario} + +\item{scenarios}{Character vector contains names of the desired scenarios. If none, all scenarios will be displayed} + +\item{variables}{Character vector contains names of the desired variables. If none, all variables will be displayed} +} +\description{ +Comparison plots show user selected variables obtained from different scenario +runs +} +\author{ +Tonn Rueter +} From e3d3017d887b16ac841917f6d00db0533bb35783 Mon Sep 17 00:00:00 2001 From: "PIKACCOUNTS\\tonnru" Date: Mon, 8 Jan 2024 14:18:31 +0100 Subject: [PATCH 18/24] Extended function documentation --- R/plotPercentiles.R | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/R/plotPercentiles.R b/R/plotPercentiles.R index 6a6d362..c4754e9 100644 --- a/R/plotPercentiles.R +++ b/R/plotPercentiles.R @@ -1,10 +1,16 @@ -#' Comparison plots show user selected variables obtained from different scenario -#' runs +#' Comparison plots show 50th percentile of user selected variables as obtained from different scenario runs. If +#' available in the data, ribbon plots will also show the 33th - 67th percentile in a darker color and the 5th – 95th +#' percentile in a lighter color. *Note*: the 5th, 33th, 67th and 95th percentile must be given in the provided data set +#' as the percentiles are not computed #' @author Tonn Rueter -#' @param df `quitte` style data frame containing all variables for each scenario +#' @param df `quitte` style data frame containing all variables for each scenario. In the quitte data frame all +#' percentiles must be given as individual variables. Manipulate input data frame such that all percentiles +#' of a given quantity are transformed to individual columns. #' @param scenarios Character vector contains names of the desired scenarios. If none, all scenarios will be displayed #' @param variables Character vector contains names of the desired variables. If none, all variables will be displayed +#' Variable names in the quitte data frame need to follow the format "Any|Variable|50.0th Percentile". When +#' selecting variables for display only use the "Any|Variable"-prefix and omit the "X-th Percentile"-suffix #' @importFrom dplyr filter mutate vars #' @importFrom reshape2 melt #' @importFrom stringr str_extract @@ -13,7 +19,7 @@ #' @export plotPercentiles <- function(df, scenarios = NULL, variables = NULL) { - # In the quitte data frame all perenctiles are given as individual variables + # In the quitte data frame all percentiles are given as individual variables # Manipulate input data frame such that all percentiles of a given quantity # are transformed to individual columns. Variable names in the quitte data # frame follow the format "Any|Variable|5.0th Percentile". The regular From b710b9edba491a98641275d763078274a7d631e6 Mon Sep 17 00:00:00 2001 From: "PIKACCOUNTS\\tonnru" Date: Mon, 8 Jan 2024 15:23:31 +0100 Subject: [PATCH 19/24] Clarified if-else-block --- R/plotPercentiles.R | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/R/plotPercentiles.R b/R/plotPercentiles.R index c4754e9..823a6ce 100644 --- a/R/plotPercentiles.R +++ b/R/plotPercentiles.R @@ -35,25 +35,30 @@ plotPercentiles <- function(df, scenarios = NULL, variables = NULL) { values_from = "value" ) - # Check which scenarios/variabes are available + # Check which scenarios/variables are available uniqueScenarios <- unique(data$scenario) uniqueVariables <- unique(data$variable) - # Check which function parameters have been provided and default to unique - # values from the data frame in case none have - theseScenarios <- if (is.null(scenarios)) { - uniqueScenarios - } else if (allItemsAvailable(scenarios, uniqueScenarios, warn = TRUE)) { - scenarios + # Check which function parameters have been provided and default to unique values from the data frame in case none + # have. If scenarios/variables have been provided by user, check whether they are available in the data + if (!is.null(scenarios)) { + diffScenarios <- setdiff(scenarios, uniqueScenarios) + if (length(diffScenarios) > 0) { + stop(paste0("Missing scenarios: ", paste0(setdiff(scenarios, uniqueScenarios), collapse = ", "), "\n")) + } + theseScenarios <- scenarios } else { - stop("Provided scenario is missing in data") + theseScenarios <- uniqueScenarios } - theseVariables <- if (is.null(variables)) { - uniqueVariables - } else if (allItemsAvailable(variables, uniqueVariables, warn = TRUE)) { - variables + + if (!is.null(variables)) { + diffVariables <- setdiff(variables, uniqueVariables) + if (length(diffVariables) > 0) { + stop(paste0("Missing variables: ", paste0(diffVariables, collapse = ", "), "\n")) + } + theseVariables <- variables } else { - stop("Provided variable is missing in data") + theseVariables <- uniqueVariables } # Set up the plot @@ -101,13 +106,3 @@ plotPercentiles <- function(df, scenarios = NULL, variables = NULL) { return(p) } - -allItemsAvailable <- function(selection, available, warn = FALSE) { - for (item in selection) { - if (!(item %in% available)) { - if (warn) warning(paste0("'", item, "' missing in available data")) - return(FALSE) - } - } - return(TRUE) -} From 1d5381c7aed2ea86fdc96b1237c0f62f775c086e Mon Sep 17 00:00:00 2001 From: "PIKACCOUNTS\\tonnru" Date: Mon, 8 Jan 2024 15:28:38 +0100 Subject: [PATCH 20/24] Re-build after documentation update --- .buildlibrary | 2 +- CITATION.cff | 2 +- DESCRIPTION | 2 +- man/plotPercentiles.Rd | 20 ++++++++++++++------ 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/.buildlibrary b/.buildlibrary index e9a33a2..687d5c6 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '29195960' +ValidationKey: '29200400' AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' - 'Warning: namespace ''.*'' is not available and has been replaced' diff --git a/CITATION.cff b/CITATION.cff index 19a3327..6362228 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -3,7 +3,7 @@ message: If you use this software, please cite it using the metadata from this f type: software title: 'mip: Comparison of multi-model runs' version: 0.148.0 -date-released: '2024-01-05' +date-released: '2024-01-08' abstract: Package contains generic functions to produce comparison plots of multi-model runs. authors: diff --git a/DESCRIPTION b/DESCRIPTION index d2fb468..5aac5e6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,7 +2,7 @@ Type: Package Package: mip Title: Comparison of multi-model runs Version: 0.148.0 -Date: 2024-01-05 +Date: 2024-01-08 Authors@R: c( person("David", "Klein", , "dklein@pik-potsdam.de", role = c("aut", "cre")), person("Jan Philipp", "Dietrich", , "dietrich@pik-potsdam.de", role = "aut"), diff --git a/man/plotPercentiles.Rd b/man/plotPercentiles.Rd index b6dd4d4..22a365c 100644 --- a/man/plotPercentiles.Rd +++ b/man/plotPercentiles.Rd @@ -2,21 +2,29 @@ % Please edit documentation in R/plotPercentiles.R \name{plotPercentiles} \alias{plotPercentiles} -\title{Comparison plots show user selected variables obtained from different scenario -runs} +\title{Comparison plots show 50th percentile of user selected variables as obtained from different scenario runs. If +available in the data, ribbon plots will also show the 33th - 67th percentile in a darker color and the 5th – 95th +percentile in a lighter color. *Note*: the 5th, 33th, 67th and 95th percentile must be given in the provided data set +as the percentiles are not computed} \usage{ plotPercentiles(df, scenarios = NULL, variables = NULL) } \arguments{ -\item{df}{`quitte` style data frame containing all variables for each scenario} +\item{df}{`quitte` style data frame containing all variables for each scenario. In the quitte data frame all +percentiles must be given as individual variables. Manipulate input data frame such that all percentiles +of a given quantity are transformed to individual columns.} \item{scenarios}{Character vector contains names of the desired scenarios. If none, all scenarios will be displayed} -\item{variables}{Character vector contains names of the desired variables. If none, all variables will be displayed} +\item{variables}{Character vector contains names of the desired variables. If none, all variables will be displayed +Variable names in the quitte data frame need to follow the format "Any|Variable|50.0th Percentile". When +selecting variables for display only use the "Any|Variable"-prefix and omit the "X-th Percentile"-suffix} } \description{ -Comparison plots show user selected variables obtained from different scenario -runs +Comparison plots show 50th percentile of user selected variables as obtained from different scenario runs. If +available in the data, ribbon plots will also show the 33th - 67th percentile in a darker color and the 5th – 95th +percentile in a lighter color. *Note*: the 5th, 33th, 67th and 95th percentile must be given in the provided data set +as the percentiles are not computed } \author{ Tonn Rueter From 5ccb6e5ee061d044a0e57462c1d5ac3f117bde57 Mon Sep 17 00:00:00 2001 From: "PIKACCOUNTS\\tonnru" Date: Tue, 9 Jan 2024 14:50:48 +0100 Subject: [PATCH 21/24] example plot for plotPercentiles docs --- man/figures/plotPercentiles1.png | Bin 0 -> 10448 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 man/figures/plotPercentiles1.png diff --git a/man/figures/plotPercentiles1.png b/man/figures/plotPercentiles1.png new file mode 100644 index 0000000000000000000000000000000000000000..b4f62155cc5ee2ed50c9a1cb2645d8c792621247 GIT binary patch literal 10448 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 Date: Tue, 9 Jan 2024 14:52:23 +0100 Subject: [PATCH 22/24] Extended plotPercentiles docs with usage example and plot --- .buildlibrary | 2 +- CITATION.cff | 2 +- DESCRIPTION | 2 +- R/plotPercentiles.R | 52 +++++++++++++++++++++++++++--------------- man/plotPercentiles.Rd | 47 +++++++++++++++++++++++++------------- 5 files changed, 67 insertions(+), 38 deletions(-) diff --git a/.buildlibrary b/.buildlibrary index 687d5c6..f194631 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '29200400' +ValidationKey: '29201880' AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' - 'Warning: namespace ''.*'' is not available and has been replaced' diff --git a/CITATION.cff b/CITATION.cff index 6362228..7ce4221 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -3,7 +3,7 @@ message: If you use this software, please cite it using the metadata from this f type: software title: 'mip: Comparison of multi-model runs' version: 0.148.0 -date-released: '2024-01-08' +date-released: '2024-01-09' abstract: Package contains generic functions to produce comparison plots of multi-model runs. authors: diff --git a/DESCRIPTION b/DESCRIPTION index 5aac5e6..86adcc0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,7 +2,7 @@ Type: Package Package: mip Title: Comparison of multi-model runs Version: 0.148.0 -Date: 2024-01-08 +Date: 2024-01-09 Authors@R: c( person("David", "Klein", , "dklein@pik-potsdam.de", role = c("aut", "cre")), person("Jan Philipp", "Dietrich", , "dietrich@pik-potsdam.de", role = "aut"), diff --git a/R/plotPercentiles.R b/R/plotPercentiles.R index 823a6ce..58c1998 100644 --- a/R/plotPercentiles.R +++ b/R/plotPercentiles.R @@ -1,16 +1,32 @@ -#' Comparison plots show 50th percentile of user selected variables as obtained from different scenario runs. If -#' available in the data, ribbon plots will also show the 33th - 67th percentile in a darker color and the 5th – 95th -#' percentile in a lighter color. *Note*: the 5th, 33th, 67th and 95th percentile must be given in the provided data set -#' as the percentiles are not computed - -#' @author Tonn Rueter -#' @param df `quitte` style data frame containing all variables for each scenario. In the quitte data frame all -#' percentiles must be given as individual variables. Manipulate input data frame such that all percentiles -#' of a given quantity are transformed to individual columns. -#' @param scenarios Character vector contains names of the desired scenarios. If none, all scenarios will be displayed -#' @param variables Character vector contains names of the desired variables. If none, all variables will be displayed -#' Variable names in the quitte data frame need to follow the format "Any|Variable|50.0th Percentile". When -#' selecting variables for display only use the "Any|Variable"-prefix and omit the "X-th Percentile"-suffix +#' Comparison line plots with percentiles +#' +#' Line plots show median (50th percentile) of user selected variable(s) obtained from different scenario runs. If +#' available in the data, ribbon plots will also show the 33th - 67th percentile region in a darker color and the +#' 5th – 95th percentile region in a lighter color. Note: the 5th, 33th, 67th and 95th percentiles must be provided in +#' the data set as the percentiles are not computed +#' +#' @author Tonn Rüter +#' @param df The \code{quitte}-style data frame must contain all percentiles of the quantity of interest as individual +#' variables (e.g. for atmospheric CO2 concentrations "Atmospheric Concentrations|CO2|50th Percentile", +#' "Atmospheric Concentrations|CO2|33th Percentile", ..., must be present) +#' @param scenarios Character vector containing names of the desired scenarios. If \code{NULL}, all scenarios present in +#' the data will be displayed +#' @param variables Character vector containing names of the desired variables. If \code{NULL}, all variables present in +#' the data will be displayed. When selecting particular variables for display only use the "Any|Variable"-prefix and +#' omit the "X-th Percentile"-suffix (e.g. for atmospheric CO2 concentrations write "Atmospheric Concentrations|CO2") +#' @examples +#' \dontrun{ +#' # Plot atmospheric CO2 concentrations for all scenarios available in the data +#' p <- plotPercentiles( +#' data, +#' # Use variable name without "X-th Percentile"-suffix +#' variables = c("AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3") +#' ) +#' # Plot all available variables for selected scenarios +#' p <- plotPercentiles(data, scenarios = c("d_delfrag", "d_another")) +#' } +#' @section Example Plot: +#' \if{html}{\figure{plotPercentiles1.png}{Atmospheric CO2 concentrations for all scenarios available in the data}} #' @importFrom dplyr filter mutate vars #' @importFrom reshape2 melt #' @importFrom stringr str_extract @@ -19,12 +35,10 @@ #' @export plotPercentiles <- function(df, scenarios = NULL, variables = NULL) { - # In the quitte data frame all percentiles are given as individual variables - # Manipulate input data frame such that all percentiles of a given quantity - # are transformed to individual columns. Variable names in the quitte data - # frame follow the format "Any|Variable|5.0th Percentile". The regular - # expressions below divide the variable name into the prefix and the - # percentile specifier + # In the quitte data frame all percentiles are given as individual variables. Manipulate input data frame such that + # all percentiles of a given quantity are transformed to individual columns. Variable names in the quitte data frame + # follow the format "Any|Variable|5.0th Percentile". The regular expressions below divide the variable name into the + # prefix and the percentile specifier data <- df %>% mutate( "percentile" = stringr::str_extract(.data$variable, "[^\\|]+?$"), diff --git a/man/plotPercentiles.Rd b/man/plotPercentiles.Rd index 22a365c..c6fa508 100644 --- a/man/plotPercentiles.Rd +++ b/man/plotPercentiles.Rd @@ -2,30 +2,45 @@ % Please edit documentation in R/plotPercentiles.R \name{plotPercentiles} \alias{plotPercentiles} -\title{Comparison plots show 50th percentile of user selected variables as obtained from different scenario runs. If -available in the data, ribbon plots will also show the 33th - 67th percentile in a darker color and the 5th – 95th -percentile in a lighter color. *Note*: the 5th, 33th, 67th and 95th percentile must be given in the provided data set -as the percentiles are not computed} +\title{Comparison line plots with percentiles} \usage{ plotPercentiles(df, scenarios = NULL, variables = NULL) } \arguments{ -\item{df}{`quitte` style data frame containing all variables for each scenario. In the quitte data frame all -percentiles must be given as individual variables. Manipulate input data frame such that all percentiles -of a given quantity are transformed to individual columns.} +\item{df}{The \code{quitte}-style data frame must contain all percentiles of the quantity of interest as individual +variables (e.g. for atmospheric CO2 concentrations "Atmospheric Concentrations|CO2|50th Percentile", +"Atmospheric Concentrations|CO2|33th Percentile", ..., must be present)} -\item{scenarios}{Character vector contains names of the desired scenarios. If none, all scenarios will be displayed} +\item{scenarios}{Character vector containing names of the desired scenarios. If \code{NULL}, all scenarios present in +the data will be displayed} -\item{variables}{Character vector contains names of the desired variables. If none, all variables will be displayed -Variable names in the quitte data frame need to follow the format "Any|Variable|50.0th Percentile". When -selecting variables for display only use the "Any|Variable"-prefix and omit the "X-th Percentile"-suffix} +\item{variables}{Character vector containing names of the desired variables. If \code{NULL}, all variables present in +the data will be displayed. When selecting particular variables for display only use the "Any|Variable"-prefix and +omit the "X-th Percentile"-suffix (e.g. for atmospheric CO2 concentrations write "Atmospheric Concentrations|CO2")} } \description{ -Comparison plots show 50th percentile of user selected variables as obtained from different scenario runs. If -available in the data, ribbon plots will also show the 33th - 67th percentile in a darker color and the 5th – 95th -percentile in a lighter color. *Note*: the 5th, 33th, 67th and 95th percentile must be given in the provided data set -as the percentiles are not computed +Line plots show median (50th percentile) of user selected variable(s) obtained from different scenario runs. If +available in the data, ribbon plots will also show the 33th - 67th percentile region in a darker color and the +5th – 95th percentile region in a lighter color. Note: the 5th, 33th, 67th and 95th percentiles must be provided in +the data set as the percentiles are not computed +} +\section{Example Plot}{ + +\if{html}{\figure{plotPercentiles1.png}{Atmospheric CO2 concentrations for all scenarios available in the data}} +} + +\examples{ +\dontrun{ +# Plot atmospheric CO2 concentrations for all scenarios available in the data +p <- plotPercentiles( + data, + # Use variable name without "X-th Percentile"-suffix + variables = c("AR6 climate diagnostics|Atmospheric Concentrations|CO2|MAGICCv7.5.3") +) +# Plot all available variables for selected scenarios +p <- plotPercentiles(data, scenarios = c("d_delfrag", "d_another")) +} } \author{ -Tonn Rueter +Tonn Rüter } From e30fffda31bb1fc8b892911842225c6f8b9c6a8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tonn=20R=C3=BCter?= Date: Wed, 28 Feb 2024 11:26:18 +0100 Subject: [PATCH 23/24] Ran buildLibrary --- .buildlibrary | 3 ++- .github/workflows/check.yaml | 5 +++++ CITATION.cff | 4 ++-- DESCRIPTION | 4 ++-- README.md | 8 ++++---- man/mip-package.Rd | 1 + 6 files changed, 16 insertions(+), 9 deletions(-) diff --git a/.buildlibrary b/.buildlibrary index e79500a..344012f 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '29335712' +ValidationKey: '29374785' AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' - 'Warning: namespace ''.*'' is not available and has been replaced' @@ -11,3 +11,4 @@ AcceptedNotes: - checking installed package size AutocreateReadme: yes allowLinterWarnings: yes +enforceVersionUpdate: no diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index 870f216..46f518a 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -49,6 +49,11 @@ jobs: shell: Rscript {0} run: lucode2:::validkey(stopIfInvalid = TRUE) + - name: Verify that lucode2::buildLibrary was successful + if: github.event_name == 'pull_request' + shell: Rscript {0} + run: lucode2:::isVersionUpdated() + - name: Checks shell: Rscript {0} run: | diff --git a/CITATION.cff b/CITATION.cff index a5f5012..0fc8414 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -2,8 +2,8 @@ cff-version: 1.2.0 message: If you use this software, please cite it using the metadata from this file. type: software title: 'mip: Comparison of multi-model runs' -version: 0.148.4 -date-released: '2024-02-15' +version: 0.148.5 +date-released: '2024-02-28' abstract: Package contains generic functions to produce comparison plots of multi-model runs. authors: diff --git a/DESCRIPTION b/DESCRIPTION index 86609c6..d1ef215 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Type: Package Package: mip Title: Comparison of multi-model runs -Version: 0.148.4 -Date: 2024-02-15 +Version: 0.148.5 +Date: 2024-02-28 Authors@R: c( person("David", "Klein", , "dklein@pik-potsdam.de", role = c("aut", "cre")), person("Jan Philipp", "Dietrich", , "dietrich@pik-potsdam.de", role = "aut"), diff --git a/README.md b/README.md index 42cdd02..73f119e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Comparison of multi-model runs -R package **mip**, version **0.148.4** +R package **mip**, version **0.148.5** [![CRAN status](https://www.r-pkg.org/badges/version/mip)](https://cran.r-project.org/package=mip) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.1158586.svg)](https://doi.org/10.5281/zenodo.1158586) [![R build status](https://github.com/pik-piam/mip/workflows/check/badge.svg)](https://github.com/pik-piam/mip/actions) [![codecov](https://codecov.io/gh/pik-piam/mip/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/mip) [![r-universe](https://pik-piam.r-universe.dev/badges/mip)](https://pik-piam.r-universe.dev/builds) @@ -47,16 +47,16 @@ In case of questions / problems please contact David Klein , R package version 0.148.4, . +Klein D, Dietrich J, Baumstark L, Humpenoeder F, Stevanovic M, Wirth S, Führlich P, Richters O, Rüter T (2024). _mip: Comparison of multi-model runs_. doi:10.5281/zenodo.1158586 , R package version 0.148.5, . A BibTeX entry for LaTeX users is ```latex @Manual{, title = {mip: Comparison of multi-model runs}, - author = {David Klein and Jan Philipp Dietrich and Lavinia Baumstark and Florian Humpenoeder and Miodrag Stevanovic and Stephen Wirth and Pascal Führlich and Oliver Richters}, + author = {David Klein and Jan Philipp Dietrich and Lavinia Baumstark and Florian Humpenoeder and Miodrag Stevanovic and Stephen Wirth and Pascal Führlich and Oliver Richters and Tonn Rüter}, year = {2024}, - note = {R package version 0.148.4}, + note = {R package version 0.148.5}, doi = {10.5281/zenodo.1158586}, url = {https://github.com/pik-piam/mip}, } diff --git a/man/mip-package.Rd b/man/mip-package.Rd index 98e5439..6ac9051 100644 --- a/man/mip-package.Rd +++ b/man/mip-package.Rd @@ -3,6 +3,7 @@ \docType{package} \name{mip-package} \alias{mip-package} +\alias{_PACKAGE} \alias{mip} \title{The MIP R package} \description{ From 753a0f865336e997dd56da66081afaad0b05a810 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tonn=20R=C3=BCter?= Date: Wed, 28 Feb 2024 12:31:25 +0100 Subject: [PATCH 24/24] legend.title.align is deprecated --- R/mipLineHistorical.R | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/R/mipLineHistorical.R b/R/mipLineHistorical.R index 3369bf0..b99874f 100644 --- a/R/mipLineHistorical.R +++ b/R/mipLineHistorical.R @@ -239,8 +239,7 @@ mipLineHistorical <- function(x,x_hist=NULL,color.dim="identifier",linetype.dim= axis.text.x=element_text(size=text_size, angle=90, hjust=.5, colour="black"), legend.position="bottom", legend.direction = "horizontal", - legend.title=element_text(size=text_size,face="bold"), - legend.title.align=0, + legend.title=element_text(size=text_size,face="bold",hjust=0), legend.text=element_text(size=text_size-2), #legend.background=element_rect(fill="white"), legend.key=element_blank(),