R quantmod chartSeries: Add multiple TA overlay to single chart - r

I'm trying to add multiple TA's to my main chartSeries chart, and they all add below instead of overlaying each other. Is it possible to add multiple TA overlays?
chartSeries(GE, theme="white",
TA="addTA(GE1);addTA(GE2);addTA(GE3)")
I've tried with the variables below;
on=1 and
overlay=TRUE
I'm looking for all the TA's to be in a single chart.
Thank you in advance for your time.

Well, on=1 is what you need. I'm having trouble getting the TA="" to work, with the data I had to hand, but this did work:
chartSeries(x,TA=NULL);addTA(EMA(x$Close),on=1)
(The TA=NULL is to remove the volume chart.)
Or, use newTA to define your TA with all the desired parameters in advance, and then the TA argument does accept it:
myEMA = newTA(EMA, Cl, on=1, col=7)
chartSeries(x, TA="myEMA()")
(See ?newTA, which is where I stole that first line from!)

Related

How to determine x, y coordinates when adding text in metafor

I’m using the metafor package to create Forest plots in R. I’d like to add text to my plots to create labels using the text() function. I’m wondering what the simplest way is to determine the x,y coordinates of where I want my text to go. Currently I just guess and see how it looks and then edit as necessary. Is there a way to overlay a grid over my plot or something to guide me (and then remove it after)?
Thank you!
Start by saving what forest() returns:
x <- forest(res)
And then take a look at x. Among other things, it contains xlim and ylim, which are the x-axis and y-axis limits. Try this:
abline(v=x$xlim)
abline(h=x$ylim)
Also useful:
text(x$xlim[1], x$rows, x$rows, xpd=NA)
I hope this helps.

How to add multiple vertical lines to quantmod?

Goal: I want to add multiple vertical lines to my chart. In this example, I want to add vertical lines for the following dates: 2012-01-09,2012-01-24, and 2012-01-31.
Issue: However, my codes adds 4 lines instead of 3 AND it adds them at the wrong dates.
Can someone tell me what I am doing wrong?
Thank you!
library(quantmod)
getSymbols("SPY", from="2012-01-01", to="2013-06-15")
#add multiple vertical lines
chartSeries(SPY,TA="addLines(v=SPY[c(5,15,20)])")
I have a hackish way to do it in my qmao package (https://r-forge.r-project.org/R/?group_id=1113), which is based on this post to the r-sig-finance mailing list.
If you don't want to bother installing the whole package, here is the code for the function you'd need: https://r-forge.r-project.org/scm/viewvc.php/pkg/qmao/R/addVLine.R?view=markup&root=twsinstrument
chartSeries(SPY)
addVLine(index(SPY[c(5,15,20)]))
which produces:

adding a vertical line to a chartSeries graphic

I hope this isn't redundant as I've googled extensively and still have not found an answer. I'm plotting intraday data and want to place a vertical line at a specific point in time. It seems I have to use the function addTA but it always plots below my graph in some weird empty white space. Here's some sample code and data. Thanks for any help.
Data:
date,value
29-DEC-2010:00:02:04.000,99.75
29-DEC-2010:00:03:44.000,99.7578125
29-DEC-2010:00:05:04.000,99.7578125
29-DEC-2010:00:07:53.000,99.7421875
29-DEC-2010:00:07:57.000,99.71875
29-DEC-2010:00:09:20.000,99.7421875
29-DEC-2010:00:11:04.000,99.75
29-DEC-2010:00:12:56.000,99.7421875
29-DEC-2010:00:13:05.000,99.7421875
Code:
#set up data
data = read.csv("foo.csv")
values = data[,2]
time = c(strptime(data[,1],format="%d-%b-%Y:%H:%M:%S",tz="GMT"))
dataxts = xts(values, order.by=time,tzone="GMT")
# chart data
chartSeries(dataxts)
# add vertical line - this is where I have no clue what's going on.
addTA(xts(TRUE,as.POSIXlt("2010-12-29 00:11:00",tz="GMT"),on=1))
What ends up happening is that I get a vertical line where I want it, 2010-12-29 00:11:00, but it sits in a new section below the graph instead of overlaid on it. Any ideas?
You're passing on as an argument to xts, but you should be passing it to addTA.
I think you mean to be doing this:
addTA(xts(TRUE,as.POSIXlt("2010-12-29 00:11:00",tz="GMT")),on=1)
That said, it still doesn't work for me with your sample data. However, if what you had works with your real data except it puts the line in a new panel, then this should work and not open a new panel.
I have an addVLine function in my qmao package that is essentially the same thing.
Edit
Aside from the typo with the parenthesis, the xts object also needs a column name in order for addTA to work (I think... at least in this case anyway). Also, you must give an x value that exists on your chart (i.e. 11:04, not 11:00)
colnames(dataxts) <- "x"
chartSeries(dataxts)
addTA(xts(TRUE,as.POSIXlt("2010-12-29 00:11:04",tz="GMT")),on=1, col='blue')
# or
# library(qmao)
# addVLine(index(dataxts[7]))
Edit 2
Perhaps a better approach is to use the addLines function
addLines(v=7)
Or, if you know the time, but don't know the row number, you could do this
addLines(v=which(index(dataxts) == as.POSIXlt("2010-12-29 00:11:04", tz="GMT")))
which gives

R X&Y axes interval setup in histogram

I have been using the following code. But, instead of starting at -5000, the x-axis is starting at 0. Also, I would like to specify an interval of 100 for both x and y-axes ranges. I tried to use xaxt='n' and axis(side=1), but that didn't help. The following code is the one I am currently working on
setwd("/path/")
data<-read.table("input.txt",sep="\t",header=F)
x<-data$V4
h<-hist(x, col="green",xlim=c(-5000, 162000),ylim=c(0,0.0003),main="TPMDistribution",xlab="TPMValues",probability=TRUE)
s=sd(x)
m=mean(x)
curve(dnorm(x,mean=m,sd=s),add=TRUE,lwd=3,col="red")
lines(density(x),col="blue")
abline(v=mean(x),col="blue")
mtext(paste("mean",round(mean(x),1),";sd",round(sd(x),1),";N",length(x),sep=""),side=1,cex=.75)
dev.off()
All helps appreciated. Thanks in advance.
You'll want to specify the breaks, probably in an array, see here:
http://stat.ethz.ch/R-manual/R-patched/library/graphics/html/hist.html
And adjust the axis using something like this:
http://stat.ethz.ch/R-manual/R-patched/library/graphics/html/axis.html
You may need to use axes=FALSE in your hist(), so you can replace them with axis() in a subsequent line.

Remove Legend In R

How I can remove the legend in a plot in R? I tried legend<-FALSE, doesn't work.
Also, is there a better way to set the position of the legend? For example, is there
a way I can pick the location with my cursor? And I am not talking about ggplot or any fancy add-ons, just regular R plotting.
In order:
This is related to what people tried to explain to you yesterday: Think of a script as primary means of creating your R session. In ESS, you get the script as a by-product; in RStudio you can also work with commands first and then pass those to your session. Lastly, no you can't remove a legend which has already been added to a plot, but you can hopefully re-create your graph using the saved commands.
Yes, since Duncan Murdoch added support for 'topleft' etc you can use logical commands:
plot(1:10) # simple plot
legend("bottomright", "foo") # 'foo' in bottom-right corner
Yes, if you use the output of locator() as input for the legend() command.
You need to specify which plotting function is producing the legend. (Most plotting function do not plot legends by default.)
There is a locator function.

Resources