user2416831
user2416831

Reputation: 3

How to creata a "plot matrix"

Hey dear R community !

I have a dataframe with stock values like this:

                   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  

I would like to creata s.th. like a "plot matrix" where in each entry a plot is comparing two lines. Each of this NxN plot has the date on x-axe and the stock value on the y-axe. The idea is to be able to compare each stockvalue with the others one by one. So you can get an idea of the correlation.

I can achieve what I want (more or less) with this command:

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

here you can see the result.

https://docs.google.com/file/d/0B88TpEM5dcSdaTRTVXk4aVdCQmM/edit?usp=sharing

In this result I dont like that every single plot has an own title. This costs much space and isn't really nice. Instead I would like to have it like my hand drawing.

https://docs.google.com/file/d/0B88TpEM5dcSdNGJnaWd4WmlhdGM/edit?usp=sharing

Any ideas ?

Upvotes: 0

Views: 277

Answers (1)

dlaehnemann
dlaehnemann

Reputation: 701

Using the package 'ggplot2' with a facet_grid() should be what you are looking for. The ggplot2 facet_grid documentation gives all the details and the [R Cookbook provides some nice] recipes(http://www.cookbook-r.com/Graphs/Facets_%28ggplot2%29/).

To use your dataframe, you'll have to melt it with the 'reshape2' package first.

Upvotes: 0

Related Questions