Skip to content

Commit

Permalink
Refactoring of plot method
Browse files Browse the repository at this point in the history
Drop the t0 argument and instead use the tspan names attribute for x
axis ticks and labels.
  • Loading branch information
stewid committed Feb 4, 2017
1 parent b0526ea commit d07f68e
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 54 deletions.
4 changes: 2 additions & 2 deletions R/SEIR.R
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ setMethod("recovered",
##' @export
setMethod("plot",
signature(x = "SEIR"),
function(x, t0 = NULL, ...)
function(x, ...)
{
callNextMethod(x, t0 = t0, legend = c("S", "E", "I", "R"), ...)
callNextMethod(x, legend = c("S", "E", "I", "R"), ...)
}
)

Expand Down
4 changes: 2 additions & 2 deletions R/SIR.R
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,9 @@ setMethod("recovered",
##' @export
setMethod("plot",
signature(x = "SIR"),
function(x, t0 = NULL, ...)
function(x, ...)
{
callNextMethod(x, t0 = t0, legend = c("S", "I", "R"), ...)
callNextMethod(x, legend = c("S", "I", "R"), ...)
}
)

Expand Down
4 changes: 2 additions & 2 deletions R/SISe.R
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ setMethod("prevalence",
##' @export
setMethod("plot",
signature(x = "SISe"),
function(x, t0 = NULL, ...)
function(x, ...)
{
callNextMethod(x, t0 = t0, legend = c("S", "I"), ...)
callNextMethod(x, legend = c("S", "I"), ...)
}
)
3 changes: 1 addition & 2 deletions R/SISe3.R
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,9 @@ setMethod("prevalence",
##' @export
setMethod("plot",
signature(x = "SISe3"),
function(x, t0 = NULL, ...)
function(x, ...)
{
callNextMethod(x,
t0 = t0,
legend = expression(S[1], I[1], S[2], I[2], S[3], I[3]),
...)
}
Expand Down
3 changes: 1 addition & 2 deletions R/SISe3_sp.R
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,9 @@ setMethod("prevalence",
##' @export
setMethod("plot",
signature(x = "SISe3_sp"),
function(x, t0 = NULL, ...)
function(x, ...)
{
callNextMethod(x,
t0 = t0,
legend = expression(S[1], I[1], S[2], I[2], S[3], I[3]),
...)
}
Expand Down
4 changes: 2 additions & 2 deletions R/SISe_sp.R
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ setMethod("prevalence",
##' @export
setMethod("plot",
signature(x = "SISe_sp"),
function(x, t0 = NULL, ...)
function(x, ...)
{
callNextMethod(x, t0 = t0, legend = c("S", "I"), ...)
callNextMethod(x, legend = c("S", "I"), ...)
}
)
9 changes: 6 additions & 3 deletions R/scheduled_events.R
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ scheduled_events <- function(E = NULL,
##' @param frame.plot a logical indicating whether a box should be
##' drawn around the plot.
##' @param ... additional arguments affecting the plot.
##' @importFrom graphics plot
##' @importFrom graphics mtext
##' @keywords internal
plot_scheduled_events <- function(x,
y,
Expand Down Expand Up @@ -338,13 +340,14 @@ plot_scheduled_events <- function(x,
graphics::mtext("Time", side = 1, line = 2)
}

##' @rdname plot-methods
##' Plot \code{\linkS4class{scheduled_events}}
##'
##' @param frame.plot Draw a frame around each plot. Default is FALSE.
##' @name plot-methods
##' @aliases plot plot-methods plot,scheduled_events-method
##' @docType methods
##' @importFrom graphics par
##' @importFrom graphics plot
##' @importFrom graphics mtext
##' @importFrom graphics par
##' @importFrom stats xtabs
##' @export
setMethod("plot",
Expand Down
39 changes: 19 additions & 20 deletions R/siminf_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -418,11 +418,8 @@ setMethod("run",
##' Plot \code{\linkS4class{siminf_model}}
##'
##' @param x The \code{model} to plot
##' @param y Unused.
##' @param legend The character vector to appear in the legend.
##' @param t0 The first date of \code{x@@tspan} as a character string
##' in format 'yyyy-mm-dd'. Default is NULL which prints the x-axis
##' labels as the sequence 1:length(x@@tspan). If non-null, the labels
##' are converted to dates.
##' @param col The plotting color for each compartment. Default is
##' black.
##' @param lty The line type for each compartment. Default is the
Expand Down Expand Up @@ -451,7 +448,7 @@ setMethod("run",
##' plot(result)
setMethod("plot",
signature(x = "siminf_model"),
function(x, legend, t0 = NULL, col = NULL, lty = NULL, ...)
function(x, legend, col = NULL, lty = NULL, ...)
{
if (identical(dim(x@U), c(0L, 0L)))
stop("Please run the model first, the 'U' matrix is empty")
Expand All @@ -478,31 +475,33 @@ setMethod("plot",
lty <- seq_len(dim(m)[1])

## Plot
if (is.null(t0)) {
plot(m[1,], type = "l", ylab = "Proportion", ylim = c(0, 1),
col = col[1], lty = lty[1], ...)
if (is.null(names(x@tspan))) {
plot(m[1,], type = "l", ylab = "Proportion",
ylim = c(0, 1), col = col[1], lty = lty[1], ...)
} else {
plot(m[1,], type = "l", ylab = "Proportion", ylim = c(0, 1),
col = col[1], lty = lty[1], xaxt = "n")
}
title(xlab = "Day", outer = TRUE, line = 0)
if (!is.null(t0)) {
axis(side = 1, at = seq_len(dim(m)[2]),
labels = as.Date(x@tspan - min(x@tspan),
origin = as.Date(t0)))
plot(x = as.Date(names(x@tspan)), y = m[1,], type = "l",
ylab = "Proportion", ylim = c(0, 1), col = col[1],
lty = lty[1], ...)
}
title(xlab = "Time", outer = TRUE, line = 0)
for (i in seq_len(dim(m)[1])[-1]) {
lines(m[i, ], type = "l", lty = lty[i], col = col[i], ...)
if (is.null(names(x@tspan))) {
lines(m[i, ], type = "l", lty = lty[i],
col = col[i], ...)
} else {
lines(x = as.Date(names(x@tspan)), y = m[i, ],
type = "l", lty = lty[i], col = col[i], ...)
}
}

## Add legend below plot
par(fig = c(0, 1, 0, 1),
oma = c(0, 0, 0, 0),
mar = c(0, 0, 0, 0), new = TRUE)
plot(0, 0, type = "n", bty = "n", xaxt = "n", yaxt = "n")
graphics::legend("bottom", inset = c(0, 0),
lty = lty, col = col, bty = "n",
horiz = TRUE, legend = legend)
graphics::legend("bottom", inset = c(0, 0), lty = lty,
col = col, bty = "n", horiz = TRUE,
legend = legend)
}
)

Expand Down
32 changes: 15 additions & 17 deletions man/plot-methods.Rd

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

2 changes: 1 addition & 1 deletion tests/SISe3.R
Original file line number Diff line number Diff line change
Expand Up @@ -1506,7 +1506,7 @@ stopifnot(identical(I_observed, I_expected))
## Check SISe3 plot method
pdf_file <- tempfile(fileext = ".pdf")
pdf(pdf_file)
plot(result, t0 = "2015-01-01")
plot(result)
dev.off()
stopifnot(file.exists(pdf_file))
unlink(pdf_file)
Expand Down
2 changes: 1 addition & 1 deletion tests/SISe3_sp.R
Original file line number Diff line number Diff line change
Expand Up @@ -1629,7 +1629,7 @@ stopifnot(identical(I_observed, I_expected))
## Check SISe3_sp plot method
pdf_file <- tempfile(fileext = ".pdf")
pdf(pdf_file)
plot(result, t0 = "2015-01-01")
plot(result)
dev.off()
stopifnot(file.exists(pdf_file))
unlink(pdf_file)
Expand Down

0 comments on commit d07f68e

Please sign in to comment.