Search code examples
rchartsquantmodcandlestick-chart

R chartSeries add extra chart with points


I have a question regarding chartSeries and the addTA / points.default functions in R. I am trying to add an extra chart below the chartSeries-plot with points in different colours (colouring is taken from a function and working with points.default), but unfortunately I'm unable to add these points with addTA. I am able to add a line to the existing charts (both not what i am looking for). The points.default function simply add those points into the chartSeries, which is not what i am looking for. I am looking for a simple straight line of points i can coloring differently with my color_fct and which is added below my chartSeries. I appreciate any help and thank you in advance!

Example Code:

getSymbols("YHOO")
data <- YHOO
chartSeries(data, type = c("auto", "candlesticks", "matchsticks","bars","line"))
hero<-rep(1,length(data$Close))
c(data, xts(hero))
#addTA provides me with a straight line and the coloring fct is not working
plot(addTA(data$hero,pch = 15,cex = 1.5, on = 2, col = color_fct))
#points.default provides me with perfect coloring, but the points are plotted in the middle of the chart
help<- rep(1, length(data$Close))
points.default(x=(1:length(data$High)),y=help+1, col= color_fct,pch = 15,cex = 1.5)

Solution

  • If you are open to using the newer chart_Series instead of chartSeries:

    x_ti <- xts(rnorm(NROW(data)), order.by = index(data))
    x_ti2 <- xts(rep(1, NROW(data)), order.by = index(data))
    x_ti2[1, ] <- 0.5 # work around to get an xts object with all the same values (of 1) to plot if points are not visible on the subplot
    
    chart_Series(data["2017"])
    add_TA(x_ti, col = "purple", pch = 15, type = 'p', cex = .7)
    # plot straight line in subplot:
    add_TA(x_ti2, col = "orange", pch = 9, type = 'p', cex = .7)
    

    enter image description here