Skip to content

Commit

Permalink
French-ify several plotting functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cgrandin committed Oct 24, 2024
1 parent c4e2e9e commit 66ca660
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 38 deletions.
85 changes: 52 additions & 33 deletions R/extra.R
Original file line number Diff line number Diff line change
@@ -1,49 +1,68 @@
plot_cpue_predictions <- function(dat, model_version = "Combined", scale = FALSE) {
plot_cpue_predictions <- function(dat,
model_version = "Combined",
scale = FALSE,
french = FALSE) {
if (scale) {
dat <- dat %>%
group_by(.data$formula_version, .data$model, .data$area) %>%
mutate(geo_mean = exp(mean(log(.data$est)))) %>%
mutate(upr = .data$upr / .data$geo_mean, lwr = .data$lwr / .data$geo_mean,
est = .data$est / .data$geo_mean) %>%
dat <- dat |>
group_by(.data$formula_version, .data$model, .data$area) |>
mutate(geo_mean = exp(mean(log(.data$est)))) |>
mutate(upr = .data$upr / .data$geo_mean,
lwr = .data$lwr / .data$geo_mean,
est = .data$est / .data$geo_mean) |>
ungroup()
}

unstandardized <- dat %>%
filter(.data$formula_version == "Unstandardized") %>%
unstandardized <- dat |>
filter(.data$formula_version == "Unstandardized") |>
rename(est_unstandardized = .data$est,
lwr_unstandardized = .data$lwr, upr_unstandardized = .data$upr) %>%
lwr_unstandardized = .data$lwr, upr_unstandardized = .data$upr) |>
select(.data$year, .data$area, .data$model,
.data$est_unstandardized, .data$lwr_unstandardized,
.data$upr_unstandardized)
.data$est_unstandardized, .data$lwr_unstandardized,
.data$upr_unstandardized)

temp <- dat %>%
filter(.data$formula_version != "Unstandardized") %>%
left_join(unstandardized, by = c("area", "year", "model")) %>%
filter(model == model_version) %>%
mutate(formula_version = gsub("\\+ ", " ", .data$formula_version)) %>%
mutate(
formula_version =
gsub("Full standardization", "All variables", .data$formula_version)
) %>%
mutate(
formula_version =
forcats::fct_relevel(.data$formula_version, "All variables", after = Inf)
temp <- dat |>
filter(.data$formula_version != "Unstandardized") |>
left_join(unstandardized, by = c("area", "year", "model")) |>
filter(model == model_version) |>
mutate(formula_version = gsub("\\+ ", " ", .data$formula_version)) |>
mutate(formula_version =
gsub("Full standardization",
ifelse(french,
"Toutes les variables",
"All variables"),
.data$formula_version)
) |>
mutate(formula_version =
forcats::fct_relevel(.data$formula_version,
ifelse(french,
"Toutes les variables",
"All variables"),
after = Inf)
)
temp %>%

if(!scale){
y_label <- ifelse(french,
"CPUE (kg/heure)",
"CPUE (kg/hour)")
}else{
y_label <- ifelse(french,
"CPUE (kg/h) moyenne géométrique divisée",
"CPUE (kg/hour) divided\nby geometric mean")
}

temp |>
ggplot(aes_string("year", "est", ymin = "upr", ymax = "lwr")) +
geom_line(aes_string(y = "est_unstandardized"), colour = "grey30", lty = 1) +
ggplot2::geom_ribbon(
aes_string(ymin = "lwr_unstandardized", ymax = "upr_unstandardized"),
fill = "#00000030"
) +
geom_line(aes_string(y = "est_unstandardized"),
colour = "grey30",
lty = 1) +
ggplot2::geom_ribbon(aes_string(ymin = "lwr_unstandardized",
ymax = "upr_unstandardized"),
fill = "#00000030") +
ggplot2::geom_ribbon(alpha = 0.4, fill = "red") +
geom_line(colour = "red") +
theme_pbs() +
facet_grid(area ~ formula_version, scales = "free_y") +
labs(
y = if (!scale) "CPUE (kg/hour)" else "CPUE (kg/hour) divided\nby geometric mean",
x = ""
) +
labs(y = y_label, x = "") +
ylim(0, NA) +
guides(colour = "none", fill = "none")
}
Expand Down
13 changes: 9 additions & 4 deletions R/plot_predictor_bubbles.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#' @param variable The column to summarize (character value)
#' @param group_id The events to count over (e.g. `"fishing_event_id"` or `"trip_id"`)
#' @param ncol Number of facetted columns (facets by area)
#' @param french If `TRUE`, all text will appear as French in the plot
#'
#' @return A ggplot object
#' @export
Expand All @@ -16,7 +17,7 @@
#' area = rep("a", 400))
#' plot_predictor_bubbles(d, "my_predictor")
plot_predictor_bubbles <- function(dat, variable,
group_id = "fishing_event_id", ncol = 2) {
group_id = "fishing_event_id", ncol = 2, french = FALSE) {
temp_pos <- dat %>%
filter(spp_catch > 0) %>%
group_by(area, year, !!rlang::sym(variable)) %>%
Expand All @@ -32,15 +33,19 @@ plot_predictor_bubbles <- function(dat, variable,
mutate(n_tot = sum(n)) %>%
ungroup()

leg_title <- ifelse(french,
paste0("Nombre de\n", group_id),
paste0("Number of\n", group_id))

p <- temp_pos %>%
ggplot(aes_string("as.factor(year)", y = variable)) +
geom_point(aes_string(size = "n", fill = "n"), alpha = 0.4, pch = 21) +
geom_point(data = temp_all, aes_string(size = "n"), alpha = 0.4, pch = 21) +
facet_wrap(~area, scales = "free", ncol = ncol) +
ggplot2::scale_x_discrete(breaks = seq(1950, 2020, 5)) +
xlab("") + ylab(firstup(gsub("_", " ", variable))) +
labs(size = paste0("Number of\n", group_id)) +
labs(fill = paste0("Number of\n", group_id)) +
xlab("") + ylab(firstup(gsub("_", " ", en2fr(variable, translate = french)))) +
labs(size = leg_title) +
labs(fill = leg_title) +
ggplot2::scale_size_continuous(range = c(0, 7), breaks = c(1, 10, 100, 500, 1000)) +
ggplot2::scale_fill_viridis_c(trans = "log", breaks = c(1, 10, 100, 500, 1000)) +
guides(fill = guide_legend(reverse=T), size = guide_legend(reverse=T)) +
Expand Down
10 changes: 9 additions & 1 deletion man/plot_predictor_bubbles.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 66ca660

Please sign in to comment.