嘿,亲爱的R社区!
我有一个数据框,其股票价值如下:
1 2 3 ... N
EADS Daimler BOEING
01.01.2012 5,2 6,7 52
02.01.2012 5,4 6,5 51,8
. . . .
. . . .
. . . .
31.12.2012 7,4 4,8 71
我想创建s.th。就像“曲线图矩阵”,在每个条目中,曲线图是比较两条线。每个NxN图都有x轴上的日期和y轴上的股票价值。这个想法是能够将每个股票的价值逐个与其他股票进行比较。所以你可以对这种相关性有一个了解。
我可以使用以下命令实现我想要的(或多或少):
# hdMn is a matrix containing the normalized entries of the dataframe
windows(title="Comparison CHART (normalized data)")
par(mfrow=c(dim(hdMn)[2],dim(hd)[2])
for (i in 1:dim(hdMn)[2])
{
for (j in 1:dim(hdMn)[2])
{
plot(x=1:dim(hdMn)[1],y=hdMn[1:dim(hdMn)[1],i],col="red",main=paste("comparison"
+ , names(historicalData)[i],"and", names(historicalData)[j]),xlab="working
+ days",ylab="stock value [Euro]",type="l")
lines(x=1:dim(hdMn)[1],y=hdMn[1:dim(hdMn)[1],j],col=(if(i==j)"red"
+ else"green"),type="l")
}
}
在这里您可以看到结果。
https://docs.google.com/file/d/0B88TpEM5dcSdaTRTVXk4aVdCQmM/edit?usp=sharing
在这个结果中,我不喜欢每个单独的情节都有自己的标题。这需要很大的空间,而且不是很好。相反,我想让它像我的手绘一样。
https://docs.google.com/file/d/0B88TpEM5dcSdNGJnaWd4WmlhdGM/edit?usp=sharing
有什么想法吗?
发布于 2013-05-24 18:12:36
使用带有facet_grid()
的包'ggplot2‘应该是你想要的。ggplot2 facet_grid documentation提供了所有的细节,R Cookbook提供了一些很好的食谱(http://www.cookbook-r.com/Graphs/Facets_%28ggplot2%29/)。
要使用你的数据帧,你必须先用'reshape2‘包melt
它。
https://stackoverflow.com/questions/16732152
复制相似问题