File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -129,12 +129,26 @@ plot_catch <- function(dat,
129129 unreliable = c(1996 , 2006 ),
130130 blank_plot = FALSE ) {
131131
132- units <- match.arg(units )
133- scale_val <- switch (units ,
134- `1000 t` = 1e6 ,
135- `kt` = 1e6 ,
136- `t` = 1e3 ,
137- `kg` = 1 )
132+ # Choose best unit (or user specified)
133+ unit_label <- c(" 1000 t" = 1e6 , " kt" = 1e6 , " t" = 1e3 , " kg" = 1 )
134+
135+ if (is.null(units )) {
136+ max_val <- max(dat $ value , na.rm = TRUE )
137+ matches <- which((max_val / unit_label ) > = 1 )
138+
139+ if (length(matches ) > 0 ) {
140+ idx <- matches [which.max(unit_label [matches ])] # largest matching unit
141+ } else {
142+ idx <- which.min(unit_label ) # smallest unit if no matches
143+ }
144+
145+ units <- names(unit_label )[[idx ]]
146+ scale_val <- unit_label [[idx ]]
147+
148+ } else {
149+ units <- match.arg(units )
150+ scale_val <- unit_label [[units ]]
151+ }
138152
139153 gears <- c(
140154 " Bottom trawl" ,
You can’t perform that action at this time.
0 commit comments