Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
210 changes: 22 additions & 188 deletions R/utils_plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ plot_timeseries <- function(
x = .data[[x]],
ymin = estimate_lower,
ymax = estimate_upper,
fill = group_var
fill = interaction(model, group_var)
),
alpha = 0.3
) +
Expand All @@ -99,9 +99,9 @@ plot_timeseries <- function(
ggplot2::aes(
x = .data[[x]],
y = .data[[y]],
linetype = group_var,
# linetype = group_var,
# linetype = ifelse(!is.null(group), group_var, "solid"),
color = model
color = interaction(model, group_var)
),
# linewidth = 1.0,
...
Expand All @@ -122,195 +122,29 @@ plot_timeseries <- function(
)

# Add labels to axis and legend
labs <- plot + ggplot2::labs(
x = xlab,
y = ylab,
color = "Model",
linetype = cap_first_letter(group),
fill = cap_first_letter(group),
shape = cap_first_letter(group)
)

# Remove linetype or point when there is no grouping
if (is.null(group) & length(unique(dat$model)) == 1) {
labs <- switch(geom,
"line" = labs + ggplot2::guides(linetype = "none"),
"point" = labs + ggplot2::guides(shape = "none"),
# return plot if option beyond line and point for now
labs
)
}
if (length(unique(dat$model)) == 1) {
labs <- switch(geom,
"line" = labs + ggplot2::guides(color = "none"),
"point" = labs + ggplot2::guides(color = "none"),
"area" = labs + ggplot2::guides(fill = "none"),
# return plot if option beyond line and point for now
labs
)
}

# Calc axis breaks
x_n_breaks <- axis_breaks(dat[[x]])
breaks <- ggplot2::scale_x_continuous(
breaks = x_n_breaks,
guide = ggplot2::guide_axis(
minor.ticks = TRUE
)
)

# Put together final plot
final <- labs + breaks + ggplot2::expand_limits(y = 0) +
ggplot2::scale_y_continuous(
labels = scales::label_comma()
if (length(unique(dat$model)) > 1 & !is.null(group)) {
labs <- plot + ggplot2::labs(
x = xlab,
y = ylab
# color = "Model",
# linetype = cap_first_letter(group),
# fill = cap_first_letter(group),
# shape = cap_first_letter(group)
) +
ggplot2::theme(legend.title = ggplot2::element_blank())
} else {
labs <- plot + ggplot2::labs(
x = xlab,
y = ylab,
color = "Model",
linetype = cap_first_letter(group),
fill = cap_first_letter(group),
shape = cap_first_letter(group)
)

# Remove legend if no group is selected
if (is.null(group) & is.data.frame(dat) & any(is.na(unique(dat$model)))) {
final <- final + ggplot2::theme(legend.position = "none")
}

# Check if facet(s) are desired
if (!is.null(facet)) {
facet <- paste("~", paste(facet, collapse = " + "))
facet_formula <- stats::reformulate(facet)

final <- final + ggplot2::facet_wrap(facet_formula)
}
final
}

#------------------------------------------------------------------------------

#' Create plot with error
#'
#' @param dat filtered data frame from standard output file(s) preformatted for
#' the target label from \link[stockplotr]{filter_data}
#' @param x a string of the column name of data used to plot on the x-axis (default
#' is "year")
#' @param y a string of the column name of data used to plot on the y-axis (default
#' is "estimate")
#' @param geom type of geom to use for plotting found in ggplot2 (e.g. "point",
#' "line", etc.). Default is "line". Other options are "point" and "area".
#' @param xlab a string of the x-axis label (default is "Year")
#' @param ylab a string of the y-axis label. If NULL, it will be set to the name
#' of `y`.
#' @param group a string of a single column that groups the data (e.g. "fleet",
#' "sex", "area", etc.). Currently can only have one level of grouping.
#' @param facet a string or vector of strings of a column that facets the data
#' (e.g. "year", "area", etc.)
#' @param ... inherited arguments from internal functions from ggplot2::geom_xx
#'
#'
#' @returns Create a time series plot for a stock assessment report. The user
#' has options to create a line, point, or area plot where the x-axis is year
#' and Y can vary for any time series quantity. Currently, grouping is
#' restricted to one group where faceting can be any number of facets.
#' @export
#'
#' @examples
#' \dontrun{
#' plot_timeseries(dat,
#' x = "year",
#' y = "estimate",
#' geom = "line",
#' xlab = "Year",
#' ylab = "Biomass",
#' group = "fleet",
#' facet = "area"
#' )
#' }
plot_timeseries <- function(
dat,
x = "year",
y = "estimate",
geom = "line",
xlab = "Year",
ylab = NULL,
group = NULL,
facet = NULL,
...
) {
# Start plot
plot <- ggplot2::ggplot()
# make into new geom?
# more defaults and fxnality for ggplot

# Add geom
plot <- switch(geom,
"point" = {
point_size <- ifelse(
is.null(list(...)$size),
2.0,
list(...)$size
)
plot +
ggplot2::geom_point(
data = dat,
ggplot2::aes(
.data[[x]],
.data[[y]],
# TODO: add more groupings
# shape = ifelse(any(grepl("shape", names(group))), .data[[group[[grep("shape", names(group))]]]], 1),
# color = ifelse(any(grepl("color", names(group))), .data[[group[[grep("color", names(group))]]]], "black")
color = model,
shape = group_var
),
# size = point_size,
...
)
},
"line" = {
plot +
ggplot2::geom_ribbon(
dat = dat |> dplyr::filter(!is.na(estimate_lower)),
ggplot2::aes(
x = .data[[x]],
ymin = estimate_lower,
ymax = estimate_upper
),
colour = "grey",
alpha = 0.3
) +
ggplot2::geom_line(
data = dat,
ggplot2::aes(
.data[[x]],
.data[[y]],
linetype = group_var,
# linetype = ifelse(!is.null(group), group_var, "solid"),
color = model
),
# linewidth = 1.0,
...
)
},
"area" = {
plot +
ggplot2::geom_area(
data = dat,
ggplot2::aes(
x = .data[[x]],
y = .data[[y]],
fill = model
),
...
)
}
)

# Add labels to axis and legend
labs <- plot + ggplot2::labs(
x = xlab,
y = ylab,
color = "Model",
linetype = cap_first_letter(group),
fill = cap_first_letter(group),
shape = cap_first_letter(group)
)

# Remove linetype or point when there is no grouping
if (is.null(group)) {
if (is.null(group) & length(unique(dat$model)) == 1) {
labs <- switch(geom,
"line" = labs + ggplot2::guides(linetype = "none"),
"point" = labs + ggplot2::guides(shape = "none"),
Expand Down
30 changes: 0 additions & 30 deletions man/plot_timeseries.Rd

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

Loading