I am new to R and it was very helpful to use your site. Unfortunately, I struggled with my code now for two days, so I wanted to ask a few questions. I tried to create good graphics to pdf, but I have few problems with all the packages that I was using. Therefore, I want to create one pdf sheet with three graphs and one correlation table. The following is an example that I created very similar to what I want to do, but there are a few things that I would like to change. I use chartSeries in quantmod for graphs and tables. I use textplot.
A few questions:
- Can I delete a date in the upper right corner of the graph?
- Instead of the text Last xxxx (the text of the green series), I would like to get the name of the series itself, for example. MSFT, is it doable?
- How to specify axis names, for example. Return date?
- In the Cumulative Difference column, I use addVo () and that function gives this nice barPlot. On other charts, I do not use addVo () just addTA and type = 'h', and you can see the difference in the bar / histogram sections. Is it possible to get the same plot using addTA, as in addVo?
- Is there a better way to choose a correlation table and / or make it more professional. Perhaps another feature is better?
Best
OTV
install.packages("quantmod")
install.packages("gplots")
library(quantmod)
library(gplots)
getSymbols("MSFT")
getSymbols("AAPL")
getSymbols("COKE")
getSymbols("PEP")
MSFT.Return <- diff(MSFT)/lag(MSFT)
AAPL.Return <- diff(AAPL)/lag(AAPL)
COKE.Return <- diff(COKE)/lag(COKE)
PEP.Return <- diff(PEP)/lag(PEP)
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))
techDifference <- MSFT.Close - AAPL.Close
bevDifference <- COKE.Close - PEP.Close
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"
tech <- cbind(MSFT.Close,AAPL.Close,techDifference)
bev <- cbind(COKE.Close,PEP.Close,bevDifference)
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)"))
techCumulative <- cumsum(abs(techDifference))
bevCumulative <- cumsum(abs(bevDifference))
diffCumulative <- techCumulative - bevCumulative
colnames(techCumulative)[1] <- "Tech"
colnames(bevCumulative)[1] <- "Beverage"
colnames(diffCumulative)[1] <- "Volume"
cumulative <- cbind(techCumulative,bevCumulative,diffCumulative)
chartSeries(cumulative,order=1,up.col='green', name='Cumulative Difference', layout=NULL,
TA=c("addTA(cumulative,order=2,on=1,layout=NULL)", addVo()))
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()