How to plot S&P 500 and Sotheby's Time Series in one ggplot?

独自空忆成欢 提交于 2019-12-03 20:36:50

The data from Sotheby's has slightly fewer observations than the data from the S&P. If you remove the dates from S&P that do not appear in Sotheby's, then it works fine. You were doing some weird things in defining your dataframe as well, so I fixed that.

library(zoo)
library(tseries)
library(quantmod)
library(ggplot2)

# import 
env1 = new.env()
getSymbols("^GSPC", env = env1, src ="yahoo", from = as.Date("1988-06-01"),to = as.Date("2013-05-29"))
GSPC = env1$GSPC
gspc.df = data.frame(date=time(GSPC), coredata(GSPC))

env2 = new.env()
getSymbols("BID", env = env2, src ="yahoo", from = as.Date("1988-06-01"),to = as.Date("2013-05-29"))
BID = env2$BID
sothebys.df = data.frame(date=time(BID), coredata(BID))

# find which dates are in GSPC but not in Sotheby's
bad.dates <- sothebys.df$date[-which(gspc.df$date %in% sothebys.df$date)]

# remove the 'bad dates' from the dataframe so that both stocks have representative observations
# from each date
gspc.df <- gspc.df[-which(gspc.df$date %in% bad.dates),]

# verify the lengths
length(gspc.df) == length(sothebys.df)

# build the dataframe with dates and stock prices to be used in graphing
df = data.frame(Date = gspc.df$date,
                GSPC = gspc.df$GSPC.Adjusted,
                Sothebys = sothebys.df$BID.Adjusted)

# plot prices over time
ggplot(data = df, aes(x = Date)) + geom_line(aes(y = GSPC), colour = "blue") +
                                   geom_line(aes(y = Sothebys), colour = "red")

You should definitely think about looking at just the day-to-day price changes, which is a common practice when comparing stocks. The difference in trading volume between an index and a particular security is so great that you can't learn much by looking at the chart you requested. I use the normalization function below occasionally. It isn't perfect for this situation (it scales everything to a range of 0 to 1), but I'll leave it up to you to standardize your data properly. In the mean time, the following code will give you a good idea of how they compare:

NormalizeVector <- function(x) {
  NormCalc <- function(x) {
    (x - min(x, na.rm=TRUE))/(max(x,na.rm=TRUE) - min(x, na.rm=TRUE))
  }
  if (class(x) %in% c("integer", "numeric")) {
    norm.val <- NormCalc(x)
  }
  else norm.val <- x
  return(norm.val)
}

df2 = as.data.frame(lapply(df, NormalizeVector))

# plot normalized prices over time
ggplot(data = df2, aes(x = Date)) + geom_line(aes(y = GSPC), colour = "blue") +
                                   geom_line(aes(y = Sothebys), colour = "red")

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!