scens_plot_: Plot scenario and variable comparison figures

Description Usage Arguments Details Value Examples

Description

scens_plot_boxplot() is similar to scens_plot_range() and scens_plot_cloud(), except it uses boxplots to show the range for each year. The boxplot are based on CRSSIO::stat_boxplot_custom(), so whiskers extend to 5th and 95th percentiles, and outliers are shown for points beyond those percentiles.

scens_plot_cloud() plots the range of results for multiple scenarios. The range is shown as a shaded region (cloud) extending from the 10th to 90th percentiles, along with a solid line for the median. Typically this is done for only one variable, but multiple variables can be provided and will be shown as separate facets.

Additionally, scens_plot_cloud() can use the "fill_label" option.

Plotting functions that are designed to largely compare across scenarios are the scens_plot_*() family, while those designed to compare across variables are the vars_plot_*() family. However, both families will facet across the other dimention, e.g., scens_plot_*() will facet by variable.

scens_plot_probs() plots probability plots, i.e., the chance of a variable occurring. Different scenarios are shown as different colors, and if there are different variables (vars) they are shown as different facets.

scen_plot_range() plots the range of results for multiple scenarios. The range is shown as lines for 10th, 50th, and 90th percentiles. Typically this is done for only one variable, but multiple variables can be provided and will be shown as separate facets.

var_plot_trace_scatter() creates a trace number vs. value scatter plot for a single year and variable. If multiple scenarios are specified the scenarios are shown as different facets.

vars_plot_heatmap() creates a heatmap based on the percentage of traces that fall into each bin, i.e., variable. The heatmap is years on the x axis and variables on the y axis, with the color representing the percent of traces in a given variable. Works with the title, subtitle, caption, y_lab, color_label, legend_wrap, facet_scales, facet_nrow, and facet_ncol plot options.

vars_plot_probs() plots the probabilities for multiple variables on a a single plot. Different vars are shown as different colors, and if there are multiple scenarios they are shown as different facets. The variables' probabilities can be shown as lines (plot_type = "line") or as stacked bars (plot_type = "stacked bar"). For stacked bars, care should be taken by user to ensure summing probabilities makes sense.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
scens_plot_boxplot(
  df,
  vars,
  years = NULL,
  scenarios = NULL,
  plot_colors = NULL,
  scen_labels = NULL,
  ...
)

scens_plot_cloud(
  df,
  vars,
  historical = NULL,
  years = NULL,
  scenarios = NULL,
  plot_colors = NULL,
  scen_labels = NULL,
  connect_historical = TRUE,
  ...
)

scens_plot_probs(
  df,
  vars,
  years = NULL,
  scenarios = NULL,
  plot_colors = NULL,
  scen_labels = NULL,
  ...
)

scens_plot_range(
  df,
  vars,
  years = NULL,
  scenarios = NULL,
  plot_colors = NULL,
  scen_labels = NULL,
  ...
)

var_plot_trace_scatter(
  df,
  scenarios,
  years,
  vars,
  color_by = NULL,
  plot_colors = NULL,
  ...
)

vars_plot_heatmap(
  df,
  scenarios,
  years = NULL,
  vars = NULL,
  var_labels = NULL,
  ...
)

vars_plot_probs(
  df,
  scenarios,
  years = NULL,
  vars = NULL,
  plot_colors = NULL,
  var_labels = NULL,
  plot_type = "line",
  ...
)

Arguments

df

Data frame. Must have "Year", "Variable", "ScenarioGroup", and "Value" columns.

vars

Character vector specifying the variable(s) to use (found in df$Variable). If NULL, use all variables in df. Must be specified in scens_plot_*() family. For vars_plot_heatmap(), the order that vars are provided will be used to order the heatmap from top to bottom.

years

Numeric vector specifying the years to show. If NULL, use all years in df.

scenarios

Character vector specifying the scenarios to use (found in df$ScenarioGroup). If NULL, use all scenarios in df. Must be specified in vars_plot_*() family.

plot_colors

Named character vector to set custom plot colors. Names should match scenarios found in df$ScenarioGroup.

scen_labels

Named character vector to set custom legend labels for the scenarios. Used to show legend labels that are different from values found in df$ScenarioGroup.

...

Parameters passed to other functions. See details.

historical

Data frame of historical data to add to the figure. Must have a "Year" column, and the same number of additional columns as the length of vars. If there is only one variable, then the column names in this data frame do not matter (except for Year). However, if there are more than one variable, then the column names must match those in vars.

connect_historical

If historical data are provided, and this is TRUE, then the historical year prior to the first projected year of data will be "connected" to the projections so as to provide a connected look to the plot. If FALSE, then projected data are shown as they natively exist in df and if historical data are not provided this has no affect on the output.

