R:向绘图添加文本不起作用

时间:2017-12-13 16:08:37

标签: r text plot finance

我遇到了以下问题;我写了一个简单的代码来为一个讲座创建一些情节,但不知怎的,它不会在我的情节中添加文字。以为没有警告也没有错误,在网上搜索后到目前为止我找不到任何解决方案。 提前感谢您的帮助,代码如下。简短说明:情节中的任何其他内容都可以按照我的意愿运行,它只是缺少的文本行。

编辑:这是完整的代码

libraries = c("dygraphs", "quantmod", "stringr", "ggplot2")
lapply(libraries, function(x) if (!(x %in% installed.packages())) { install.packages(x) })
lapply(libraries, library, quietly = TRUE, character.only = TRUE)
tickers = c("AMZN","GOOG", "MSFT")

end = Sys.Date()

getSymbols(tickers, from = "2017-10-01", to = end)

a = seq(1, 51, by =1)

time = index(AMZN.1)
time = time[a]
time = format(time, format = "%d.%m.")
for (i in 1:3){
  assign(paste0(tickers[i], ".1"), Cl(get(tickers[i])))
}
AMZN.2 = as.numeric(AMZN.1)
GOOG.2 = as.numeric(GOOG.1)
MSFT.2 = as.numeric(MSFT.1)


abc = as.numeric(match("27.10.", time))

plot(AMZN.2, type = "l", xlab = "time", ylab = "price (USD, NASDAQ)", xaxs = "i", xaxt = "n", main = "Share price Amazon Inc. (Oct 17 - Dec 17)", sub = "qrtly results announced at oct 26th")
axis(1,a, labels = time) #a is a numeric vector, time a character vector
abline(v = abc, col = "red", lty = "dotted") #abc is a number (=20)
abline(h = 972.43, col = "red", lty = "dotted")
abline(h = 1100.95, col = "red", lty = "dotted")
text(abc, "my text here", col = "red", srt = 90) #abc see above

1 个答案:

答案 0 :(得分:1)

运行你的代码,在我看来问题是abc提供了一个x坐标,但是没有提供y坐标。简单地将x和y参数提供为

text(x=abc, y=1000, "my text here", col = "red", srt = 90)

是一个似乎与我的理论一致的例子。