1313# ' @export
1414# '
1515
16- plot_abc_acl <- function (shadedRegion = NULL ,
17- report = " MidAtlantic" ,
18- plottype = " Stacked" ) {
19-
16+ plot_abc_acl <- function (
17+ shadedRegion = NULL ,
18+ report = " MidAtlantic" ,
19+ plottype = " Stacked"
20+ ) {
2021 # generate plot setup list (same for all plot functions)
21- setup <- ecodata :: plot_setup(shadedRegion = shadedRegion ,
22- report = report )
22+ setup <- ecodata :: plot_setup(shadedRegion = shadedRegion , report = report )
2323
2424 # which report? this may be bypassed for some figures
2525 if (report == " MidAtlantic" ) {
@@ -38,69 +38,90 @@ plot_abc_acl <- function(shadedRegion = NULL,
3838 # splitting code by report for now
3939
4040 if (report == " MidAtlantic" ) {
41-
4241 ABCs <- ecodata :: abc_acl | >
4342 dplyr :: filter(EPU == filterEPUs ) | >
4443 tidyr :: separate(col = Var , into = c(" Fishery" , " Var" ), sep = " _" ) | >
4544 dplyr :: filter(Var == " Quota" ) | >
46- dplyr :: mutate(Fishery = gsub(" Commercial" , " C" , Fishery ),
47- Fishery = gsub(" Recreational" , " R" , Fishery )) | >
45+ dplyr :: mutate(
46+ Fishery = gsub(" Commercial" , " C" , Fishery ),
47+ Fishery = gsub(" Recreational" , " R" , Fishery )
48+ ) | >
4849 dplyr :: group_by(Fishery , Time ) | >
49- dplyr :: summarise(Value = sum(Value ),
50- .groups = " drop" )
50+ dplyr :: summarise(Value = sum(Value ), .groups = " drop" )
51+
52+ # Determine species order for stacked bar plot based on first year
53+ stackedOrder <- ABCs | >
54+ dplyr :: filter(Time == min(ABCs $ Time )) | >
55+ dplyr :: arrange(Value ) | >
56+ dplyr :: mutate(
57+ Order = 1 : nrow(dplyr :: filter(ABCs , Time == min(ABCs $ Time )))
58+ ) | >
59+ dplyr :: select(Fishery , Order )
60+
61+ # Join stacked bar plot subset with ordered species list based on first year
62+ # Convert 'Fishery' column into a factor with levels based on this order
63+ ABCs <- ABCs | >
64+ dplyr :: left_join(stackedOrder ) | >
65+ dplyr :: mutate(Order = ifelse(is.na(Order ), 0 , Order )) | >
66+ dplyr :: arrange(Order ) | >
67+ dplyr :: mutate(Fishery = factor (Fishery , levels = unique(Fishery )))
5168
5269 CatchABC <- ecodata :: abc_acl | >
53- unique()| >
70+ unique() | >
5471 dplyr :: filter(EPU == filterEPUs ) | >
5572 tidyr :: separate(col = Var , into = c(" Fishery" , " Var" ), sep = " _" ) | >
56- tidyr :: pivot_wider(names_from = Var , values_from = Value ) | >
73+ tidyr :: pivot_wider(names_from = Var , values_from = Value ) | >
5774 # tidyr::separate(Catch, into = c("Catch", "X"), sep = ",") %>%
58- dplyr :: mutate(Catch = as.numeric(stringr :: str_extract(Catch , pattern = " \\ d+" )),
59- Quota = as.numeric(stringr :: str_extract(Quota , pattern = " \\ d+" )),
60- Value = Catch / Quota # ,
61- # Time = as.character(Time)
75+ dplyr :: mutate(
76+ Catch = as.numeric(stringr :: str_extract(Catch , pattern = " \\ d+" )),
77+ Quota = as.numeric(stringr :: str_extract(Quota , pattern = " \\ d+" )),
78+ Value = Catch / Quota # ,
79+ # Time = as.character(Time)
6280 ) | >
6381 dplyr :: filter(! is.na(Value ))
6482
6583 meanCatchABC <- CatchABC | >
6684 dplyr :: group_by(Time ) | >
67- dplyr :: summarise(val = mean(Value ),
68- .groups = " drop" ) | >
85+ dplyr :: summarise(val = mean(Value ), .groups = " drop" ) | >
6986 dplyr :: ungroup() | >
7087 dplyr :: mutate(Time = as.numeric(Time ))
71-
7288 }
7389
7490 if (report == " NewEngland" ) {
75-
7691 ABCs <- ecodata :: abc_acl | >
7792 dplyr :: filter(EPU == filterEPUs ) | >
78- tidyr :: separate(col = Var , into = c(" FMP" , " Fishery" , " Var" ), sep = " _" ) | >
93+ tidyr :: separate(
94+ col = Var ,
95+ into = c(" FMP" , " Fishery" , " Var" ),
96+ sep = " _"
97+ ) | >
7998 dplyr :: filter(Var == " ABC" ) | >
8099 dplyr :: group_by(Fishery , Time ) | >
81- dplyr :: summarise(Value = sum(Value ),
82- .groups = " drop" )
100+ dplyr :: summarise(Value = sum(Value ), .groups = " drop" )
83101
84102 CatchABC <- ecodata :: abc_acl | >
85- unique()| >
103+ unique() | >
86104 dplyr :: filter(EPU == filterEPUs ) | >
87- tidyr :: separate(col = Var , into = c(" FMP" , " Fishery" , " Var" ), sep = " _" ) | >
88- tidyr :: pivot_wider(names_from = Var , values_from = Value ) | >
105+ tidyr :: separate(
106+ col = Var ,
107+ into = c(" FMP" , " Fishery" , " Var" ),
108+ sep = " _"
109+ ) | >
110+ tidyr :: pivot_wider(names_from = Var , values_from = Value ) | >
89111 # tidyr::separate(Catch, into = c("Catch", "X"), sep = ",") %>%
90- dplyr :: mutate(Catch = as.numeric(stringr :: str_extract(Catch , pattern = " \\ d+" )),
91- Quota = as.numeric(stringr :: str_extract(ABC , pattern = " \\ d+" )),
92- Value = Catch / ABC # ,
93- # Time = as.character(Time)
94- ) | >
112+ dplyr :: mutate(
113+ Catch = as.numeric(stringr :: str_extract(Catch , pattern = " \\ d+" )),
114+ Quota = as.numeric(stringr :: str_extract(ABC , pattern = " \\ d+" )),
115+ Value = Catch / ABC # ,
116+ # Time = as.character(Time)
117+ ) | >
95118 dplyr :: filter(! is.na(Value ))
96119
97120 meanCatchABC <- CatchABC | >
98121 dplyr :: group_by(Time ) | >
99- dplyr :: summarise(val = mean(Value ),
100- .groups = " drop" ) | >
122+ dplyr :: summarise(val = mean(Value ), .groups = " drop" ) | >
101123 dplyr :: ungroup() | >
102124 dplyr :: mutate(Time = as.numeric(Time ))
103-
104125 }
105126
106127 # code for generating plot object p
@@ -109,108 +130,63 @@ plot_abc_acl <- function(shadedRegion = NULL,
109130 # xmin = setup$x.shade.min , xmax = setup$x.shade.max
110131 #
111132 if (plottype == " Stacked" ) {
112-
113- p <- ABCs | >
114- ggplot2 :: ggplot()+
115- ggplot2 :: geom_bar(ggplot2 :: aes( y = Value , x = Time , fill = Fishery ), stat = " identity" , position = " stack" )+
116- ggplot2 :: scale_x_continuous(breaks = scales :: pretty_breaks()) +
117- ggplot2 :: ggtitle(" ABC or ACL for Managed Species" )+
118- ggplot2 :: theme(legend.text = ggplot2 :: element_text(size = 8 ),
119- legend.key.height = ggplot2 :: unit(2 , " mm" ))+
120- ggplot2 :: ylab(" ABC or ACL, metric tons" )+
121- ggplot2 :: xlab(ggplot2 :: element_blank())+
122- ecodata :: theme_ts()+
123- ggplot2 :: guides(fill = ggplot2 :: guide_legend(ncol = 1 ))+
124- ecodata :: theme_title()
133+ # Define a new palette that accommodates 18+ data classes
134+ if (report == " MidAtlantic" ) {
135+ customPalette <- rev(Polychrome :: light.colors(19 ))
136+ } else {
137+ customPalette <- Polychrome :: palette36.colors()
138+ }
139+
140+ p <- ABCs | >
141+ ggplot2 :: ggplot() +
142+ ggplot2 :: geom_bar(
143+ ggplot2 :: aes(y = Value , x = Time , fill = Fishery ),
144+ stat = " identity" ,
145+ position = " stack"
146+ ) +
147+ ggplot2 :: scale_x_continuous(breaks = scales :: pretty_breaks()) +
148+ ggplot2 :: ggtitle(" ABC or ACL for Managed Species" ) +
149+ ggplot2 :: theme(
150+ legend.text = ggplot2 :: element_text(size = 8 ),
151+ legend.key.height = ggplot2 :: unit(2 , " mm" )
152+ ) +
153+ ggplot2 :: ylab(" ABC or ACL, metric tons" ) +
154+ ggplot2 :: xlab(ggplot2 :: element_blank()) +
155+ ecodata :: theme_ts() +
156+ ggplot2 :: guides(fill = ggplot2 :: guide_legend(ncol = 1 )) +
157+ ecodata :: theme_title() +
158+ ggplot2 :: scale_fill_manual(values = unname(customPalette [- 16 ]))
125159
126160 return (p )
127-
128161 }
129162
130163 if (plottype == " Catch" ) {
131-
132164 p <- CatchABC | >
133- ggplot2 :: ggplot()+
165+ ggplot2 :: ggplot() +
134166 # geom_boxplot()+
135- ggplot2 :: geom_point(ggplot2 :: aes(x = Time , y = Value ))+
136- ggplot2 :: geom_point(data = meanCatchABC , ggplot2 :: aes(x = Time , y = val ), color = " red" )+
137- ggplot2 :: geom_line(data = meanCatchABC , ggplot2 :: aes(x = Time , y = val ), color = " red" )+
138- ggplot2 :: geom_hline(yintercept = 1 , linetype = ' dashed' , col = ' gray' )+
139- ggplot2 :: scale_x_continuous(breaks = scales :: pretty_breaks()) +
140- ggplot2 :: ggtitle(" Catch per ABC or ACL" )+
141- ggplot2 :: ylab(expression(" Catch / ABC or ACL" ))+
142- ggplot2 :: theme(legend.title = ggplot2 :: element_blank())+
143- ggplot2 :: xlab(ggplot2 :: element_blank())+
144- ecodata :: theme_ts()+
167+ ggplot2 :: geom_point(ggplot2 :: aes(x = Time , y = Value )) +
168+ ggplot2 :: geom_point(
169+ data = meanCatchABC ,
170+ ggplot2 :: aes(x = Time , y = val ),
171+ color = " red"
172+ ) +
173+ ggplot2 :: geom_line(
174+ data = meanCatchABC ,
175+ ggplot2 :: aes(x = Time , y = val ),
176+ color = " red"
177+ ) +
178+ ggplot2 :: geom_hline(yintercept = 1 , linetype = ' dashed' , col = ' gray' ) +
179+ ggplot2 :: scale_x_continuous(breaks = scales :: pretty_breaks()) +
180+ ggplot2 :: ggtitle(" Catch per ABC or ACL" ) +
181+ ggplot2 :: ylab(expression(" Catch / ABC or ACL" )) +
182+ ggplot2 :: theme(legend.title = ggplot2 :: element_blank()) +
183+ ggplot2 :: xlab(ggplot2 :: element_blank()) +
184+ ecodata :: theme_ts() +
145185 ecodata :: theme_title()
146186
147187 return (p )
148188 }
149-
150189}
151190
152- attr(plot_abc_acl ," report" ) <- c(" MidAtlantic" ," NewEngland" )
153- attr(plot_abc_acl ," plottype" ) <- c(" Stacked" ," Catch" )
154-
155-
156- # Paste commented original plot code chunk for reference
157- #
158- # Stacked
159- # mean<- ecodata::abc_acl %>%
160- # ecodata::abc_acl |>
161- # dplyr::filter(EPU == "MAB") |>
162- # tidyr::separate(col = Var, into = c("Fishery", "Var"), sep = "_") |>
163- # dplyr::filter(Var == "Quota") |>
164- # dplyr::mutate(Fishery = gsub("Commercial", "C", Fishery),
165- # Fishery = gsub("Recreational", "R", Fishery)) |>
166- # dplyr::group_by(Fishery, Time) |>
167- # dplyr::summarise(Value = sum(Value)) |>
168- # ggplot2::ggplot()+
169- # ggplot2::geom_bar(ggplot2::aes( y = Value, x = Time, fill = Fishery), stat="identity", position = "stack" )+
170- # ggplot2::ggtitle("ABC or ACL for MAFMC Managed Species")+
171- # ggplot2::theme(legend.text = ggplot2::element_text(size = 8),
172- # legend.key.height = ggplot2::unit(2, "mm"))+
173- # ggplot2::ylab("ABC or ACL")+
174- # ggplot2::xlab(ggplot2::element_blank())+
175- # ecodata::theme_ts()+
176- # ggplot2::guides(fill=ggplot2::guide_legend(ncol=2))+
177- # ecodata::theme_title()
178- #
179- # Catch
180- # mean<- ecodata::abc_acl %>%
181- # dplyr::filter(EPU == "MAB") %>%
182- # tidyr::separate(col = Var, into = c("FMP", "Var"), sep = "_") %>%
183- # tidyr::pivot_wider(names_from = Var, values_from = Value) %>%
184- # #tidyr::separate(Catch, into = c("Catch", "X"), sep = ",") %>%
185- # dplyr::mutate(Catch = as.numeric(stringr::str_extract(Catch, pattern = "\\d+")),
186- # Quota = as.numeric(stringr::str_extract(Quota, pattern = "\\d+")),
187- # Value = Catch/Quota,
188- # Time = as.character(Time)) %>%
189- # filter(!Value == "NA") %>%
190- # dplyr::group_by(Time) %>%
191- # dplyr::summarise(val = mean(Value)) %>%
192- # dplyr::ungroup() %>%
193- # dplyr::mutate(Time = as.numeric(Time))
194- #
195- # ecodata::abc_acl %>%
196- # dplyr::filter(EPU == "MAB") %>%
197- # tidyr::separate(col = Var, into = c("FMP", "Var"), sep = "_") %>%
198- # tidyr::pivot_wider(names_from = Var, values_from = Value) %>%
199- # #tidyr::separate(Catch, into = c("Catch", "X"), sep = ",") %>%
200- # dplyr::mutate(Catch = as.numeric(stringr::str_extract(Catch, pattern = "\\d+")),
201- # Quota = as.numeric(stringr::str_extract(Quota, pattern = "\\d+")),
202- # Value = Catch/Quota,
203- # Time = as.numeric(Time))%>%
204- # filter(!Value == "NA") %>%
205- # ggplot2::ggplot()+
206- # #geom_boxplot()+
207- # geom_point(aes(x = Time, y = Value))+
208- # geom_point(data = mean, aes(x = Time, y = val), color = "red")+
209- # geom_line(data = mean, aes(x = Time, y = val), color = "red")+
210- # geom_hline(yintercept = 1, linetype='dashed', col = 'gray')+
211- # ggplot2::ggtitle("MAFMC Catch per ABC or ACL")+
212- # ggplot2::ylab(expression("Catch / ABC or ACL"))+
213- # ggplot2::theme(legend.title = element_blank())+
214- # ggplot2::xlab(element_blank())+
215- # ecodata::theme_ts()+
216- # ecodata::theme_title()
191+ attr(plot_abc_acl , " report" ) <- c(" MidAtlantic" , " NewEngland" )
192+ attr(plot_abc_acl , " plottype" ) <- c(" Stacked" , " Catch" )
0 commit comments