joshuaulrich / xts

Extensible time series class that provides uniform handling of many R time series classes by extending zoo.
http://joshuaulrich.github.io/xts/
GNU General Public License v2.0
219 stars 70 forks source link

`addEventLines()` for multiple panels #420

Closed ethanbsmith closed 1 month ago

ethanbsmith commented 1 month ago

Description

addEventLines() current only draws on a single panel or adds a new panel. this is cumbersome when needing to add the same even to multiple panels. eg, see attached image

Expected behavior

would be nice if the on parameter either supported a vector of panels, or maybe a special value (like NULL) which would draw the event across all panels

Minimal, reproducible example

image

joshuaulrich commented 1 month ago

Thanks for the report. Here's an example.

library(xts)
data(sample_matrix)
x <- as.xts(sample_matrix)
plot(x, multi.panel = TRUE)
addEventLines(xts("a", index(x)[10]), pos = 4)

Setting on to a vector of panel numbers errors. This also fails for addLegend().

addEventLines(xts("a", index(x)[10]), pos = 4, on = 1:4)
addLegend(on = 1:4)
joshuaulrich commented 1 month ago

@ethanbsmith please see if that patch works for you.

ethanbsmith commented 1 month ago

confirmed, works w/ on as a vector. tested on r 4.3.3 basic example crashes R session on 4.4.0:

> library(xts)
Loading required package: zoo

Attaching package: ‘zoo’

The following objects are masked from ‘package:base’:

    as.Date, as.Date.numeric

> data(sample_matrix)
> x <- as.xts(sample_matrix)
> 
> sessionInfo()
R version 4.4.0 (2024-04-24 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 22631)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8    LC_MONETARY=English_United States.utf8
[4] LC_NUMERIC=C                           LC_TIME=English_United States.utf8    

time zone: America/Denver
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] xts_0.13.2.2 zoo_1.8-12  

loaded via a namespace (and not attached):
[1] compiler_4.4.0 tools_4.4.0    grid_4.4.0     lattice_0.22-6

> plot(x, multi.panel = TRUE) #<- crashes sesion
ethanbsmith commented 1 month ago

for the record, this crash under 4.4.0 was a build issue on my machine. works fine