diff --git a/DESCRIPTION b/DESCRIPTION index 034551ba81..8b712c64ab 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: plotly Type: Package Title: Interactive, publication-quality graphs online. -Version: 0.5.16 +Version: 0.5.17 Authors@R: c(person("Chris", "Parmer", role = c("aut", "cre"), email = "chris@plot.ly"), person("Scott", "Chamberlain", role = "aut", diff --git a/NEWS b/NEWS index 90cbe5bf52..90ae1f4812 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,7 @@ +0.5.17 -- 30 December 2014. + +Support date-time binning in histograms. + 0.5.16 -- 29 December 2014. Support colour aesthetic in geom_text(). diff --git a/R/plotly-package.r b/R/plotly-package.r index c67691cc33..136a643a35 100644 --- a/R/plotly-package.r +++ b/R/plotly-package.r @@ -7,7 +7,7 @@ #' \itemize{ #' \item Package: plotly #' \item Type: Package -#' \item Version: 0.5.16 +#' \item Version: 0.5.17 #' \item Date: 2014-03-07 #' \item License: MIT #' } diff --git a/R/plotly.R b/R/plotly.R index d75a1a2df0..6cc132cab3 100644 --- a/R/plotly.R +++ b/R/plotly.R @@ -82,7 +82,7 @@ For more help, see https://plot.ly/R or contact .") # public attributes/methods that the user has access to pub <- list(username=username, key=key, filename="from api", fileopt=NULL, - version="0.5.16") + version="0.5.17") priv <- list() pub$makecall <- function(args, kwargs, origin) { diff --git a/R/trace_generation.R b/R/trace_generation.R index 3fffb5d99f..ca7b8b94ca 100644 --- a/R/trace_generation.R +++ b/R/trace_generation.R @@ -456,6 +456,13 @@ geom2trace <- list( L$xbins=list(start=params$xstart, end=params$xend, size=params$binwidth) + if (inherits(data$x.name, "POSIXt")) { + # Convert seconds into milliseconds + L$xbins <- lapply(L$xbins, function(x) x * 1000) + } else if (inherits(data$x.name, "Date")) { + # Convert days into milliseconds + L$xbins <- lapply(L$xbins, function(x) x * 24 * 60 * 60 * 1000) + } } } else { L$y <- data$y diff --git a/tests/testthat/test-ggplot-histogram.R b/tests/testthat/test-ggplot-histogram.R index 42b236bcb9..1939de2559 100644 --- a/tests/testthat/test-ggplot-histogram.R +++ b/tests/testthat/test-ggplot-histogram.R @@ -11,7 +11,7 @@ test_that("default position is translated to barmode=stack", { expect_identical(L$kwargs$layout$xaxis$type, "category") expect_identical(L[[1]]$type, "histogram") expect_true(L[[1]]$x[1] %in% c("CDN", "MEX", "USA")) - + save_outputs(hist, "histogram-barmodestack") }) @@ -27,7 +27,7 @@ test_that("binwidth is translated into xbins.size", { L <- gg2list(hist) expect_equal(length(L), 2) expect_equal(L[[1]]$xbins$size, bw) - + save_outputs(hist, "histogram-binwidth") }) @@ -44,6 +44,135 @@ test_that("dates work well with histograms", { expect_identical(L$kwargs$layout$xaxis$type, "date") expect_identical(L[[1]]$x[1], "2012-01-01 00:00:00") expect_identical(L[[1]]$x[2], "2012-02-01 00:00:00") - + save_outputs(hist, "histogram-dates") }) + +# Non-numeric (date) data, specifying binwidth +killed <- data.frame(date=c("2014-12-24", + "2014-12-23", + "2014-12-22", + "2014-12-22", + "2014-12-22", + "2014-12-18", + "2014-12-22", + "2014-12-21", + "2014-12-21", + "2014-12-21", + "2014-12-20", + "2014-12-19", + "2014-12-18", + "2014-12-18", + "2014-12-17", + "2014-12-17", + "2013-12-20", + "2014-04-25", + "2014-12-01", + "2014-12-17", + "2014-12-17", + "2014-12-17", + "2014-12-17", + "2014-12-17", + "2014-12-17", + "2014-12-15", + "2014-12-15", + "2014-12-15", + "2014-12-14", + "2014-12-14", + "2014-12-14", + "2014-12-13", + "2014-12-13", + "2013-05-18", + "2014-12-13", + "2014-12-12", + "2014-12-12", + "2014-12-11", + "2014-12-10", + "2014-12-10", + "2014-12-10", + "2014-12-10", + "2014-12-09", + "2014-12-09", + "2014-12-09", + "2014-12-09", + "2014-12-08", + "2014-12-08", + "2014-12-08", + "2014-12-07", + "2014-12-08", + "2014-12-07", + "2014-05-01", + "2014-12-05", + "2014-12-05", + "2014-12-05", + "2014-12-04", + "2014-12-04", + "2014-12-04", + "2014-07-13", + "2014-12-02", + "2014-12-03", + "2014-12-03", + "2014-12-02", + "2014-12-02", + "2014-12-01", + "2014-12-01", + "2014-12-01", + "2014-04-02", + "2014-11-30", + "2014-11-30", + "2014-11-29", + "2014-11-28", + "2014-11-29", + "2014-11-27", + "2014-11-28", + "2014-11-27", + "2014-11-26", + "2014-11-25", + "2014-11-26", + "2014-11-25", + "2014-11-25", + "2014-11-24", + "2014-11-24", + "2014-11-23", + "2014-11-23", + "2014-11-24", + "2014-11-23", + "2014-11-22", + "2014-11-23", + "2014-11-22", + "2014-11-22", + "2014-11-21", + "2014-11-21", + "2014-11-21", + "2014-11-20", + "2014-11-20", + "2014-11-20", + "2014-11-19")) + +test_that("datetime binning for class POSIXt works in histograms", { + kP <- killed + kP$date <- as.POSIXlt(kP$date) + histP <- ggplot(kP, aes(x=date)) + geom_histogram(binwidth=2592000) + + L <- gg2list(histP) + expect_equal(length(L), 2) # 1 trace + layout + expect_false(L[[1]]$autobinx) # No auto-binning + expect_identical(L$kwargs$layout$xaxis$type, "date") + expect_equal(L[[1]]$xbins$size, 2592000000) # Bin size in ms + + save_outputs(histP, "histogram-POSIXt-bins") +}) + +test_that("datetime binning for class Date works in histograms", { + kD <- killed + kD$date <- as.Date(kD$date) + histD <- ggplot(kD, aes(x=date)) + geom_histogram(binwidth=30) + + L <- gg2list(histD) + expect_equal(length(L), 2) # 1 trace + layout + expect_false(L[[1]]$autobinx) # No auto-binning + expect_identical(L$kwargs$layout$xaxis$type, "date") + expect_equal(L[[1]]$xbins$size, 2.592e+09) # Number of ms in 30 days + + save_outputs(histD, "histogram-Date-bins") +})