2
votes

I have some stock hourly data that I want to plot using ggplot2; I will do TSA on it and would like to add layer to the original plot afterwards.

an example of my data looks like below:

                   Date Volume
1   2018-03-01 10:30:00 143432
2   2018-03-01 11:30:00  93522
3   2018-03-01 12:30:00 152178
4   2018-03-01 13:30:00 117424
5   2018-03-01 14:30:00 268167
6   2018-03-01 15:30:00 245504
7   2018-03-01 15:59:00 288977
8   2018-03-02 10:30:00 230484
9   2018-03-02 11:30:00 265244
10  2018-03-02 12:30:00 183313
11  2018-03-02 13:30:00 130850
12  2018-03-02 14:30:00 139846
13  2018-03-02 15:30:00 257797
14  2018-03-02 15:59:00 261628
15  2018-03-05 10:30:00 140620
16  2018-03-05 11:30:00 171228
17  2018-03-05 12:30:00 118685
18  2018-03-05 13:30:00 107209
19  2018-03-05 14:30:00 116918
20  2018-03-05 15:30:00 225035

I have created a zoo object (I Know we could also use xts or irts):

temp <- read.csv('somefile.csv')
zt <- zoo(x = temp, order.by = as.POSIXct(temp$Date))

I then tried creating a ggplot object by:

a <- ggplot(data = zt, aes(x = index(zt), y = coredata(zt)))
a + geom_line()

the plot looked like below:

enter image description here

Apparently it is not correctly scaled and the plot is very crammed up.

How do I correctly plot it using ggplot2?

I know from some help that I can use chart_series from quantmod to plot it but I am not sure that function is designed for time series analysis (probably more for financial data plotting) and therefore may not be flexible enough for adding on layers.

EDIT

typically I would like to have my plot looking like this:

library(xts)
library(quantmod)
xxt <- xts(x = temp$Volume, order.by = as.POSIXct(temp$Date))
chart_series(xxt)

enter image description here

I don't need all the aesthetics, just the way it plotted is what I wanted. it feels like the quantmod chart has removed all the gaps.

1
Looks right to me. What did you expect it to look like You have gaps in the data (weekends?) causing the long slopes in the figureRichard Telford
@RichardTelford I have edited the question with a plot from quantmod; it is basically what I wanted, but just the data plot itself; it's easy to tell the difference.stucash
You basically need to do (by hand, I fear) what chartSeries as well as plot.xts (with the option @JoshuaUlrich showed you) do: plot with just the data index as the x axis but then use the time stamps as the labels. Not trivial. Also not sure if there is a ggplot2 extension that does it.Dirk Eddelbuettel

1 Answers

4
votes

I tried for 45 minutes to get ggplot to play nicely and came up empty. I'll talk to the 'zoo' team about adding an option autoplot.zoo() so you can just call autoplot(zt, new.option = TRUE) to generate the plot you want.


You are correct that quantmod::chart_Series() is primarily for financial time series. It looks like you are familiar with zoo and xts, which are data classes that support any time series.

You can use plot.xts(). Be sure to set observation.based = TRUE so that the x-axis will have equal amount of space between observations, regardless of the amount of time between them.

# reproducible data
x <- structure(c(143432L, 93522L, 152178L, 117424L, 268167L, 245504L, 288977L,
230484L, 265244L, 183313L, 130850L, 139846L, 257797L, 261628L, 140620L, 171228L,
118685L, 107209L, 116918L, 225035L), .Dim = c(20L, 1L),
index = structure(c(1519921800, 1519925400, 1519929000, 1519932600, 1519936200,
1519939800, 1519941540, 1520008200, 1520011800, 1520015400, 1520019000,
1520022600, 1520026200, 1520027940, 1520267400, 1520271000, 1520274600,
1520278200, 1520281800, 1520285400), tzone = "", tclass = c("POSIXct", "POSIXt")),
class = c("xts", "zoo"), .indexCLASS = c("POSIXct", "POSIXt"),
tclass = c("POSIXct", "POSIXt"), .indexTZ = "", tzone = "")

Now call plot() on the xts object.

plot(x, observation.based = TRUE, major.ticks = "hours")

enter image description here