2012-07-30 7 views
5

Ich bin ein Neuling zu R und es war sehr hilfreich, Ihre Website zu verwenden. Leider habe ich seit zwei Tagen mit meinem Code zu kämpfen, also wollte ich einige Fragen stellen. Ich habe versucht, schöne Grafiken zu erstellen, in ein PDF-Blatt zu legen, aber ich habe kleine Probleme mit allen Paketen, die ich verwenden. Was ich erreichen möchte, ist ein PDF-Blatt mit drei Graphen und einer Korrelationstabelle zu erstellen. Unten ist ein Beispiel, das ich erstellt habe, das ist sehr ähnlich zu dem, was ich tun möchte, aber es gibt einige Dinge, die ich gerne ändern würde. Ich verwende chartSeries in quantmod für die Graphen und für die Tabelle Ich benutze Textplot.Arbeiten mit chartSeries in quantmod

paar Fragen:

  1. Ist es möglich, das Datum in der oberen rechten Ecke der Graphen zu entfernen?
  2. Statt des Textes Letzter xxxx (der grüne Serie Text) Ich mag würde den Namen der Serie bekommen selbst, z.B. MSFT, ist das machbar?
  3. Wie kann ich den Achsen Namen geben, z. Rückflugdatum?
  4. Im kumulierte Differenz Graph ich die addVo() verwendet und diese Funktion gibt diese schöne BarPlot. In den anderen Graphen verwende ich nicht addVo() nur addTA und type = 'h' und Sie können den Unterschied der Balken/Histogramm Plots sehen. Ist es möglich, den gleichen Plot mit addTA wie in addVo zu erhalten?
  5. Gibt es eine Möglichkeit, die Korrelationstabelle besser anzupassen und/oder professioneller zu machen? Ist vielleicht eine andere Funktion besser?

Best,

OTB

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

#setwd("..........") 

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 matrix 
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() 

Antwort

2

Einige gute Fragen.

Q1: Nein quantmod:::chartSeries.chob hat diesen Code:

old.adj <- par('adj') 
par('adj'=0) 
do.call('title',list([email protected], [email protected]$fg.col)) 
par('adj'=1) 
do.call('title',list(paste('[',start(xx),'/',end(xx),']', sep='') 
        ,[email protected]$main.col)) 
par('adj'=old.adj) 

D.h. Das Bit start (xx)/end (xx) ist fest codiert.

Q2. Auch hier scheint es hart codiert (gleiche Funktion) wird:

if([email protected]=='line') { 
    lines(x.pos,Closes,[email protected]$up.col,[email protected]) 
    main.key <- c(list(list(legend= 
         paste('Last',last(Closes)), 
         [email protected]$up.col)),main.key) 
} 

(ich kann nichts finden in meinem Studium des Quellcodes mit Q3/Q4/Q5 zu helfen, sorry)

+0

Hallo Darren, vielen Dank für deine Antworten, es ist zumindest gut zu wissen, dass es nicht möglich ist. Ich würde es schätzen, wenn jemand die anderen Fragen bitte beantworten könnte. – OTB