Skip to content

Commit 4fface1

Browse files
committed
add back option to let plot_catch choose best unit
1 parent 6f059c4 commit 4fface1

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

R/catches.R

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff 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",

0 commit comments

Comments
 (0)