Because the problem is common to many statistical software environments, let's discuss it here on Cross Validated rather than migrating it to an R-specific forum (such as StackOverflow).
The real issue is that Date
is treated as a factor--a discrete variable--and so the lines are not being connected properly. (Nor are the points being plotted perfectly accurately in the horizontal direction.)
To make the righthand plot, the Date
field was converted from a factor to an actual date, each week was identified with a simple calculation (breaking the weeks between Saturday and Sunday) and the lines were interrupted over weekends by looping over the weeks:
oracle$date <- as.Date(oracle$Date)
oracle$week.num <- (as.integer(oracle$date) + 3) %/% 7
oracle$week <- as.Date(oracle$week.num * 7 - 3, as.Date("1970-01-01", "%Y-%m-%d"))
par(mfrow=c(1,2))
plot(as.factor(unclass(oracle$Date[1:120])), oracle$Open[1:120], type="l",
main="Original Plot: Inset", xlab="Factor code")
plot(oracle$date[1:120], oracle$Open[1:120], type="n", ylab="Price",
main="Oracle Opening Prices")
tmp <- by(oracle[1:120,], oracle$week[1:120], function(x) lines(x$date, x$Open, lwd=2))
(A date equivalent of each week, giving the Monday of that week, was also stored in the oracle
dataframe because it can be useful for plotting weekly aggregated data.)
The original intention can be achieved simply by emulating the last line to display all the data. To add some information about seasonal behavior, the following plot varies the color by week throughout each calendar year:
par(mfrow=c(1,1))
colors <- terrain.colors(52)
plot(oracle$date, oracle$Open, type="n", main="Oracle Opening Prices")
tmp <- by(oracle, oracle$week,
function(x) lines(x$date, x$Open, col=colors[x$week.num %% 52 + 1]))
R
one way to add smooth lines isloess
. I am on my way out, but try ?loess in R and, if you have trouble, edit your post and someone will certainly be able to help you. There are other smoothing methods, too, but I think loess is a good default. $\endgroup$