R语言quantmod绘制股票折线图时图例无法显示的求助
股价走势图图例无法显示问题及解决
问题描述
尝试为AAPL股价走势图添加图例框,标注各颜色线条对应的股价类型(开盘价、收盘价等),但多次尝试后图例始终无法显示。
已尝试的解决方法:
- 修改
legend()函数的x、y参数值 - 将图表导出为PDF或JPG格式
- 执行
dev.off()重启图形驱动并更新相关库 - 刷新图表预览
期望效果:显示图例框,明确标注蓝色、红色、绿色、橙色、紫色线条分别代表开盘价、收盘价、调整后价格、最高价、最低价。
原代码
#install.packages("quantmod") library(quantmod) symbol <- "AAPL" start_date <- as.Date("2023-04-11") end_date <- as.Date("2023-05-11") getSymbols(Symbols = symbol, src = "yahoo", from = start_date, to = end_date) opening_prices <- stock_data[, paste(symbol, "Open", sep = ".")] closing_prices <- stock_data[, paste(symbol, "Close", sep = ".")] adjusted_prices <- stock_data[, paste(symbol, "Adjusted", sep = ".")] high_prices <- stock_data[, paste(symbol, "High", sep = ".")] low_prices <- stock_data[, paste(symbol, "Low", sep = ".")] volume <- stock_data[, paste(symbol, "Volume", sep = ".")] #this would print default table of stock data #print(stock_data) stock_data_df <- data.frame( opening_prices, closing_prices, adjusted_prices, high_prices, low_prices, volume ) print(stock_data_df) # Create a new plot with the opening prices plot(opening_prices, type = "l", col = "blue", main = paste("Stock Prices for", symbol)) # Add lines for closing, adjusted, high, and low prices lines(closing_prices, type = "l", col = "red") lines(adjusted_prices, type = "l", col = "green") lines(high_prices, type = "l", col = "orange") lines(low_prices, type = "l", col = "purple") legend(x = "topright", legend = c("Opening Prices", "Closing Prices", "Adjusted Prices", "High Prices", "Low Prices"), lty = c(1, 1, 1, 1, 1), col = c("blue", "red", "green", "orange", "purple"))
当前输出情况
- R Studio图表输出:仅显示AAPL股价走势的多条彩色线条,右上角无预期的图例框
- dput(opening_prices)输出:返回xts格式的时间序列数据,包含日期与对应开盘价数值
问题原因及解决方法
核心原因
quantmod包返回的是xts时间序列对象,其自带的plot.xts()函数会创建特殊的绘图区域,基础绘图系统的legend()无法正确识别该区域,导致图例不显示。此外原代码中stock_data变量未定义(getSymbols默认将数据存入以symbol命名的变量AAPL),也会引发报错。
方法1:使用quantmod自带的图例绑定方式
通过addTA()添加线条,并在legend()中指定on=1绑定到主绘图区域:
#install.packages("quantmod") library(quantmod) symbol <- "AAPL" start_date <- as.Date("2023-04-11") end_date <- as.Date("2023-05-11") # getSymbols默认将数据存入变量AAPL getSymbols(Symbols = symbol, src = "yahoo", from = start_date, to = end_date) opening_prices <- AAPL[, paste(symbol, "Open", sep = ".")] closing_prices <- AAPL[, paste(symbol, "Close", sep = ".")] adjusted_prices <- AAPL[, paste(symbol, "Adjusted", sep = ".")] high_prices <- AAPL[, paste(symbol, "High", sep = ".")] low_prices <- AAPL[, paste(symbol, "Low", sep = ".")] # 用quantmod绘图系统创建主图 plot(opening_prices, type = "l", col = "blue", main = paste("Stock Prices for", symbol)) # 添加其他线条到主图区域 addTA(closing_prices, type = "l", col = "red", on=1) addTA(adjusted_prices, type = "l", col = "green", on=1) addTA(high_prices, type = "l", col = "orange", on=1) addTA(low_prices, type = "l", col = "purple", on=1) # 绑定主图区域添加图例 legend("topright", legend = c("Opening Prices", "Closing Prices", "Adjusted Prices", "High Prices", "Low Prices"), lty = 1, col = c("blue", "red", "green", "orange", "purple"), on=1)
方法2:转换为数据框使用基础绘图系统
将xts对象转为普通数据框,手动处理x轴,让基础绘图系统的legend()正常工作:
#install.packages("quantmod") library(quantmod) symbol <- "AAPL" start_date <- as.Date("2023-04-11") end_date <- as.Date("2023-05-11") getSymbols(Symbols = symbol, src = "yahoo", from = start_date, to = end_date) # 转换为带日期列的数据框 stock_data_df <- data.frame( Date = index(AAPL), Open = AAPL$AAPL.Open, Close = AAPL$AAPL.Close, Adjusted = AAPL$AAPL.Adjusted, High = AAPL$AAPL.High, Low = AAPL$AAPL.Low ) # 基础绘图系统绘制 plot(stock_data_df$Open ~ stock_data_df$Date, type = "l", col = "blue", main = paste("Stock Prices for", symbol), xlab = "Date", ylab = "Price") lines(stock_data_df$Close ~ stock_data_df$Date, type = "l", col = "red") lines(stock_data_df$Adjusted ~ stock_data_df$Date, type = "l", col = "green") lines(stock_data_df$High ~ stock_data_df$Date, type = "l", col = "orange") lines(stock_data_df$Low ~ stock_data_df$Date, type = "l", col = "purple") # 添加图例,此时可正常显示 legend("topright", legend = c("Opening Prices", "Closing Prices", "Adjusted Prices", "High Prices", "Low Prices"), lty = 1, col = c("blue", "red", "green", "orange", "purple"))
内容的提问来源于stack exchange,提问作者Mohammad Mustak Absar Khan
相关产品推荐
相关产品推荐

