Plotting Stock Price Time Series in R with Quantmod
Summary
The document concerns plotting normalized Oracle stock prices over time in R and describes problems with date parsing, weekly grouping, and reproducing a particular chart. The response recommends using the quantmod package to retrieve price history and PerformanceAnalytics to create a time-series chart. Its example charts the opening-price series, providing a simpler route to a basic stock-price visualization than the questioner’s custom grouping and plotting code.
The answer does not diagnose the reported dates extending into the wrong years or explain why the custom plots differ from the referenced chart. It explicitly says the desired target plot is unclear, so the recommendation is a general charting approach rather than a complete fix. The document provides no analysis of Oracle returns, normalized-price interpretation, or trading signal. Its practical contribution is limited to identifying R packages and a built-in chart function; users still need to confirm that dates are parsed with the correct format and that the chart matches their intended view.
Key ideas
- Quantmod can retrieve historical equity prices in R.
- PerformanceAnalytics provides a time-series chart function suitable for plotting price data.
- A package-based chart offers a straightforward alternative to manually grouping observations by week.
- The answer does not resolve the date parsing issue or specify the intended chart, limiting how directly it addresses the plotting problem.
Tags
Full text
# How to plot time series for stock data using R
# How to plot time series for stock data using R
We have a dataset which has open,high,low and close values. We have normalized the data and trying to plot normalized open values against Date.
The dataset can be found at http://finance.yahoo.com/quote/ORCL/history?period1=1323475200&period2=1481328000&interval=1d&filter=history&frequency=1d
We tried to replicate the graphs from https://stats.stackexchange.com/questions/39279/how-to-plot-20-years-of-daily-data-in-time-series
The output is not as expected.
Code:
```
oracle$date <- as.Date(oracle$Date,"%y-%m-%d")
print(oracle$date)
```
Issue 1: It prints dates up to 2027 even when have values from 2011-2016.
```
oracle$week <-(as.integer(oracle$date) + 3) %/% 7
print (oracle$week)
oracle$week <- as.Date(oracle$week * 7 - 3, as.Date("2011-12-12", "%Y-%m-%d"))
print(oracle$week)
par(mfrow=c(1,2))
plot(as.factor(unclass(oracle$Date[1:1259])), oracle$NO[1:1259], type="l",
main="Original Plot: Inset", xlab="Factor code")
plot(oracle$Date[1:1259], oracle$NO[1:1259], type="n", ylab="Price",
main="Oracle Opening Prices")
```
Graphs are not displayed as shown in answer (8) of https://stats.stackexchange.com/questions/39279/how-to-plot-20-years-of-daily-data-in-time-series
It is as displayed below:
```
tmp <- by(oracle[1:1259,], oracle$week[1:1259], function(x) lines(x$Date, x$NO, lwd=2))
print(tmp)
par(mfrow=c(1,1))
colors <- terrain.colors(52)
plot(oracle$Date, oracle$NO, type="n", main="Oracle Opening Prices")
tmp <- by(oracle, oracle$week,
function(x) lines(x$date, x$Open, col=colors[x$week %% 52 + 1]))
print(tmp)
```
## Answer by Forgottenscience (score 2)
https://quant.stackexchange.com/a/31468
This is commonly done in R using the Quantmod package and getSymbols wrapper. A good built-in chart setup is from the PerformanceAnalytics package.
```
require(quantmod)
require(PerformanceAnalytics)
getSymbols("ORCL", from = "1996-01-01")
chart.TimeSeries(ORCL[,1], main = "Oracle Opening Prices")
```
However, since I am not entirely sure what answer (8) mean (the one with 8 upvotes?), I cannot determine exactly what plot you want.Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.