R中时间序列图的传说
Legends on time series plots in R
如何在下面的情节中添加图例?
library(quantmod)
library(PerformanceAnalytics)
library(imputeTS)
library(PortfolioAnalytics)
tickers <- c("FB", "AAPL", "AMZN", "NFLX")
weights <- c(.25, .25, .25, .25)
portfolioPrices <- NULL
for (Ticker in tickers)
portfolioPrices <- cbind(portfolioPrices,
getSymbols.yahoo(Ticker, from="2016-01-01", periodicity = "daily", auto.assign=FALSE)[,4])
plot(portfolioPrices, legend = tickers)
如果将 portfolioPrices 转换为数据框,然后分别添加每一行,则可以添加图例。
以下代码无法创建最佳外观 plot/legend,但您可以通过使用 legend/plot 参数来改进它。
此外,可以使用循环而不是硬编码来添加这些行。
library(quantmod)
library(PerformanceAnalytics)
library(imputeTS)
library(PortfolioAnalytics)
tickers <- c("FB", "AAPL", "AMZN", "NFLX")
weights <- c(.25, .25, .25, .25)
portfolioPrices <- NULL
for (Ticker in tickers)
portfolioPrices <- cbind(
portfolioPrices,
getSymbols.yahoo(
Ticker,
from = "2016-01-01",
periodicity = "daily",
auto.assign = FALSE
)[, 4]
)
portfolioPrices <- data.frame(portfolioPrices)
plot(
x = as.Date(rownames(portfolioPrices)),
portfolioPrices$FB.Close,
col = "black",
type = "l",
ylim = c(min(portfolioPrices), max(portfolioPrices)),
main = "Stocks",
xlab = "Date",
ylab = "Price"
)
lines(x = as.Date(rownames(portfolioPrices)),
portfolioPrices$AAPL.Close,
col = "blue")
lines(x = as.Date(rownames(portfolioPrices)),
portfolioPrices$AMZN.Close,
col = "green")
lines(x = as.Date(rownames(portfolioPrices)),
portfolioPrices$NFLX.Close,
col = "red")
legend(
"topleft",
legend = tickers,
col = c("black", "blue", "green", "red"),
cex = 0.5,
lwd = 2
)
如何在下面的情节中添加图例?
library(quantmod)
library(PerformanceAnalytics)
library(imputeTS)
library(PortfolioAnalytics)
tickers <- c("FB", "AAPL", "AMZN", "NFLX")
weights <- c(.25, .25, .25, .25)
portfolioPrices <- NULL
for (Ticker in tickers)
portfolioPrices <- cbind(portfolioPrices,
getSymbols.yahoo(Ticker, from="2016-01-01", periodicity = "daily", auto.assign=FALSE)[,4])
plot(portfolioPrices, legend = tickers)
如果将 portfolioPrices 转换为数据框,然后分别添加每一行,则可以添加图例。
以下代码无法创建最佳外观 plot/legend,但您可以通过使用 legend/plot 参数来改进它。
此外,可以使用循环而不是硬编码来添加这些行。
library(quantmod)
library(PerformanceAnalytics)
library(imputeTS)
library(PortfolioAnalytics)
tickers <- c("FB", "AAPL", "AMZN", "NFLX")
weights <- c(.25, .25, .25, .25)
portfolioPrices <- NULL
for (Ticker in tickers)
portfolioPrices <- cbind(
portfolioPrices,
getSymbols.yahoo(
Ticker,
from = "2016-01-01",
periodicity = "daily",
auto.assign = FALSE
)[, 4]
)
portfolioPrices <- data.frame(portfolioPrices)
plot(
x = as.Date(rownames(portfolioPrices)),
portfolioPrices$FB.Close,
col = "black",
type = "l",
ylim = c(min(portfolioPrices), max(portfolioPrices)),
main = "Stocks",
xlab = "Date",
ylab = "Price"
)
lines(x = as.Date(rownames(portfolioPrices)),
portfolioPrices$AAPL.Close,
col = "blue")
lines(x = as.Date(rownames(portfolioPrices)),
portfolioPrices$AMZN.Close,
col = "green")
lines(x = as.Date(rownames(portfolioPrices)),
portfolioPrices$NFLX.Close,
col = "red")
legend(
"topleft",
legend = tickers,
col = c("black", "blue", "green", "red"),
cex = 0.5,
lwd = 2
)