【发布时间】:2012-07-30 04:32:35
【问题描述】:
我是 R 的新手,使用您的网站非常有帮助。 不幸的是,我已经为我的代码苦苦挣扎了两天,所以我想要 问几个问题。我一直在尝试创建漂亮的图表以放入 pdf 表,但我对我一直使用的所有包裹都没有什么问题 使用。所以我想要完成的是创建一个包含三个的 pdf 表 图表和一个相关表。下面是我创建的一个例子 与我想做的非常相似,但我想做的事情很少 改变。我在 quantmod 中为图表和表格使用 chartSeries 我正在使用文本图。
几个问题:
- 是否可以删除右上角的日期 图表?
- 代替文本 Last xxxx(绿色系列文本)我想得到 系列本身的名称,例如MSFT,这可行吗?
- 如何给坐标轴名称,例如归期?
- 在累积差异图中,我使用了 addVo() 和 函数给出了这个不错的 barPlot。在其他图表中,我没有使用 addVo() 只需 addTA 和 type='h' 就可以看到条形图/直方图的区别 情节。是否可以使用 addTA 获得与 addVo 中相同的图?
- 有没有办法更好地拟合相关表和/或更专业。也许另一个功能更好?
最好的,
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()
【问题讨论】: