如何合并由 autoplot 创建的两个图形?
How to combine two graphs created by autoplot?
让我们考虑两种金融资产(苹果和黄金):
start <- as.Date("2013-01-01")
end <- as.Date("2016-10-01")
library(quantmod)
library(ggplot2)
getSymbols("AAPL", src = "yahoo", from = start, to = end)
getSymbols("GOLD", src = "yahoo", from = start, to = end)
让我们看看苹果和黄金的情节是怎样的:
autoplot(Cl(AAPL))
autoplot(Cl(GOLD))
但是我不知道如何将它们都放在一张图表上。我试图寻找解决方案,但其中 none 个正在使用 autoplot()
函数。在一个坐标系上有两个上述图形是否可行?
我正在寻找类似的东西,但它是用 autoplot()
创建的
ggplot()+geom_line(aes(x = 1:945, y = Cl(AAPL)))+geom_line(aes(x = 1:945, y = Cl(GOLD)))
使用例如patchwork
这可以这样实现:
start <- as.Date("2013-01-01")
end <- as.Date("2016-10-01")
library(quantmod)
library(ggplot2)
getSymbols("AAPL", src = "yahoo", from = start, to = end)
#> [1] "AAPL"
getSymbols("GOLD", src = "yahoo", from = start, to = end)
#> [1] "GOLD"
library(patchwork)
p1 <- autoplot(Cl(AAPL))
p2 <- autoplot(Cl(GOLD))
p1 + p2
编辑 按照 zoo::autopilot.zoo
文档中的示例,您可以像这样使用 ggplot2
手动制作绘图:
ggplot(mapping = aes(x = Index, y = Value)) +
geom_line(data = fortify(Cl(AAPL), melt = TRUE), aes(color = "AAPL")) +
geom_line(data = fortify(Cl(GOLD), melt = TRUE), aes(color = "GOLD")) +
xlab("Index") + ylab("x")
使用 cbind
创建一个组合的 xts/zoo 对象,然后 autoplot
它。如果您希望每个都在自己的面板中,请省略 facet=NULL
。如果您想先对价格进行标准化,请使用 scale(closes)
代替 closes
。 autoplot
行的输出显示在末尾。
closes <- cbind(Cl(AAPL), Cl(GOLD))
autoplot(closes, facet = NULL)
很多股票
如果有很多股票,我们不想单独 cbind
它们,那么我们可以使用此代码创建 closes
。
tickers <- c("AAPL", "GOLD")
getSymbols(tickers, from = start, to = end, env = stocks <- new.env())
closes <- do.call("cbind", eapply(stocks, Cl))
或者我们可以使用此代码,假设上面的 tickers
:
getSymbols(tickers, from = start, to = end)
closes <- do.call("cbind", lapply(mget(tickers), Cl))
输出
让我们考虑两种金融资产(苹果和黄金):
start <- as.Date("2013-01-01")
end <- as.Date("2016-10-01")
library(quantmod)
library(ggplot2)
getSymbols("AAPL", src = "yahoo", from = start, to = end)
getSymbols("GOLD", src = "yahoo", from = start, to = end)
让我们看看苹果和黄金的情节是怎样的:
autoplot(Cl(AAPL))
autoplot(Cl(GOLD))
但是我不知道如何将它们都放在一张图表上。我试图寻找解决方案,但其中 none 个正在使用 autoplot()
函数。在一个坐标系上有两个上述图形是否可行?
我正在寻找类似的东西,但它是用 autoplot()
ggplot()+geom_line(aes(x = 1:945, y = Cl(AAPL)))+geom_line(aes(x = 1:945, y = Cl(GOLD)))
使用例如patchwork
这可以这样实现:
start <- as.Date("2013-01-01")
end <- as.Date("2016-10-01")
library(quantmod)
library(ggplot2)
getSymbols("AAPL", src = "yahoo", from = start, to = end)
#> [1] "AAPL"
getSymbols("GOLD", src = "yahoo", from = start, to = end)
#> [1] "GOLD"
library(patchwork)
p1 <- autoplot(Cl(AAPL))
p2 <- autoplot(Cl(GOLD))
p1 + p2
编辑 按照 zoo::autopilot.zoo
文档中的示例,您可以像这样使用 ggplot2
手动制作绘图:
ggplot(mapping = aes(x = Index, y = Value)) +
geom_line(data = fortify(Cl(AAPL), melt = TRUE), aes(color = "AAPL")) +
geom_line(data = fortify(Cl(GOLD), melt = TRUE), aes(color = "GOLD")) +
xlab("Index") + ylab("x")
使用 cbind
创建一个组合的 xts/zoo 对象,然后 autoplot
它。如果您希望每个都在自己的面板中,请省略 facet=NULL
。如果您想先对价格进行标准化,请使用 scale(closes)
代替 closes
。 autoplot
行的输出显示在末尾。
closes <- cbind(Cl(AAPL), Cl(GOLD))
autoplot(closes, facet = NULL)
很多股票
如果有很多股票,我们不想单独 cbind
它们,那么我们可以使用此代码创建 closes
。
tickers <- c("AAPL", "GOLD")
getSymbols(tickers, from = start, to = end, env = stocks <- new.env())
closes <- do.call("cbind", eapply(stocks, Cl))
或者我们可以使用此代码,假设上面的 tickers
:
getSymbols(tickers, from = start, to = end)
closes <- do.call("cbind", lapply(mget(tickers), Cl))