Skip to content

Support date-time binning in histograms #151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 30, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]"),
person("Scott", "Chamberlain", role = "aut",
Expand Down
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -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().
Expand Down
2 changes: 1 addition & 1 deletion R/plotly-package.r
Original file line number Diff line number Diff line change
Expand Up @@ -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
#' }
Expand Down
2 changes: 1 addition & 1 deletion R/plotly.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ For more help, see https://plot.ly/R or contact <[email protected]>.")

# 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) {
Expand Down
7 changes: 7 additions & 0 deletions R/trace_generation.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
135 changes: 132 additions & 3 deletions tests/testthat/test-ggplot-histogram.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"))

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whitespace?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, actually: indentation. This is the convention / automatically added in RStudio.
Both conventions (A: preserving indentation within a block and B: no indentation on empty lines) make sense, I once found a great thread on the topic on StackOverflow... Let me see if I can find it again.

save_outputs(hist, "histogram-barmodestack")
})

Expand All @@ -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")
})

Expand All @@ -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")
})