R 如何绘制包含4只股票收益的XTS?

R 如何绘制包含4只股票收益的XTS?,r,ggplot2,xts,quantmod,R,Ggplot2,Xts,Quantmod,这是我的密码 library(ggplot2) library(quantmod) library(xts) library(magrittr) start <- as.Date("2012-01-01") end <- as.Date("2019-10-01") getSymbols(c("AAPL","MSFT", "GOOG","INTC","AMD"), src = "yahoo", from = start, to = end) stocks <- as.xts(da

这是我的密码

library(ggplot2)
library(quantmod)
library(xts)
library(magrittr)
start <- as.Date("2012-01-01")
end <- as.Date("2019-10-01")
getSymbols(c("AAPL","MSFT", "GOOG","INTC","AMD"), src = "yahoo", from = start, to = end)
stocks <- as.xts(data.frame(AAPL = AAPL[, "AAPL.Close"], MSFT = MSFT[, "MSFT.Close"], 
                            GOOG = GOOG[, "GOOG.Close"],INTC = INTC[, "INTC.Close"],AMD = AMD[, "AMD.Close"]))
head(stocks)
stock_change = stocks %>% log %>% diff
head(stock_change)
库(ggplot2)
图书馆(quantmod)
图书馆(xts)
图书馆(magrittr)

start如果要使用ggplot2,可能需要使用tidyquant。这将以整洁的格式包装xts和quantmod,以便与dplyr和ggplot2一起使用。请参阅下面tidyquant渐晕图中的简单示例。有关图表的更多信息,请访问

库(tidyquant)
图书馆(GG2)

start如果要使用ggplot2,可能需要使用tidyquant。这将以整洁的格式包装xts和quantmod,以便与dplyr和ggplot2一起使用。请参阅下面tidyquant渐晕图中的简单示例。有关图表的更多信息,请访问

库(tidyquant)
图书馆(GG2)
开始
library(tidyquant)
library(ggplot2)

start <- as.Date("2012-01-01")
end <- as.Date("2019-10-01")

# default source is yahoo. Read help to use other sources.
stocks <- tq_get(c("AAPL","MSFT", "GOOG","INTC","AMD"), from = start, to = end)
stocks %>%
  ggplot(aes(x = date, y = close, group = symbol)) +
  geom_candlestick(aes(open = open, high = high, low = low, close = close)) +
  labs(title = "Stocks Candlestick Chart", 
       subtitle = "Experimenting with Mulitple Stocks",
       y = "Closing Price", x = "") + 
  coord_x_date(xlim = c(start, end)) +
  facet_wrap(~ symbol, ncol = 2, scale = "free_y") + 
  theme_tq()