How to add multiple vertical lines to quantmod? - r

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:

Related

Rarefaction curves in RStudio. How do I add labels to the end of the lines?

I am using R to do a simple rarecurve, it makes a nice graph and i can colour and add lables to the axis or title. But I can not get it to show the plot labels.
Someone help?
Below is what I started with.
library(vegan)
rarecurve(orders, step=1, xlab="Individuals", ylab="Orders")
When I change
rarecurve(orders, step=1, xlab="Individuals", ylab="Orders", label=TRUE)
Nothing changes on the graph. I just want a label at the end of each plot saying what row it is from?
I've seen others who have used Vegan and gotten lables on their data, I have tried copying exactly what they have done and I still get no labels. Is there something with the data I need to change?
Thank you to those who viewed my question. For those of you who are also new here is the answer! Hopefully it saves you 12 hours of your life..
I made sure the excel file I used had row names in the first column. Then when loading the data into R I used:
dataname<-read.csv(file="nameofexcel.csv", header = TRUE, row.names=1)
This last bit was not explained and took a lot of playing to figure out. Sorry it is a amateur error but that is what we all are when we start out. Well many of us.

Graphing multiple variables in R

I am currently attempting to graph multiple columns in a matrix in R. So far, I have figured things out, but here is my problem- when I submit a matrix with 5 columns, I only get a graph with 4 lines. I've noticed that the missing line is always the line closest to the x-axis. I've been working on this for several hours now, and I have tried several different things. Any advice or help on how to get R to produce that 5th line (with a corresponding color filling the space between the x-axis and the line) would be greatly appreciated.
gender=cbind(matrix(malepop),matrix(femalepop))
plotmat(year,gender)
#a sample set
biggen=cbind(malepop,femalepop,malepop,femalepop)
#start of the function
plotmat2=function(years,m,colors){
n=m/1000000
#create a plot with the base line
plot(years,n[,1],type='l',ylim=c(0,10))
##create a for loop to generate all other lines and fill in the spaces
for (i in ncol(n):2) {
newpop=matrix(rowSums(n[,1:i]))
lines(year,newpop)
cord.xmat=c(min(years),years,max(years))
cord.ymat=c(-1,newpop[,1],-1)
polygon(cord.xmat,cord.ymat,col=clrs[i])
next
cord.xmat=c(min(years),years,max(years))
cord.ymat1=c(-1,n[,1]/1000000,-1)
polygon(cord.xmat,cord.ymat,col="purple")
}
}
#sample color set
clrs=c("red","blue","yellow","pink","purple", "cyan", "hotpink")
#run the function
plotmat2(year,biggen,clrs)
Thanks for any and all help you can provide!
It might be that you are unintentionally covering up your first line with the other colored sections, and that you may be skipping the creation of the polygon for n[,1].
From the way you tried to graph the columns in descending order, I am assuming you know that your columns are in ascending size order (the section that is pink in your example plot would be the final column in the matrix "biggen"). In case I am wrong about this, it might be a good idea to change your polygon shading using the density argument, which may help you see if you are covering up other sections by accident.
## plotmat2 function
plotmat2=function(years,m,colors){
n=m/1000000
#create a blank plot based on the baseline
plot(years,n[,1],type='n',ylim=c(0,10))
##create a for loop to generate all other lines and fill in the spaces
for (i in ncol(n):1) {
newpop=matrix(rowSums(n[,1:i]))
lines(year,newpop)
cord.xmat=c(min(years),years,max(years))
cord.ymat=c(-1,newpop[,1],-1)
polygon(cord.xmat,cord.ymat,col=colors[i], density=10)
}
}
P.S. If this doesn't help fix the problem, it might help if you provided a portion of your dataset. I am still learning about R and about StackOverflow, but that seems to be sensible advice that is given on a lot of the threads I have read on here. Good luck!

how can I put two bar charts to one graph?

I use two variables from a dataset:
I got the graph for one of them:
barchart(data_derm$PP_SD_ARBVLZ=="1")
I want to put the graph for this variable data_derm$PP_SD_ARBTLZ=="1" next to the other graph to one graph
thanks a lot for any help!
Without a reproducible example, this question will be difficult to answer definitively. One possibility is to use something like ?layout:
layout(matrix(1:2, nrow=1))
barchart(data_derm$PP_SD_ARBVLZ=="1")
barchart(data_derm$PP_SD_ARBTLZ=="1")
See the Quick-R website for more.
So you want two graphs in one figure if I am understanding you correctly.
If you want the commands from the base package. You can use:
> data(mtcars)
# par(mfrow=c(row, col)) and "row" is the number of plots you put in a row and same for "col"
> par(mfrow=c(1,2))
> barplot(mtcars$mpg)
> boxplot(mtcars$mpg ~ as.factor(mtcars$cyl))
You can use other functions if you don't mind using with GGPLOT2.. etc.

LocusZoom standalone change x-axis and use the plots in a script

I am using the standalone LocusZoom software,but I am having two problems:
I have to create a plot showing only position on the x-axis (not showing the genes). If I just use showGenes=FALSE with nothing else the genes still appear, but if I use rfrows=0 then the genes are not shown, but the problem is that also the x-axis label with the positions disappears. Is there a way to only show the position label? It looks like the only way to do this is to modify the original script...
Is there a way to use several plots created using LocusZoom in an R script to position many plots into one unique figure? (output is a pdf for now) There is an option listed in the LocusZoom webpage (http://genome.sph.umich.edu/wiki/LocusZoom_Standalone) called "prelude" but I cannot get more info on how to use it.
If you have any suggestions for either of these two issues it would be very helpful! Thanks!!

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

Resources