Skip to content
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

Add candlestick support #2053

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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,851 changes: 2,851 additions & 0 deletions GeomCustomAnn_pars

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## Improvements

* `ggplotly()` does not issue warnings with `options(warnPartialMatchArgs = TRUE)` any longer. (#2046, @bersbersbers)
* `ggplotly()` now supports `tidyquant::geom_candlestick()`. (#2053)
* `ggplotly()` does not issue warnings with `options(warnPartialMatchArgs = TRUE)` any longer. (#2046, @bersbersbers)

# 4.10.0

Expand Down
10 changes: 10 additions & 0 deletions R/layers2traces.R
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,16 @@ to_basic.GeomQuantile <- function(data, prestats_data, layout, params, p, ...){
dat
}

#' @export
to_basic.GeomRectCS <- function(data, ...){
to_basic.GeomRect(data, ...)
}
#' @export
to_basic.GeomLinerangeBC <- function(data, ...){
data[, c("xend", "y", "yend")] <- data[, c("x", "ymin", "ymax")]
to_basic.GeomSegment(data, ...)
}
Comment on lines +625 to +633
Copy link
Collaborator

@cpsievert cpsievert Nov 1, 2021

Choose a reason for hiding this comment

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

We should probably do more to come up with better hovertext (the tooltip info is basically useless at this point).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll look into this


#' @export
to_basic.default <- function(data, prestats_data, layout, params, p, ...) {
data
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/_snaps/geom-candlestick/geom-candle.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions tests/testthat/test-geom-candlestick.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
if(require(tidyquant)){
Copy link
Collaborator

Choose a reason for hiding this comment

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

Use testthat::skip_if_not_installed() instead

df <- data.frame(symbol = c("AMZN", "AMZN", "AMZN", "AMZN", "AMZN",
"AMZN", "AMZN", "AMZN", "AMZN", "AMZN", "AMZN", "AMZN", "AMZN",
"AMZN", "AMZN", "AMZN", "AMZN", "AMZN", "AMZN"), date = structure(c(17140,
17141, 17142, 17143, 17144, 17147, 17148, 17149, 17150, 17151,
17154, 17155, 17156, 17157, 17158, 17162, 17163, 17164, 17165
), class = "Date"), open = c(745, 763.98999, 764.549988, 771.869995,
770, 766.400024, 764.960022, 778.25, 766.280029, 765, 758.890015,
768.650024, 770, 768.119995, 764.549988, 763.400024, 776.25,
772.400024, 766.469971), high = c(761.48999, 768.23999, 770.419983,
773.789978, 770.25, 766.890015, 782.460022, 780.859985, 769.099976,
765.130005, 770.5, 774.390015, 771.219971, 771.210022, 766.5,
774.650024, 780, 773.400024, 767.400024), low = c(742, 757.25,
755.820007, 765.190002, 765.340027, 757.200012, 762, 762.809998,
760.309998, 754, 756.159973, 767.710022, 765.700012, 763.02002,
757.98999, 761.200012, 770.5, 760.849976, 748.280029), close = c(759.359985,
764.719971, 770.419983, 767.330017, 768.659973, 760.119995, 774.340027,
768.820007, 761, 757.77002, 766, 771.219971, 770.599976, 766.340027,
760.590027, 771.400024, 772.130005, 765.150024, 749.869995),
volume = c(4314700, 3794700, 3684900, 3189600, 2470900, 2963900,
5285300, 5454800, 3801900, 4848200, 3113200, 2703600, 2044600,
2543600, 1981600, 2638700, 3301000, 3158300, 4139400), adjusted = c(759.359985,
764.719971, 770.419983, 767.330017, 768.659973, 760.119995,
774.340027, 768.820007, 761, 757.77002, 766, 771.219971,
770.599976, 766.340027, 760.590027, 771.400024, 772.130005,
765.150024, 749.869995), stringsAsFactors = F)

test_that("candlestick gets rendered correctly", {
p <- ggplot(df, aes(x = date, y = close)) +
geom_candlestick(aes(open = open, close = close, high = high, low = low)) +
labs(title = "AMZN: New Candlestick Geom!",
x = "", y = "Closing Price") +
coord_x_date(ylim = c(700, 870))
expect_doppelganger(ggplotly(p), "geom-candle")
})
}
2 changes: 1 addition & 1 deletion tests/testthat/test-ggplot-step.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ test_that("direction vhv is translated to shape=vhv", {
L <- expect_doppelganger_built(gg.vhv, "step-gg.vhv")
expect_equivalent(length(L$data), 2)
expect_identical(L$data[[1]]$line$shape, "vhv")
})
})