Skip to content

Commit

Permalink
set ggplot2 datetime to UTC; fix fit plot
Browse files Browse the repository at this point in the history
This will fix #99

 - ggplot2 timezone set to UTC for POSIXt plotting
 - match the data type in `add_incidence_fit()` of the predictors and
   the incoming ggplot2 date column. This is necessary because of the
   change in #92 where fit data were changed to Date to give more
   sensible coefficients.
 - update POSIXt test plot that was accidentally saved from the KST time
   zone
  • Loading branch information
zkamvar committed Jan 9, 2019
1 parent e2882aa commit d9bc202
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
8 changes: 5 additions & 3 deletions R/incidence.R
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ incidence.POSIXt <- function(dates, interval = 1L, standard = TRUE, groups = NUL
last_date = NULL, ...) {
## make sure input can be used

dots <- check_dots(list(...), names(formals(incidence.Date)))
dots <- check_dots(list(...), names(formals(incidence.Date)))
dates <- check_dates(dates)

ret <- incidence(as.Date(dates),
Expand All @@ -266,8 +266,10 @@ incidence.POSIXt <- function(dates, interval = 1L, standard = TRUE, groups = NUL
last_date = last_date,
...)

f <- if (inherits(dates, "POSIXct")) as.POSIXct else as.POSIXlt
ret$dates <- f(ret$dates)
ret$dates <- as.POSIXlt(ret$dates)
if (inherits(dates, "POSIXct")) {
ret$dates <- as.POSIXct(ret$dates)
}
ret
}

17 changes: 15 additions & 2 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,10 @@ plot.incidence <- function(x, ..., fit = NULL, stack = is.null(fit),
out <- out + ggplot2::scale_x_date(breaks = breaks,
date_breaks = db
)
} else if (inherits(x$dates, "POSIXct")) {
} else if (inherits(x$dates, "POSIXt")) {
out <- out + ggplot2::scale_x_datetime(breaks = breaks,
date_breaks = db
date_breaks = db,
timezone = "UTC"
)
} else {
out <- out + ggplot2::scale_x_continuous(breaks = breaks)
Expand Down Expand Up @@ -331,6 +332,18 @@ add_incidence_fit <- function(p, x, col_pal = incidence_pal1){
}
df <- get_info(x, "pred")

# In the case that the incidence object is of the type POSIXt, the data from
# the fit object must be presented as POSIXt or ggplot2 will throw a fit.

if (inherits(p$data$dates, "POSIXt")) {
# I add half a day here because any fractional days (0.5) will be thrown out
# on conversion and I'm not quite sure why that is
df$dates <- as.POSIXlt(df$dates) + 43200 # adding half a day
if (inherits(p$data$dates, "POSIXct")) {
df$dates <- as.POSIXct(df$dates)
}
}

out <- suppressMessages(
p + ggplot2::geom_line(
data = df,
Expand Down
28 changes: 14 additions & 14 deletions tests/figs/test-plotting/incidence-plot-from-posixct-data.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d9bc202

Please sign in to comment.