Working with quantmod chartSeries and plot.zoo

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
OTB
Reply | Threaded
Open this post in threaded view
|

Working with quantmod chartSeries and plot.zoo

OTB
Hi all,

I'm a newbie to R and it has been very helpful to use your website. Unfortunately I've been struggling with my code now for two days so I wanted to ask few questions. I've been trying to create nice graphs to put into a pdf sheet but I'm having little problems with all the packages I've been using. So what I want to accomplish is create one pdf sheet with three graphs and one correlation table. Attached is an example I created which is very similar to what I want to do but there are few things I would like to change. I'm using chartSeries in quantmod for the graphs and for the table I'm using textplot. Few questions:

1) Is it possible to remove the date in the upper right corner of the graphs?
2) Instead of the text Last xxxx (the green series) I would like to get the name of the series itself, e.g. Stock 1, is that doable?
3) How can I give the axes names, e.g. Return, Date?
4) In the Cumulative Difference graph I'm using the addVo() and that function gives this nice barPlot. In the other graphs I'm not using addVo() just addTA and type='h' and you can see the difference of the bar/histogram plots. Is it possible to get the same plot using addTA as in addVo?
5) What is the best way to fit the correlation table nicely in the file and maybe also set the number of digits?


I've also been trying to use plot.zoo because it is easier to work with the chart but it seems like impossible to get all the graphs into one page in pdf similar to the attached file. The graphs always display on separate pages. I've tried layout, which works for quantmod, and par(mfrow=), which works for regular plots, but neither seem to work for plot.zoo. What is the right way to do this?TestGraph.pdf

I appreciate your help.

Best,
OTB
Reply | Threaded
Open this post in threaded view
|

Re: Working with quantmod chartSeries and plot.zoo

Gabor Grothendieck
On Fri, Jul 27, 2012 at 1:21 AM, OTB <[hidden email]> wrote:

> Hi all,
>
> I'm a newbie to R and it has been very helpful to use your website.
> Unfortunately I've been struggling with my code now for two days so I wanted
> to ask few questions. I've been trying to create nice graphs to put into a
> pdf sheet but I'm having little problems with all the packages I've been
> using. So what I want to accomplish is create one pdf sheet with three
> graphs and one correlation table. Attached is an example I created which is
> very similar to what I want to do but there are few things I would like to
> change. I'm using chartSeries in quantmod for the graphs and for the table
> I'm using textplot. Few questions:
>
> 1) Is it possible to remove the date in the upper right corner of the
> graphs?
> 2) Instead of the text Last xxxx (the green series) I would like to get the
> name of the series itself, e.g. Stock 1, is that doable?
> 3) How can I give the axes names, e.g. Return, Date?
> 4) In the Cumulative Difference graph I'm using the addVo() and that
> function gives this nice barPlot. In the other graphs I'm not using addVo()
> just addTA and type='h' and you can see the difference of the bar/histogram
> plots. Is it possible to get the same plot using addTA as in addVo?
> 5) What is the best way to fit the correlation table nicely in the file and
> maybe also set the number of digits?
>
>
> I've also been trying to use plot.zoo because it is easier to work with the
> chart but it seems like impossible to get all the graphs into one page in
> pdf similar to the attached file. The graphs always display on separate
> pages. I've tried layout, which works for quantmod, and par(mfrow=), which
> works for regular plots, but neither seem to work for plot.zoo. What is the
> right way to do this?
> http://r.789695.n4.nabble.com/file/n4638057/TestGraph.pdf TestGraph.pdf
>
> I appreciate your help.
>
> Best,
> OTB
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Working-with-quantmod-chartSeries-and-plot-zoo-tp4638057.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

Please read the last line to every post on r-help and note the part
about reproducible code.

--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
OTB
Reply | Threaded
Open this post in threaded view
|

Re: Working with quantmod chartSeries and plot.zoo

OTB
Hi all,

I'm sorry, I didn't notice this comment. Thank you Gabor for letting me know. Following is a reproducible code and a new picture. The questions are the same but I've figured little bit out how to use textplot but I would like to have the table little bit more professional, maybe better to use another function?

install.packages("quantmod")
library(quantmod)

getSymbols("MSFT")
getSymbols("AAPL")
getSymbols("COKE")
getSymbols("PEP")

#Get the return
MSFT.Return <- diff(MSFT)/lag(MSFT)
AAPL.Return <- diff(AAPL)/lag(AAPL)
COKE.Return <- diff(COKE)/lag(COKE)
PEP.Return <- diff(PEP)/lag(PEP)

#Get the return for last two months and only get close price return.
#because in my data I only have the close price.
MSFT.Close <- MSFT.Return['2012-06-01::2012-07-27', 'MSFT.Close']
AAPL.Close <- AAPL.Return['2012-06-01::2012-07-27', 'AAPL.Close']
COKE.Close <- COKE.Return['2012-06-01::2012-07-27', 'COKE.Close']
PEP.Close <- PEP.Return['2012-06-01::2012-07-27', 'PEP.Close']

pdf(sprintf("%s.pdf","ExampleGraph"), width=11.69, height=8.27)

layout(matrix(1:8, nrow=4))

#Get the difference in return
techDifference <- MSFT.Close - AAPL.Close
bevDifference <- COKE.Close - PEP.Close

#Rename columns
colnames(MSFT.Close)[1] <- "MSFT"
colnames(AAPL.Close)[1] <- "AAPL"
colnames(techDifference)[1] <- "Difference"

colnames(COKE.Close)[1] <- "COKE"
colnames(PEP.Close)[1] <- "PEP"
colnames(bevDifference)[1] <- "Difference"

#Combine into two tables
tech <- cbind(MSFT.Close,AAPL.Close,techDifference)
bev <- cbind(COKE.Close,PEP.Close,bevDifference)

