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

plot.xts: automatic ylim fails #156

Closed
DanVal80 opened this issue Oct 19, 2016 · 0 comments
Closed

plot.xts: automatic ylim fails #156

DanVal80 opened this issue Oct 19, 2016 · 0 comments
Assignees

Comments

@DanVal80
Copy link

plot.xts seems to have an issue at determining the ylim range when a timeseries has constant value for its entire duration.

Here is a reproducible example:

data=rep(100, 23); #23 observation with value 100
index=seq(as.POSIXlt("2016-06-01 01:00"), as.POSIXlt("2016-06-01 23:00"), by="60 min"); # 23 timestamps spaced hourly
data.xts<-xts(data,index);
plot(data.xts);

which results in:

Error in segments(xlim[1], y_grid_lines(get_ylim()[[2]]), xlim[2], y_grid_lines(get_ylim()[[2]]), :
cannot mix zero-length and non-zero-length coordinates

Setting the ylim manually with plot(data.xts, ylim=c(0,110)) prevents the error from happening.
I am using the last xts version from git-hub (cloned last week).

joshuaulrich added a commit that referenced this issue Jul 13, 2017
plot.xts() would error if called on an object with a series of one
constant value (e.g. .xts(rep(1,10), 1:10)) because the y-coordinates
are the same.

Create different y-coordinates that are +/-20% from the series value.

Also move the check for all ylim equal to zero outside of the if/else
block. This avoids code duplication, and therefore potential future
bugs.

Fixes #156.
@joshuaulrich joshuaulrich self-assigned this Jul 13, 2017
@joshuaulrich joshuaulrich added this to the Release 0.10-1 milestone Jul 29, 2017
bollard added a commit to bollard/xts that referenced this issue May 27, 2022
I am attempting to use `plot.xts` to plot a series which contains constant values, however I encounter the error

> Error in segments(xlim[1], y_grid_lines(get_ylim()[[2]]), xlim[2], y_grid_lines(get_ylim()[[2]]), :
> cannot mix zero-length and non-zero-length coordinates

After a little Googling, I was puzzled to see that this issue was fixed in joshuaulrich#156

With the help of `debug` I found that in my case, numerical precision issues we're causing my data to side step the check. The difference can only be seen with `sprintf`. Debug console output

```
Debug> yrange <- range(cs$Env$xdata[, 1][subset], na.rm = TRUE)
Debug> dput(yrange)
c(6e+05, 6e+05)
Debug> yrange[1L] == yrange[2L]
[1] FALSE
Debug> sprintf("%.6f", yrange[[1]])
[1] "600000.000000"
Debug> sprintf("%.9f", yrange[[1]])
[1] "600000.000000000"
Debug> sprintf("%.12f", yrange[[1]])
[1] "599999.999999999767"
Debug> sprintf("%.12f", yrange[[2]])
[1] "600000.000000000116"
Debug> isTRUE(all.equal(yrange[1], yrange[2]))
[1] TRUE
```

Proposal is to replace the strict `==` check, with `all.equal`, which includes a tolerance of `tolerance=sqrt(.Machine$double.eps)`

Thanks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants