Skip to content

Commit 50f70b8

Browse files
committed
date conversion
1 parent da64433 commit 50f70b8

File tree

2 files changed

+44
-16
lines changed

2 files changed

+44
-16
lines changed

R/ggplotly.R

+27-16
Original file line numberDiff line numberDiff line change
@@ -234,17 +234,20 @@ gg2list <- function(p){
234234
## what ggplot_build gives us).
235235
misc <- list()
236236
for(a in c("fill", "colour", "x", "y")){
237-
fun.name <- sprintf("scale_%s_continuous", a)
238-
fun <- get(fun.name)
239-
misc$is.continuous[[a]] <- tryCatch({
240-
suppressMessages({
241-
with.scale <- p+fun()
237+
for(data.type in c("continuous", "date", "datetime", "discrete")){
238+
fun.name <- sprintf("scale_%s_%s", a, data.type)
239+
misc.name <- paste0("is.", data.type)
240+
misc[[misc.name]][[a]] <- tryCatch({
241+
fun <- get(fun.name)
242+
suppressMessages({
243+
with.scale <- p+fun()
244+
})
245+
ggplot2::ggplot_build(with.scale)
246+
TRUE
247+
}, error=function(e){
248+
FALSE
242249
})
243-
ggplot2::ggplot_build(with.scale)
244-
TRUE
245-
}, error=function(e){
246-
FALSE
247-
})
250+
}
248251
}
249252

250253
## scales are needed for legend ordering.
@@ -329,8 +332,12 @@ gg2list <- function(p){
329332
ax.list$titlefont <- theme2font(title.text)
330333
ax.list$type <- if(misc$is.continuous[[xy]]){
331334
"linear"
332-
}else{## TODO: time scales?
335+
}else if(misc$is.discrete[[xy]]){
333336
"category"
337+
}else if(misc$is.date[[xy]] || misc$is.datetime[[xy]]){
338+
"date"
339+
}else{
340+
stop("unrecognized data type for ", xy, " axis")
334341
}
335342
## Lines drawn around the plot border:
336343
ax.list$showline <- ifelse(is.blank("panel.border"), FALSE, TRUE)
@@ -373,15 +380,19 @@ layer2traces <- function(l, d, misc){
373380
## needed for when group, etc. is an expression.
374381
g$aes <- sapply(l$mapping, function(k) as.character(as.expression(k)))
375382

376-
## For factors on the axes, we should take the values from the
377-
## original data.
383+
## For non-numeric data on the axes, we should take the values from
384+
## the original data.
378385
for(axis.name in c("x", "y")){
379386
if(!misc$is.continuous[[axis.name]]){
380387
aes.names <- paste0(axis.name, c("", "end", "min", "max"))
381388
aes.used <- aes.names[aes.names %in% names(g$aes)]
382-
if(length(aes.used)){
383-
col.used <- g$aes[aes.used]
384-
g$data[aes.used] <- l$data[col.used]
389+
for(a in aes.used){
390+
col.name <- g$aes[aes.used]
391+
data.vec <- l$data[[col.name]]
392+
if(inherits(data.vec, "POSIXt")){
393+
data.vec <- strftime(data.vec, "%Y-%m-%d %H:%M:%S")
394+
}
395+
g$data[[a]] <- data.vec
385396
}
386397
}
387398
}

inst/tests/test-ggplot-date.R

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
context("date")
2+
3+
test_that("datetimes are converted to e.g. 2013-01-02 05:00:00", {
4+
in.str <- c("17 Mar 1983 06:33:44 AM",
5+
"17 Mar 1984 01:59:55 PM")
6+
time.obj <- strptime(in.str, "%d %b %Y %I:%M:%S %p")
7+
out.str <- strftime(time.obj, "%Y-%m-%d %H:%M:%S")
8+
df <- rbind(data.frame(who="me", time.obj, dollars=c(1.1, 5.6)),
9+
data.frame(who="you", time.obj, dollars=c(10.2, 0)))
10+
gg <- qplot(time.obj, dollars, data=df, color=who, geom="line")
11+
info <- gg2list(gg)
12+
expect_equal(length(info), 3)
13+
expect_identical(info$kwargs$layout$xaxis$type, "date")
14+
for(trace in info[1:2]){
15+
expect_identical(trace$x, out.str)
16+
}
17+
})

0 commit comments

Comments
 (0)