R语言 地块线的图例不可见

mzaanser  于 2023-05-20  发布在  其他
关注(0)|答案(1)|浏览(232)

尝试添加一个图例框,其中每条彩色线表示的值将被清楚地提及。但试了很多次,还是看不出来。
我试过了-
1.更改图例中的x和y值()
1.导出为PDF或JPG
1.重新启动图形驱动程序dev.off()并更新库
1.更新预览
我期待一个图例框,说明每条彩色线表示什么(例如。开盘价、收盘价)。
我的代码:

#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):

输出dput(opening_prices)

iezvtpos

iezvtpos1#

事实上,你的图是一个可扩展的时间序列(xts)图,对于这种类型的图,xts包为你的目的提供了一个addLegend函数。
addLegend(legend.loc = "center", legend.names = NULL, col = NULL, ncol = 1, on = 1, ...)
在你的情况下

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)
#> [1] "AAPL"
stock_data=AAPL # stock_data was not defined in the question code 
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 = ".")]
stock_data_df <- data.frame(
  opening_prices, 
  closing_prices, 
  adjusted_prices, 
  high_prices, 
  low_prices, 
  volume
)

# 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")
addLegend(legend.loc = "topleft",
          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"))

创建于2023-05-12带有reprex v2.0.2

相关问题