问题描述
我无法在图表中添加垂直线或水平线.我觉得我错过了一些非常简单的东西,但我似乎无法找到它.我从网上复制了一些例子,但它不起作用.我做错了什么?
这就是我正在尝试的:
库(quantmod)getSymbols("^FTSE", src="yahoo", from=as.Date('2004-01-01'),to=as.Date('2013-01-01'), periodity="weekly")情节(富时 $ 富时.关闭)abline(v='2008-01-01', col="red")
我也试过:
abline(h = 5000, col="red")
和abline(h = mean(FTSE$FTSE.Close), col="red")
但它们也不起作用.未显示任何线条.
我只想要一条垂直线.我正在使用 RStudio.
或者,我们将不得不使用 xts::addEventLines
.
实际上有一个
注意:也可以定义多个事件线,例如events .
I'm unable to add a vertical, or horizontal line to my graph. I feel I'm missing something very simple, but I can't seem to find it. I've copied some examples from the internet but it won't work. What am I doing wrong?
This is what I'm trying:
library(quantmod)
getSymbols("^FTSE", src="yahoo", from=as.Date('2004-01-01'),
to=as.Date('2013-01-01'), periodicity="weekly")
plot(FTSE$FTSE.Close)
abline(v='2008-01-01', col="red")
I've also tried:
abline(h = 5000, col="red")
andabline(h = mean(FTSE$FTSE.Close), col="red")
But they also won't work. No lines are shown.
I just want a vertical line. I'm using RStudio.
解决方案
Alternatively, we will have to use
xts::addEventLines
.
There was actually an issue opened at github on Feb, 2015 that
abline
simply won't work with the new plot.xts
and we should use the alternative.
library(quantmod)
getSymbols("^FTSE", src="yahoo", from=as.Date('2004-01-01'),
to=as.Date('2013-01-01'), periodicity="weekly")
plot(FTSE$FTSE.Close)
# abline(v='2008-01-01', col="red") # won't work
# alternative
events <- xts("", as.Date("2008-01-01"))
addEventLines(events, col="red", lwd=2)
Result
Note: It's also possible to define multiple event lines, e.g.
events <- xts(letters[1:3], as.Date(c("2008-01-01", "2009-01-01", "2010-01-01")))
.
这篇关于如何在 R 中使用 abline() 绘制简单的垂直线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!