在 get.current.chob() 中的 chartSeries 错误中绘制一条水平线:设置不正确或缺少图形设备
Plot a horizontal line in chartSeries Error in get.current.chob() : improperly set or missing graphics device
我想为高盛自 2000 年以来调整后的股票价格创建一个时间序列图,并为平均价格画一条水平线。但是,当我尝试绘制水平线时出现 "Error in get.current.chob() : improperly set or missing graphics device" 错误消息。
library(quantmod)
getSymbols("GS", from = "2000-01-01", src="yahoo")
chart_Series(GS[,6], name = "Goldman Sachs", TA = 'addLines(h = mean(GS[,6]))')
您正在混合来自 2 个不同图表选项的代码。您在 quantmod 中有 chartSeries
和 chart_Series
。有点混乱,因为第二个版本往往需要更好的图表,但不像 chartSeries
那样完整。 chart_Series
没有 add_Lines
函数。但也有解决方法。
简单的解决方法,使用chartSeries
:
library(quantmod)
getSymbols("GS", from = "2000-01-01", src="yahoo")
chartSeries(GS[,6], name = "Goldman Sachs", TA = 'addLines(h = mean(GS[,6]))')
使用chart_Series
有点复杂:
您需要使用 add_TA,它需要一个 xts 对象,因此您首先需要创建一个具有相同值的 xts 对象。这些是下面的前 3 行代码。接下来绘制数据,然后使用 add_TA 添加水平线。并且您需要告诉 add_TA 在何处绘制线条(on = 1 表示主图 window)。
dates <- index(GS)
gs_mean <- mean(GS[,6])
gs_mean_xts <- xts(rep(gs_mean, length(dates)), dates)
# create chart
chart_Series(GS[,6], name = "Goldman Sachs")
# plot horizontal line on plot
add_TA(gs_mean_xts, on = 1, col = "blue", lwd = 2)
我想为高盛自 2000 年以来调整后的股票价格创建一个时间序列图,并为平均价格画一条水平线。但是,当我尝试绘制水平线时出现 "Error in get.current.chob() : improperly set or missing graphics device" 错误消息。
library(quantmod)
getSymbols("GS", from = "2000-01-01", src="yahoo")
chart_Series(GS[,6], name = "Goldman Sachs", TA = 'addLines(h = mean(GS[,6]))')
您正在混合来自 2 个不同图表选项的代码。您在 quantmod 中有 chartSeries
和 chart_Series
。有点混乱,因为第二个版本往往需要更好的图表,但不像 chartSeries
那样完整。 chart_Series
没有 add_Lines
函数。但也有解决方法。
简单的解决方法,使用chartSeries
:
library(quantmod)
getSymbols("GS", from = "2000-01-01", src="yahoo")
chartSeries(GS[,6], name = "Goldman Sachs", TA = 'addLines(h = mean(GS[,6]))')
使用chart_Series
有点复杂:
您需要使用 add_TA,它需要一个 xts 对象,因此您首先需要创建一个具有相同值的 xts 对象。这些是下面的前 3 行代码。接下来绘制数据,然后使用 add_TA 添加水平线。并且您需要告诉 add_TA 在何处绘制线条(on = 1 表示主图 window)。
dates <- index(GS)
gs_mean <- mean(GS[,6])
gs_mean_xts <- xts(rep(gs_mean, length(dates)), dates)
# create chart
chart_Series(GS[,6], name = "Goldman Sachs")
# plot horizontal line on plot
add_TA(gs_mean_xts, on = 1, col = "blue", lwd = 2)