#Plot charts
chartSeries(tech, order=1,up.col='green', name='MSFT & AAPL', layout=NULL,
TA=c("addTA(tech,order=2,on=1,layout=NULL);
addTA(tech$Difference,legend='Difference',type='h',layout=NULL)"))

chartSeries(bev, order=1,up.col='green', name='COKE & PEP', layout=NULL,
TA=c("addTA(bev,order=2,on=1,layout=NULL);
addTA(bevDifference$Difference,legend='Difference',type='h',layout=NULL)"))

#Take the cumulative difference for each sector
techCumulative <- cumsum(abs(techDifference))
bevCumulative <- cumsum(abs(bevDifference))
diffCumulative <- techCumulative - bevCumulative

#Rename columns
colnames(techCumulative)[1] <- "Tech"
colnames(bevCumulative)[1] <- "Beverage"
#If I set the name as Volume, I can use addVo() and get nice barplot.
#Problem with that is the legend name will be Volume but I would like to
#have it Difference and of course I'm using wrong column name.
colnames(diffCumulative)[1] <- "Volume"

#Combine into one table
cumulative <- cbind(techCumulative,bevCumulative,diffCumulative)

#Plot chart
chartSeries(cumulative,order=1,up.col='green', name='Cumulative Difference', layout=NULL,
TA=c("addTA(cumulative,order=2,on=1,layout=NULL)", addVo()))

#Get the correlation matric
correlationTable <- cbind(tech[,1:2],bev[,1:2])
correlation <- cor(correlationTable)
corTable <- as.table(correlation)
corrFormatted <- formatC(corTable, format = "f", digits = 3)
textplot(corrFormatted,valign="top",col.data=colors()[300],
col.rownames=colors()[300],col.colnames=colors()[300])
title("Correlation",cex.main=2.5,col.main=colors()[300])

dev.off()ExampleGraph.pdf

Thank you.

Best,
OTB
Reply | Threaded
Open this post in threaded view
|

Re: Working with quantmod chartSeries and plot.zoo

Gabor Grothendieck
On Fri, Jul 27, 2012 at 2:46 PM, OTB <[hidden email]> wrote:
> Hi all,
>
> I'm sorry, I didn't notice this comment. Thank you Gabor for letting me
> know. Following is a reproducible code and a new picture. The questions are
> the same but I've figured little bit out how to use textplot but I would
> like to have the table little bit more professional, maybe better to use
> another function?

Did you solve the plot.zoo problem?

--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
OTB
Reply | Threaded
Open this post in threaded view
|

Re: Working with quantmod chartSeries and plot.zoo

OTB
Hi Gabor,

No I haven't. I would like to go for the quantmod one so I wanted to see if someone could help me out with that first.

Best,
OTB

On Fri, Jul 27, 2012 at 4:03 PM, Gabor Grothendieck [via R] <[hidden email]> wrote:
On Fri, Jul 27, 2012 at 2:46 PM, OTB <[hidden email]> wrote:
> Hi all,
>
> I'm sorry, I didn't notice this comment. Thank you Gabor for letting me
> know. Following is a reproducible code and a new picture. The questions are
> the same but I've figured little bit out how to use textplot but I would
> like to have the table little bit more professional, maybe better to use
> another function?

Did you solve the plot.zoo problem?

--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



If you reply to this email, your message will be added to the discussion below:
http://r.789695.n4.nabble.com/Working-with-quantmod-chartSeries-and-plot-zoo-tp4638057p4638172.html
To unsubscribe from Working with quantmod chartSeries and plot.zoo, click here.
NAML

Reply | Threaded
Open this post in threaded view
|

Re: Working with quantmod chartSeries and plot.zoo

jayesh.baviskar
This post has NOT been accepted by the mailing list yet.
In reply to this post by OTB
Hi to all,

I am beginner in R programming. I am trying to implement the code given above. I calculated correlation table.

Here is the code

require(quantmod)
getSymbols("YHOO",src="yahoo",from="2013-12-30",to="2014-02-12")
getSymbols("GOOG",src="yahoo",from="2013-12-30",to="2014-02-12")

#Get the return
YHOO.Return <- diff(YHOO)/lag(YHOO)
GOOG.Return <- diff(GOOG)/lag(GOOG)

#Get the return for last month and only get close price return.
YHOO.Close<-YHOO.Return['2014-01-01::2014-01-31', 'YHOO.Close']
GOOG.Close<-GOOG.Return['2014-01-01::2014-01-31', 'GOOG.Close']

layout(matrix(1:3, nrow=3))

#Get the difference in return
Diff <- YHOO.Close- GOOG.Close

#Rename columns
colnames(YHOO.Close)[1] <- "YHOO"
colnames(GOOG.Close)[1] <- "GOOG"
colnames(Diff)[1] <- "Difference"

#Combine into two tables
tech <- cbind(YHOO.Close,GOOG.Close,Diff)

corrFormatted <- formatC(as.table(cor(tech[,1:2])), format = "f", digits = 3)

chartSeries(tech, order=1,up.col='green', name='YAHOO & GOOGLE', layout=NULL,            TA=c("addTA(tech,order=2,on=1,layout=NULL); addTA(tech$Difference,legend='Difference',type='h',layout=NULL)"))

# I want to print correlation table values on 3rd row of matrix
text(corrFormatted,valign="top",col.data=colors()[300], col.rownames=colors()[300],col.colnames=colors()[300])
title("Correlation",cex.main=2.5,col.main=colors()[300])
#but Its not get printed on 3rd row.
#it is getting printed on 2nd row only

But I am not able to print this correlation table value on the graph.

does anyone can help me to sort out this problem.