Add Horizontal Line to xts Plot

点点圈 提交于 2020-01-16 18:39:13

问题


I would like to add a horizontal line to an xts object plot. I know there is the addEventLines() function in xts to add vertical lines, but the following does not add a line to an xts plot:

abline(h=abc, col="green")

Is there a workaround for this other than adding a new column to the object itself before plotting?


回答1:


You can just create some constant data and use the lines function. Below is a solution together with a reproducible example.

# load package
require(xts)

# get data
data(sample_matrix)
sample.xts <- as.xts(sample_matrix)

# create line data
sample.xts$horizontal_line <- 49.5

# plot 
plot(sample.xts[,"Close"])
lines(sample.xts[, "horizontal_line"], col = "blue")


来源:https://stackoverflow.com/questions/51423633/add-horizontal-line-to-xts-plot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!