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:
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)
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.
quantmod
; it is basically what I wanted, but just the data plot itself; it's easy to tell the difference. – stucashchartSeries
as well asplot.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 aggplot2
extension that does it. – Dirk Eddelbuettel