如何在quantmod图表中突出显示单个蜡烛?

时间:2016-12-11 16:57:02

标签: r charts xts quantmod

我找不到任何关于如何在quantmod图表中突出显示单个蜡烛的信息。这是一个示例代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>WORLDS</button>
<div class='overlay'>
  <div class='popup'>
    <iframe width='' src="http://stackoverflow.com/"></iframe>
  </div>
</div>

我想做的是以某种方式在2007-01-09强调该栏。它可以是不同的蜡烛颜色,围绕它的矩形或可能是不同的背景颜色。任何想法怎么做?

2 个答案:

答案 0 :(得分:4)

执行此操作的一种方法是更改​​主题颜色以匹配要突出显示的点。在下面的代码中,我将上下颜色从单个名称更改为与数据长度匹配的颜色矢量。为此,我使用了您的AAPL$show向量。我添加&#34; 1&#34;到AAPL$show+1向量是我想将0,1向量转换为1,2。然后用于在c("red","cyan")之间进行选择。

library(quantmod)                                                              
getSymbols("AAPL", src="yahoo")                                                
AAPL$show <- ifelse(as.Date(index(AAPL)) == as.Date("2007-01-09"), 1, 0)       

myTheme <- chart_theme()                                                       
myTheme$col$dn.col <- c("red","cyan")[AAPL$show+1]                             
myTheme$col$up.col <- c("white","cyan")[AAPL$show+1]                           
chart_Series(AAPL, subset="2007-01",theme=myTheme)                             

add_TA(AAPL$show, col="red")      

hyphen/minus-like characters

答案 1 :(得分:2)

xts拥有现成的工具。如果您传入包含logical类型数据的xts,则可以在一个班轮中非常快速地包含垂直线标记(在调用chart_Series之后):

x_vline <- xts(TRUE, as.Date("2007-01-09"))
add_TA(x_vline, on = -1, col = "lightblue", border='red')
# Or, if you understand what's going on, replace the above with it all packaged together:
# add_TA(xts(TRUE, as.Date("2007-01-09")), on = -1, col = "lightblue", border='red')

enter image description here

使用on = 1在蜡烛前绘制on =-1以绘制在蜡烛后面。其他参数应该是自我解释的。

请注意,您也可以添加多个垂直标记,如果您愿意,可以跨越多个条形(整齐地遮蔽区域)。例如:使用x_vline <- xts(rep(TRUE, 3), as.Date(c("2007-01-09", "2007-01-24", "2007-01-25")))给出: enter image description here