2424# ' \item{plot}{ggplot object}
2525# ' \item{data}{dataframe used in the plotting}
2626# '
27- # ' @importFrom magrittr "%>%"
2827# ' @importFrom rlang .data
2928# '
3029# ' @export
3130
32- plot_ts <- function (itis = NULL , stock = NULL , metric = " Catch" ,
33- facetplot = FALSE , printfig = TRUE ) {
34-
35-
31+ plot_ts <- function (
32+ itis = NULL ,
33+ stock = NULL ,
34+ metric = " Catch" ,
35+ facetplot = FALSE ,
36+ printfig = TRUE
37+ ) {
3638 # error check for metric names
3739 if (is.null(itis )) {
3840 stop(" If you do not know the ITIS code then please use get_species_itis()" )
3941 }
4042 if (! metric %in% c(" Catch" , " Fmort" , " Recruitment" , " Abundance" , " Index" )) {
41- stop(" Please use of the defined metrics to plot. Catch, Fmort, Recruitment,
42- Abundance, Index" )
43+ stop(
44+ " Please use of the defined metrics to plot. Catch, Fmort, Recruitment,
45+ Abundance, Index"
46+ )
4347 }
4448
45-
46-
4749 # filter by ITIS and Metric
48- dataToPlot <- stocksmart :: stockAssessmentData %> %
49- dplyr :: filter(.data $ ITIS == itis ,
50- .data $ Metric == metric ) %> %
50+ dataToPlot <- stocksmart :: stockAssessmentData | >
51+ dplyr :: filter(.data $ ITIS == itis , .data $ Metric == metric ) | >
5152 dplyr :: mutate(AssessmentYear = as.factor(.data $ AssessmentYear ))
5253
53- if (! is.null(stock )) { # filter by stock
54- dataToPlot <- dataToPlot %> %
54+ if (! is.null(stock )) {
55+ # filter by stock
56+ dataToPlot <- dataToPlot | >
5557 dplyr :: filter(.data $ StockName == stock )
5658 }
5759
@@ -61,83 +63,103 @@ plot_ts <- function(itis = NULL, stock = NULL, metric = "Catch",
6163 }
6264
6365 # select stocks for this ITIS
64- stocks <- dataToPlot % > %
66+ stocks <- dataToPlot | >
6567 dplyr :: distinct(.data $ StockName )
6668
6769 # if multiple stocks and user argument - NULL
6870 if ((nrow(stocks ) > 1 ) && (is.null(stock ))) {
69- message(paste0(" There are multiple stocks for ITIS = " ,
70- itis ,
71- " . Please specify which stock you'd like to plot" ))
71+ message(paste0(
72+ " There are multiple stocks for ITIS = " ,
73+ itis ,
74+ " . Please specify which stock you'd like to plot"
75+ ))
7276 stop(stocks )
7377 }
7478 # if multiple stocks and user argument doesn't match
75- if ((nrow(stocks ) > 1 ) && (! stock %in% (stocks % > % dplyr :: pull()))) {
79+ if ((nrow(stocks ) > 1 ) && (! stock %in% (stocks | > dplyr :: pull()))) {
7680 message(paste0(" Please specify a valid stock to plot " ))
7781 stop(stocks )
7882 }
7983
8084 # if only a single stock and user argument doesn't match
8185 if (nrow(stocks ) == 1 ) {
82- if (is.null(stock )) { # user left blank
86+ if (is.null(stock )) {
87+ # user left blank
8388 stock <- stocks
84- } else { # user entered a string
85- if (! stock %in% (stocks %> % dplyr :: pull())) {
89+ } else {
90+ # user entered a string
91+ if (! stock %in% (stocks | > dplyr :: pull())) {
8692 message(paste0(" Please specify a valid stock to plot " ))
8793 stop(stocks )
8894 }
8995 }
90-
9196 }
9297
9398 # filter out stock to plot
9499 if (length(unique(dataToPlot $ StockArea )) > 1 ) {
95100 # filter out stock
96- stockToPlot <- dataToPlot %> %
97- dplyr :: filter(.data $ StockName == stock ) %> %
98- dplyr :: mutate(AssmtYrUnits = as.factor(paste0(.data $ AssessmentYear ,
99- " (" , .data $ Units , " )" )))
101+ stockToPlot <- dataToPlot | >
102+ dplyr :: filter(.data $ StockName == stock ) | >
103+ dplyr :: mutate(
104+ AssmtYrUnits = as.factor(paste0(
105+ .data $ AssessmentYear ,
106+ " (" ,
107+ .data $ Units ,
108+ " )"
109+ ))
110+ )
100111 } else {
101112 # no need to filter out stock (else causes error in dplyr 1.1.0)
102- stockToPlot <- dataToPlot %> %
103- dplyr :: mutate(AssmtYrUnits = as.factor(paste0(.data $ AssessmentYear ,
104- " (" , .data $ Units , " )" )))
113+ stockToPlot <- dataToPlot | >
114+ dplyr :: mutate(
115+ AssmtYrUnits = as.factor(paste0(
116+ .data $ AssessmentYear ,
117+ " (" ,
118+ .data $ Units ,
119+ " )"
120+ ))
121+ )
105122 }
106123
107124 # units check and standardize
108125 # eg if units change over time from say mt to thousands mt
109126
110-
111127 # plot data
112128 if (facetplot == TRUE ) {
113129 p <- ggplot2 :: ggplot(stockToPlot ) +
114- ggplot2 :: geom_line(ggplot2 :: aes(x = .data $ Year ,
115- y = .data $ Value )) +
116- ggplot2 :: facet_wrap(~ .data $ AssmtYrUnits , scales = " free_y" ) +
130+ ggplot2 :: geom_line(ggplot2 :: aes(x = .data $ Year , y = .data $ Value )) +
131+ ggplot2 :: facet_wrap(~ .data $ AssmtYrUnits , scales = " free_y" ) +
117132 ggplot2 :: ylab(metric )
118133 } else {
119- if ((stockToPlot %> % dplyr :: distinct(.data $ Units ) %> % nrow()) > 1 ) {
120- message(" The units change over time. Plotting as a facet plot
121- will be better" )
134+ if ((stockToPlot | > dplyr :: distinct(.data $ Units ) | > nrow()) > 1 ) {
135+ message(
136+ " The units change over time. Plotting as a facet plot
137+ will be better"
138+ )
122139 }
123140 p <- ggplot2 :: ggplot(stockToPlot ) +
124- ggplot2 :: geom_line(ggplot2 :: aes(x = .data $ Year ,
125- y = .data $ Value ,
126- color = .data $ AssessmentYear )) +
127- ggplot2 :: ylab(paste0(metric , " (" , stockToPlot %> %
128- dplyr :: distinct(.data $ Units ), " )" )) +
141+ ggplot2 :: geom_line(ggplot2 :: aes(
142+ x = .data $ Year ,
143+ y = .data $ Value ,
144+ color = .data $ AssessmentYear
145+ )) +
146+ ggplot2 :: ylab(paste0(
147+ metric ,
148+ " (" ,
149+ stockToPlot | >
150+ dplyr :: distinct(.data $ Units ),
151+ " )"
152+ )) +
129153 ggplot2 :: labs(color = " Assessment Year" )
130154 }
131155
132156 p <- p +
133- ggplot2 :: ggtitle(stockToPlot % > % dplyr :: distinct(.data $ StockName ))
157+ ggplot2 :: ggtitle(stockToPlot | > dplyr :: distinct(.data $ StockName ))
134158
135159 # print figure to window
136160 if (printfig ) {
137161 print(p )
138162 }
139163
140164 return (list (plot = p , data = stockToPlot ))
141-
142-
143165}
0 commit comments