Skip to content

Commit

Permalink
Merge pull request #17 from ssi-dk:telkamp7/issue12
Browse files Browse the repository at this point in the history
Telkamp7/issue12
  • Loading branch information
telkamp7 authored Nov 2, 2023
2 parents d56e249 + 4b3271f commit f13f42e
Show file tree
Hide file tree
Showing 7 changed files with 266 additions and 7 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ RoxygenNote: 7.2.3
URL: https://github.com/telkamp7/aedseo, https://telkamp7.github.io/aedseo/
BugReports: https://github.com/telkamp7/aedseo/issues
Suggests:
ggplot2,
ISOweek,
kableExtra,
knitr,
Expand All @@ -23,6 +22,7 @@ Config/testthat/edition: 3
Imports:
base,
dplyr,
ggplot2,
lifecycle,
magrittr,
purrr,
Expand Down
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# Generated by roxygen2: do not edit by hand

S3method(autoplot,aedseo)
S3method(autoplot,aedseo_tsd)
S3method(plot,aedseo)
S3method(plot,aedseo_tsd)
S3method(predict,aedseo)
S3method(summary,aedseo)
export("%>%")
export(aedseo)
export(fit_growth_rate)
export(tsd)
importFrom(ggplot2,autoplot)
importFrom(graphics,plot)
importFrom(lifecycle,deprecated)
importFrom(magrittr,"%>%")
importFrom(rlang,.data)
Expand Down
78 changes: 78 additions & 0 deletions R/autoplot.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#' Create a complete ggplot appropriate to a particular data type
#'
#' @description
#' `r lifecycle::badge("experimental")`
#'
#' This function generates a complete ggplot object suitable for
#' visualizing time series data in an `aedseo_tsd` object. It creates a line
#' plot connecting the observations and adds points at each data point.
#'
#' @param object An `aedseo_tsd` or `aedseo` object
#' @param linewidth Numeric, the width of the line for the growth rate
#' @param alpha Numeric, the alpha (transparency) for the confidence interval
#' ribbon
#' @param ... Additional arguments (not used).
#'
#' @return A ggplot object for visualizing the time series data.
#'
#' @aliases autoplot
#'
#' @examples
#' # Create an example aedseo_tsd object
#' aedseo_tsd_object <- tsd(
#' observed = c(100, 120, 150, 180, 220, 270),
#' time = as.Date(c(
#' "2023-01-01",
#' "2023-01-02",
#' "2023-01-03",
#' "2023-01-04",
#' "2023-01-05",
#' "2023-01-06"
#' )),
#' time_interval = "day"
#' )
#'
#' # Create a ggplot visualization for the aedseo_tsd object
#' # autoplot(aedseo_tsd_object)
#'
#' # Create an aedseo object
#' aedseo_object <- aedseo(
#' tsd = aedseo_tsd_object,
#' k = 3,
#' level = 0.95,
#' family = "quasipoisson"
#' )
#'
#' # Create a ggplot visualization of growth rates with confidence intervals
#' # autoplot(aedseo_object, linewidth = 1, alpha = 0.2)
#' @importFrom ggplot2 autoplot
#' @rdname autoplot
#' @method autoplot aedseo_tsd
#' @export
autoplot.aedseo_tsd <- function(object, ...) {
object %>%
ggplot2::ggplot(
mapping = ggplot2::aes(
x = .data$time,
y = .data$observed
)
) +
ggplot2::geom_point() +
ggplot2::geom_line()
}
#' @rdname autoplot
#' @method autoplot aedseo
#' @export
autoplot.aedseo <- function(object, linewidth = 0.7, alpha = 0.3, ...) {
object %>%
ggplot2::ggplot(
mapping = ggplot2::aes(
x = .data$reference_time,
y = .data$growth_rate,
ymin = .data$lower_growth_rate,
ymax = .data$upper_growth_rate
)
) +
ggplot2::geom_line(linewidth = linewidth) +
ggplot2::geom_ribbon(alpha = alpha)
}
59 changes: 59 additions & 0 deletions R/plot.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#' Create a complete ggplot appropriate to a particular data type
#'
#' @description
#' `r lifecycle::badge("experimental")`
#'
#' This function generates a complete ggplot object suitable for
#' visualizing time series data in an `aedseo_tsd` object. It creates a line
#' plot connecting the observations and adds points at each data point.
#'
#' @param x An `aedseo_tsd` or `aedseo` object
#' @param ... Additional arguments passed to specific methods.
#'
#' @return A ggplot object for visualizing the time series data.
#'
#' @aliases plot
#'
#' @seealso [autoplot()]
#'
#' @examples
#' # Create an example aedseo_tsd object
#' aedseo_tsd_object <- tsd(
#' observed = c(100, 120, 150, 180, 220, 270),
#' time = as.Date(c(
#' "2023-01-01",
#' "2023-01-02",
#' "2023-01-03",
#' "2023-01-04",
#' "2023-01-05",
#' "2023-01-06"
#' )),
#' time_interval = "day"
#' )
#'
#' # Create a ggplot visualization for the aedseo_tsd object
#' plot(aedseo_tsd_object)
#'
#' # Create an aedseo object
#' aedseo_object <- aedseo(
#' tsd = aedseo_tsd_object,
#' k = 3,
#' level = 0.95,
#' family = "quasipoisson"
#' )
#'
#' # Create a ggplot visualization of growth rates with confidence intervals
#' plot(aedseo_object, linewidth = 1, alpha = 0.2)
#' @importFrom graphics plot
#' @rdname plot
#' @method plot aedseo_tsd
#' @export
plot.aedseo_tsd <- function(x, ...) {
print(autoplot(x, ...))
}
#' @rdname plot
#' @method plot aedseo
#' @export
plot.aedseo <- function(x, ...) {
print(autoplot(object = x, ...))
}
61 changes: 61 additions & 0 deletions man/autoplot.Rd

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

59 changes: 59 additions & 0 deletions man/plot.Rd

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

8 changes: 2 additions & 6 deletions vignettes/aedseo_introduction.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,8 @@ n <- years * weeks
# ... and a vector containing the overall time passed
time_overall <- 1:n
# Create arbitrary dates
dates <- as.POSIXct(
x = time_overall * 86400 * 7,
origin = "2010-01-01",
tz = "UTC",
format = "%F"
)
dates <- seq.Date(from = as.Date("2010-01-01"), by = "week", length.out = n)
# Simulate the data
simulation <- simulate_from_nbinom(t = time_overall, theta = log(1000), phi = 5)
Expand Down

0 comments on commit f13f42e

Please sign in to comment.