【问题标题】:R: Adding text to a plot does not workR:将文本添加到绘图不起作用
【发布时间】: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,所以应该适合。

标签: r text plot finance


【解决方案1】:

运行您的代码,在我看来,问题在于 abc 提供了 x 坐标,但未提供 y 坐标。只需将 x 和 y 参数提供为

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

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

【讨论】:

  • 非常感谢。监督那个失踪的 y,我觉得很愚蠢。
  • 不客气!我去过那里,有时,它只需要第二双眼睛!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-03
  • 2012-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多