-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathplot_storminess.R
77 lines (68 loc) · 2.55 KB
/
plot_storminess.R
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
#' plot storminess data
#'
#' plots storminess
#'
#' @param shadedRegion Numeric vector. Years denoting the shaded region of the plot (most recent 10)
#' @param report Character string. Which SOE report ("MidAtlantic", "NewEngland")
#' @param n Numeric scalar. Number of years used (from most recent year) to estimate short term trend . Default = 0 (No trend calculated)
#'
#' @return ggplot object
#'
#'
#' @export
#'
plot_storminess <- function(shadedRegion = NULL,
report="MidAtlantic",
n = 0) {
# generate plot setup list (same for all plot functions)
setup <- ecodata::plot_setup(shadedRegion = shadedRegion,
report=report)
# which report? this may be bypassed for some figures
if (report == "MidAtlantic") {
filterEPUs <- c("MAB")
} else {
filterEPUs <- c("GB", "GOM")
}
# optional code to wrangle ecodata object prior to plotting
# e.g., calculate mean, max or other needed values to join below
fix<- ecodata::storminess |>
dplyr::filter(EPU %in% filterEPUs) |>
dplyr::mutate(Time = as.numeric(Year),
Value = as.numeric(Value)) |>
dplyr::group_by(Var) |>
dplyr::mutate(hline = mean(Value))
# code for generating plot object p
# ensure that setup list objects are called as setup$...
# e.g. fill = setup$shade.fill, alpha = setup$shade.alpha,
# xmin = setup$x.shade.min , xmax = setup$x.shade.max
#
p <- fix |>
ggplot2::ggplot(ggplot2::aes(x = Time, y = Value))+
ggplot2::annotate("rect", fill = setup$shade.fill, alpha = setup$shade.alpha,
xmin = setup$x.shade.min , xmax = setup$x.shade.max,
ymin = -Inf, ymax = Inf) +
ggplot2::geom_point()+
ggplot2::geom_line()+
ggplot2::ggtitle("")+
ggplot2::ylab("Number of Events") +
ggplot2::xlab(ggplot2::element_blank())+
ggplot2::facet_wrap(.~Var,ncol = 2)+
ggplot2::geom_hline(ggplot2::aes(yintercept = hline),
linewidth = setup$hline.size,
alpha = setup$hline.alpha,
linetype = setup$hline.lty)+
ecodata::geom_gls()+
ecodata::geom_lm(n=n)+
ecodata::theme_ts()+
ecodata::theme_facet()+
ecodata::theme_title()
# # optional code for New England specific (2 panel) formatting
# if (report == "NewEngland") {
# p <- p +
# ggplot2::theme(legend.position = "bottom",
# legend.title = ggplot2::element_blank())
#
# }
return(p)
}
attr(plot_storminess,"report") <- c("MidAtlantic","NewEngland")