color_by

For var_plot_trace_scatter(), the points can be colored based on a specified column in the df. This should be specified as a string, and should exist in df.

var_labels

Similar to scen_labels, this is a named character that allows the user to cusomize the variable names that show up in the legend.

plot_type

For vars_plot_probs(), should the plot use lines ('line'), or stacked bars ('stacked bar'). Can also use 1 or 2, respectively.

Details

... is used to pass additional options to ggplot functions. The following are used:

scens_plot_probs() and scens_plot_range() will use the following additional options: "y", "title", "color_label", "legend_wrap", "facet_scales", "facet_nrow", and "facet_ncol".

The legend order can be modified by converting "ScenarioGroup" column to a factor before calling scens_plot_*(), with the levels specifying the order the scenarios will show up in the legend.

Value

gg object.

Examples

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
# quick simple plot using 1 variable and all years
scens_plot_boxplot(ex_pe, vars = "powell_dec_pe")

# fully customized for multiple variables, custom colors, and custom names
tst_names <- c("April ST 2007 UCRC" = "Scen 1", "April ST CT" = "Scen 2")
pal <- c("April ST 2007 UCRC" = "#fc8d62", "April ST CT" = "#8da0cb")
scens_plot_boxplot(
  ex_pe, 
  vars = c("powell_dec_pe", "mead_dec_pe"), 
  years = 2021:2036,
  title = "Mead and Powell", subtitle = "End-of-December Elevation",
  y_lab = "(feet)", caption = "Results from April 20xx",
  facet_scales = "free_y", 
  plot_colors = pal,
  scen_labels = tst_names,
  legend_wrap = 10
)

scens_plot_probs(ex_pe, "powell_wy_min_lt_3525", y_lab = "percent")

# quick simple plot of one variable
scens_plot_range(ex_pe, "mead_dec_pe")

# now add ylabel 
scens_plot_range(ex_pe, "mead_dec_pe", y_lab = "feet")

# show two variables
scens_plot_range(
  ex_pe, 
  c("powell_dec_pe", "mead_dec_pe"), 
  facet_scales = "free_y"
)

# subset scenarios
scens_plot_range(ex_pe, "mead_dec_pe", scenarios = "April ST CT")

# custom colors and scenario labels + add title and caption
pc <- c("April ST CT" = "red", "April ST 2007 UCRC" = "black")
sl <- c("April ST CT" = "s1", "April ST 2007 UCRC" = "s2")
scens_plot_range(ex_pe, 
  "powell_dec_pe", 
  plot_colors = pc, 
  scen_labels = sl,
  title = "PE", 
  caption = "this is a caption"
)

# change to two rows in stead of two columns for showing two variables
scens_plot_range(
  ex_pe, 
  c("powell_dec_pe", "mead_dec_pe"), 
  facet_scales = "free_y",
  facet_nrow = 2
)

# scatter plot for Mead elevation in Dec. 2021
var_plot_trace_scatter(
  ex_pe, 
  vars = "mead_dec_pe", 
  years = 2021, 
  scenarios = "April ST CT"
)

# add in a new variable to be used to color the points:
zz <- dplyr::mutate(ex_pe, color_cat = dplyr::case_when(
  Value > 1095 ~ "No concern",
  Value > 1076 ~ "Some concern",
  Value > 1074 ~ "Moderate concern",
  TRUE ~ "concern")
)

cc <- c("No concern" = "grey20", "Some concern" = "blue", 
        "Moderate concern" = "steelblue", "concern" = "red")

# color by the new variable, and show two scenarios:
gg <- var_plot_trace_scatter(
  zz, 
  vars = "mead_dec_pe", 
  years = 2021, 
  scenarios = c("April ST CT", "April ST 2007 UCRC"), 
  color_by = "color_cat"
)


vv <- c("mead_min_lt_1000", "mead_min_lt_1020", "powell_wy_min_lt_3490", 
"powell_dec_lt_3525")

gg <- vars_plot_probs(ex_pe, "April ST CT", vars = vv, years = 2020:2026)

# or show both scenarios:
ss <- unique(ex_pe$ScenarioGroup)
gg <- vars_plot_heatmap(ex_pe, ss, vars = vv, years = 2020:2026)

vv <- c("mead_min_lt_1000", "mead_min_lt_1020", "powell_wy_min_lt_3490", 
"powell_dec_lt_3525")

gg <- vars_plot_probs(ex_pe, "April ST CT", vars = vv)

# it does not make sense to stack bars for these 4 variables, but you can:
gg <- vars_plot_probs(
  ex_pe, 
  "April ST CT", 
  vars = vv, 
  plot_type = "stacked bar"
)

rabutler-usbr/crssplot documentation built on Feb. 6, 2022, 3:33 p.m.