Dear R users, I have already asked this in r-sig-finance (not getting a solution) but it seems to be plot-related anyway. I like to plot several financial charts to files with an identical layout according to "indicators" so the charts can be browsed quickly. quantmod::chart_Series is a function to plot a financially-related chart in the upper part and zero or more indicators below. Normally these indicators would be assigned defined values at least in some part of the subset/window to be plotted, which is fine for plot. But if all observations are NA chart_Series throws Error in plot.window(c(1, 31), c(NaN, NaN)) : need finite 'ylim' values While this outcome for plot/plot.window may be intended for most applications it is undesirable here. Getting a blank subwindow here is intended if the indicator is completely NA (at least in the subset to be plotted). This post https://stat.ethz.ch/pipermail/r-sig-finance/2020q3/015000.html suggests to generate the plot object with chart_Series and then to explicitly set x$Env$ylim[[4]] before plotting - without success. Can I tell plot/plot.window to ignore such errors and simply generate an empty region instead? Thanks Mike My minimal reproducible: library(quantmod) my_plot_function <- function () { data (sample_matrix) sample.xts <- as.xts (sample_matrix[1:50,'Close'], dateFormat="POSIXct") sample.xts <- cbind (sample.xts, NA) sample.xts[50,2] <- 0 colnames (sample.xts) <- c('Close', 'Indicator') # Indicator ta <- list ("add_TA(sample.xts[,2])") # In the range to be plotted ta is completely NA subset <- '2007-01-10::2007-01-30' plot (chart_Series (sample.xts[,1], subset=subset, TA=ta)) } my_plot_function ()
Hi Mike, Try this: plot (chart_Series (sample.xts[,1], subset=subset, TA=ta), type="n",ylim=c(minimum,maximum)) where minimum and maximum are the extremes of the plot if there were any valid values. Jim On Fri, Aug 21, 2020 at 6:32 PM Mike <mike9 at posteo.nl> wrote:> > Dear R users, > > I have already asked this in r-sig-finance (not getting a solution) > but it seems to be plot-related anyway. > > I like to plot several financial charts to files with an identical > layout according to "indicators" so the charts can be browsed > quickly. > > quantmod::chart_Series is a function to plot a financially-related > chart in the upper part and zero or more indicators below. Normally > these indicators would be assigned defined values at least in some > part of the subset/window to be plotted, which is fine for plot. But > if all observations are NA chart_Series throws > > Error in plot.window(c(1, 31), c(NaN, NaN)) : need finite 'ylim' values > > While this outcome for plot/plot.window may be intended for most > applications it is undesirable here. Getting a blank subwindow here is > intended if the indicator is completely NA (at least in the subset to > be plotted). > > This post > https://stat.ethz.ch/pipermail/r-sig-finance/2020q3/015000.html > suggests to generate the plot object with chart_Series and then to > explicitly set x$Env$ylim[[4]] before plotting - without success. > > Can I tell plot/plot.window to ignore such errors and simply generate > an empty region instead? > > Thanks > Mike > > > My minimal reproducible: > > library(quantmod) > > my_plot_function <- function () { > data (sample_matrix) > sample.xts <- as.xts (sample_matrix[1:50,'Close'], dateFormat="POSIXct") > sample.xts <- cbind (sample.xts, NA) > sample.xts[50,2] <- 0 > colnames (sample.xts) <- c('Close', 'Indicator') > > # Indicator > ta <- list ("add_TA(sample.xts[,2])") > > # In the range to be plotted ta is completely NA > subset <- '2007-01-10::2007-01-30' > > plot (chart_Series (sample.xts[,1], subset=subset, TA=ta)) > } > > my_plot_function () > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Hi Jim, on 21.08. you wrote:> Try this: > > plot (chart_Series (sample.xts[,1], subset=subset, TA=ta), > type="n",ylim=c(minimum,maximum)) > > where minimum and maximum are the extremes of the plot if there were > any valid values.I've set minimum <- 0 maximum <- 1 The error persists. But besides that, passing 'type="n"' to plot would only make sense when called once an I know there are only NAs to plot. Since I want to generate many charts in a loop I would have to check for plotable data first to decide if 'type="n"' should be passed. As a workaround for now I do something similar. I check if the range to be plotted is completely NA. If so I manipulate one of those observations (which can only be 0, 1 or NA) to an "illegal" value of 0.5. Mike
You are using a function whose help page says it is "highly experimental". In such cases it is probably better to contact the package maintainer with a feature request. The DESCRIPTION file says contact should be through one of http://www.quantmod.com https://github.com/joshuaulrich/quantmod -- David.> On Aug 20, 2020, at 10:55 AM, Mike <mike9 at posteo.nl> wrote: > > Dear R users, > > I have already asked this in r-sig-finance (not getting a solution) > but it seems to be plot-related anyway. > > I like to plot several financial charts to files with an identical > layout according to "indicators" so the charts can be browsed > quickly. > > quantmod::chart_Series is a function to plot a financially-related > chart in the upper part and zero or more indicators below. Normally > these indicators would be assigned defined values at least in some > part of the subset/window to be plotted, which is fine for plot. But > if all observations are NA chart_Series throws > > Error in plot.window(c(1, 31), c(NaN, NaN)) : need finite 'ylim' values > > While this outcome for plot/plot.window may be intended for most > applications it is undesirable here. Getting a blank subwindow here is > intended if the indicator is completely NA (at least in the subset to > be plotted). > > This post > https://stat.ethz.ch/pipermail/r-sig-finance/2020q3/015000.html > suggests to generate the plot object with chart_Series and then to > explicitly set x$Env$ylim[[4]] before plotting - without success. > > Can I tell plot/plot.window to ignore such errors and simply generate > an empty region instead? > > Thanks > Mike > > > My minimal reproducible: > > library(quantmod) > > my_plot_function <- function () { > data (sample_matrix) > sample.xts <- as.xts (sample_matrix[1:50,'Close'], dateFormat="POSIXct") > sample.xts <- cbind (sample.xts, NA) > sample.xts[50,2] <- 0 > colnames (sample.xts) <- c('Close', 'Indicator') > > # Indicator > ta <- list ("add_TA(sample.xts[,2])") > > # In the range to be plotted ta is completely NA > subset <- '2007-01-10::2007-01-30' > > plot (chart_Series (sample.xts[,1], subset=subset, TA=ta)) > } > > my_plot_function () > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.