【发布时间】:2017-12-13 16:08:36
【问题描述】:
我遇到了以下问题;我写了一个简单的代码来为讲座创建一些情节,但不知何故它没有在我的情节中添加文本。认为既没有警告也没有错误,并且在互联网上搜索后我没有找到任何解决方案。 提前感谢您的帮助,代码如下。简短说明:情节的其他任何内容都可以按我的意愿进行,只是缺少文本行。
编辑:这里是完整的代码
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
【问题讨论】:
-
根据我的经验,这是由于文本被绘制在图表上但在当前比例之外造成的。例如,当您的图形仅从 (x=1,y=1) 变为 (x=2,y=2) 时,您将坐标设置为 (x=10,y=10)。你能仔细检查一下你的文本坐标是否有效吗?
-
如果您向reproducible example 提供示例输入数据,这样我们就可以自己运行和测试代码,这样会更容易为您提供帮助。
-
绘制的向量 (AMZN.2) 的长度为 51,时间向量和 a 向量也是如此; abc 的值为 20,所以应该适合。