3

Is it possible to change the legend on the plot displayed in Quantmod so that values are displayed rather than the variable name? For example:

library("quantmod")
getSymbols("YHOO")
temp1 <- 6
temp2 <- "SMA"
barChart(YHOO)
addTA(ADX(YHOO, n=temp1, maType=temp2))

The legend that is displayed in the plot is ADX(YHOO, n=temp1, maType=temp2). I would like it to display the specific values instead i.e. ADX(YHOO, n=6, maType='SMA').

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
user2500880
  • 125
  • 1
  • 9
  • Please add a [minimal reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). This makes it much easier to understand your problem and to propose a solution. – Henrik Sep 08 '13 at 12:53
  • Thanks Joshua Ulrich and Paolo Casciello for the improved formatting and corrections. – user2500880 Sep 08 '13 at 21:38

2 Answers2

2

There isn't a way to do this automatically with addTA, because it would need to know which of the the parameters of the TA call it needs to evaluate. But you can do it manually by setting the legend= argument yourself.

One way to do it is to use paste (or paste0).

barChart(YHOO)
Legend <- paste0('ADX(YHOO, n=',temp1,', maType=',temp2,')')
addTA(ADX(YHOO, n=temp1, maType=temp2), legend=Legend)

Or you could create and manipulate the call to get what you want.

barChart(YHOO)
callTA <- call("ADX",quote(YHOO),n=temp1,maType=temp2)
eval(call("addTA", callTA, legend=deparse(callTA)))
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • Thanks Joshua. I'd worked out a similar solution to your first suggestion which will also display the output values for the TA in the legend. Unfortunately I haven't worked out how to get the color of the output text to match the relevant color of line on the plot. – user2500880 Sep 10 '13 at 11:47
0

The following is a partial solution which displays the values rather than variable names in the legend as well as the relevant output values for the TA. However, unlike the default settings of addTA, the text for each output value doesn't match the colour of the line on the addTA plot. Unfortunately I haven't worked out how to get the text of the output values to match the colour of its relevant line on the addTA plot. Any suggestions?

  library("quantmod")
  getSymbols("YHOO")
  barChart(YHOO, subset="last 4 months")
  col <- c("red", "blue", "green", "orange")
  temp1 <- 8
  temp2 <- "SMA"
  temp <- ADX(HLC(YHOO), n=temp1, maType=temp2)
  legend <- rep(NA, NCOL(temp)+1)
  legend[1] <- paste("ADX(HLC(YHOO)", "n=", temp1, "maType=", temp2)
  for(x in 2:(NCOL(temp)+1)){
      legend[x] <- (paste(colnames(temp[,(x-1)]),": ", round(last(temp[,(x-1)]),3), sep=""))
  }
  addTA(temp, legend = legend, col=col)
user2500880
  • 125
  • 1
  • 9