2012-07-30 12 views
5

Tôi là người mới đến R và nó đã rất hữu ích để sử dụng trang web của bạn. Thật không may tôi đã đấu tranh với mã của tôi bây giờ trong hai ngày vì vậy tôi muốn để đặt một vài câu hỏi. Tôi đã cố gắng để tạo ra các đồ thị tốt đẹp để đưa vào một tấm pdf nhưng tôi có vấn đề nhỏ với tất cả các gói tôi đã sử dụng . Vì vậy, những gì tôi muốn thực hiện là tạo ra một tờ pdf với ba đồ thị và một bảng tương quan. Dưới đây là một ví dụ tôi tạo ra là rất giống với những gì tôi muốn làm nhưng có một vài điều tôi muốn thay đổi. Tôi đang sử dụng chartSeries trong quantmod cho các đồ thị và cho bảng Tôi đang sử dụng textplot.Làm việc với biểu đồSeries trong quantmod

vài câu hỏi:

  1. Có thể loại bỏ ngày ở góc trên bên phải của đồ thị?
  2. Thay vì văn bản xxxx cuối cùng (văn bản chuỗi màu xanh lá cây) Tôi muốn nhận được tên của chính chuỗi đó, ví dụ: MSFT, có thể thực hiện được không?
  3. Tôi làm cách nào để cung cấp tên trục, ví dụ: Ngày trở lại?
  4. Trong đồ thị sự khác biệt tích lũy, tôi đang sử dụng hàm addVo() và hàm cung cấp cho barPlot tốt đẹp này. Trong các đồ thị khác, tôi không sử dụng addVo() chỉ cần addTA và type = 'h' và bạn có thể thấy sự khác biệt của thanh/biểu đồ lô. Có thể nhận được cùng một âm mưu bằng cách sử dụng addTA như trong addVo?
  5. Có cách nào để phù hợp với bảng tương quan tốt hơn và/hoặc làm điều đó chuyên nghiệp hơn không. Có lẽ một chức năng khác tốt hơn?

nhất,

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

Trả lời

2

Một số câu hỏi tốt.

Q1: Số quantmod:::chartSeries.chob có mã này:

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) 

Tức là bit start (xx)/end (xx) được mã hóa cứng.

Q2. Một lần nữa, nó dường như được mã hóa cứng (giống chức năng):

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) 
} 

(Tôi không thể tìm thấy bất cứ điều gì trong các nghiên cứu của tôi về mã nguồn để giúp đỡ với Q3/Q4/Q5, xin lỗi)

+0

Hi Darren, Cảm ơn bạn rất nhiều vì câu trả lời của bạn, ít nhất là tốt để biết điều đó là không thể. Tôi sẽ đánh giá cao nếu ai đó có thể vui lòng trả lời các câu hỏi khác. – OTB