Skip to content

Commit

Permalink
Avoid small bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
damianooldoni committed Aug 5, 2022
1 parent 1ce16a2 commit b7d4df1
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions R/apply_gam.R
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,9 @@ apply_gam <- function(df,
growth = NA_real_,
method = method_em
)
model <- deriv1 <- deriv2 <- summary_pv <- p_ok <- ptitle <- NULL
# Initialize the plot with observations only
model <- deriv1 <- deriv2 <- summary_pv <- p_ok <- NULL
# Compose plot title
ptitle <- paste("GAM", type_indicator, method_em, sep = "_")
if (!is.null(taxon_key)) {
ptitle <- paste(ptitle, taxon_key, sep = "_")
}
Expand All @@ -417,6 +418,7 @@ apply_gam <- function(df,
if (!is.null(df_title)) {
ptitle <- paste(ptitle, df_title, sep = "_")
}
# Initialize the plot with observations only
plot_gam <- df %>%
ggplot2::ggplot(ggplot2::aes(x = .data$year, y = get(y_var))) +
ggplot2::geom_point(color = "black") +
Expand Down Expand Up @@ -456,7 +458,9 @@ apply_gam <- function(df,
))
}
# add annotation saying that emergence status cannot be assessed
plot_gam <- add_annotation(plot_obs = plot_gam, df = df)
plot_gam <- add_annotation(plot_obs = plot_gam,
df = df,
y_axis = y_var)
} else {
if (isFALSE(p_ok)) {
if (verbose) {
Expand All @@ -467,7 +471,9 @@ apply_gam <- function(df,
))
}
# add annotation saying that emergence status cannot be assessed
plot_gam <- add_annotation(plot_obs = plot_gam, df = df)
plot_gam <- add_annotation(plot_obs = plot_gam,
df = df,
y_axis = y_var)
} else {
output_model <- df
# Add method
Expand Down Expand Up @@ -580,14 +586,7 @@ apply_gam <- function(df,
.data$growth,
.data$method
)

# Create plot with conf. interval + colour for status
ptitle <- paste("GAM",
type_indicator,
method_em,
p_title,
sep = "_"
)
plot_gam <- plot_ribbon_em(
df_plot = output_model,
x_axis = year,
Expand Down Expand Up @@ -629,7 +628,9 @@ apply_gam <- function(df,
}
}
# add annotation saying that emergence status cannot be assessed
plot_gam <- add_annotation(plot_obs = plot_gam, df = df)
plot_gam <- add_annotation(plot_obs = plot_gam,
df = df,
y_axis = y_var)
}

# save plot if asked
Expand Down Expand Up @@ -739,6 +740,7 @@ plot_ribbon_em <- function(df_plot,
#'
#' @param plot_obs ggplot2 plot object showing the observations.
#' @param df tibble data.frame with observations.
#' @param y_axis character. The name of the column containing the data to plot
#' @param text character to show as annotation. Default: "The emergence status
#' cannot be assessed".
#' @param colour colour of the annotation. Default: red.
Expand All @@ -747,11 +749,12 @@ plot_ribbon_em <- function(df_plot,
add_annotation <- function(
plot_obs,
df,
y_axis,
text = "The emergence status \ncannot be assessed",
colour = "red") {
annotated_plot <- plot_obs +
ggplot2::annotate("text",
y = max(df$obs),
y = max(df[[y_axis]]),
x = max(df$year),
hjust = 1,
vjust = 1,
Expand Down

0 comments on commit b7d4df1

Please sign in to comment.