0

Following data can be either a tibble or data.table:

  datetime                volume
  <dttm>                   <dbl>
  2020-08-25 09:30:00.000      0
  2020-08-25 09:30:12.000    107
  2020-08-25 09:30:50.000    221
  2020-08-25 09:30:50.000    132
  2020-08-25 09:30:50.000    148
  2020-08-25 09:30:50.000    100
  2020-08-25 09:30:50.000    100
  2020-08-25 09:30:58.000    100
  2020-08-25 09:31:56.000    157
  2020-08-25 09:32:36.000    288
  2020-08-25 09:32:36.000    100
  2020-08-25 09:33:10.000    235
  2020-08-25 09:33:23.000    182
  2020-08-25 09:33:44.000    218
  2020-08-25 09:33:44.000    179
  2020-08-25 09:34:18.000    318
  2020-08-25 09:34:27.000    101
  2020-08-25 09:34:27.000    157
  2020-08-25 09:34:27.000    200
  2020-08-25 09:34:27.000    114

I have converted above data into a xts object and wanted to call the plotting function from Rscript, as a command line utility.

Here's how I transform my data into xts:

data %>% as.xts() -> testdata1

Above line of code does not make a different time series than:

data %>% as.xts() -> testdata2
identical(testdata1, testdata2)
# output: TRUE

on SO there were 2 possible methods to go about this:

  # first option
  x11()  # linux calls x11()
  plot(x = data, 
       observation.based = TRUE, 
       major.ticks = "seconds", 
       main = "Test Plot")
  prompt <- "press any key to close the plot"
  capture <- tcltk::tk_messageBox(message = prompt)
  
  # second option
  png("test.png")
  plot(x = data, 
       observation.based = TRUE, 
       major.ticks = "seconds", 
       main = "Test Plot")
  dev.off()
  browseURL("test.png)

Neither of above worked for xts object plotting. it worked for plain data.frame, data.table and ggplot.

Anyone has tried plotting xts to the new device, from command line?

stucash
  • 1,078
  • 1
  • 12
  • 23
  • Both plotting methods worked with the following example: `data(AirPassengers); xts1 <- as.xts(AirPassengers)`. I think your error is with how you convert to `xts` object – astrofunkswag Sep 11 '20 at 17:08
  • @astrofunkswag not sure that's right; I was `as.xts` as well; and more importantly, `AirPassengers` has different resolution than mine, i.e. mine is irregular time series in seconds, `AirPassengers` was equidistant yearly data; if you could reproduce the plot using my example data that'd surely mean something wrong with my way of plotting. But yes I should add how I transform my data. – stucash Sep 12 '20 at 02:50
  • Maybe the resolution is a problem though. I just tried equidistant hourly data; it didn't work for me either. – stucash Sep 12 '20 at 03:11

1 Answers1

0

I have had the same issue, but referencing this question helped solve it for me.

Try assigning the returned plot object and then printing. e.g.

  png("test.png")
  plt <- plot(x = data, 
              observation.based = TRUE, 
              major.ticks = "seconds", 
              main = "Test Plot")
  print(plt)
  dev.off()
  browseURL("test.png)
billelev
  • 369
  • 2
  • 13