acf, autocorrelation plot with ggplot2

793 views
Skip to first unread message

Bunny, lautloscrew.com

unread,
May 19, 2010, 7:06:27 AM5/19/10
to ggp...@googlegroups.com
Dear all, 

I am looking for a possibility to create autocorrelation plots comparable to those plotted by acf(). 

I have plotted my ts nicely with qplot, like this:

qplot(date,value,data=result,geom="line") + geom_hline(yintercept=0,color="red",alpha=0.6,size=0.2)+ facet_grid(variable~.) , 

Now there is two things i´d love to do: 

1) add bars of autocorrelation to the same graph. That would really be nice because the original time series is already scaled from -1 to 1. 

2) actually I have a data.frame of these 4 time series. I´d like to use series 1 as a reference and have autocorrelation plot with the other 3. If I use acf(mydataframe) I get 16 plots. Is there a way to show only one side of the correlation (just like with covariance matrices) ?

In general it would already nice to do what acf does with ggplot2 because the plots look so much better. 

Currently, my idea for 1) is to acf(x,plot=F) and then try to plot the result into the same graph with some bar geom. But I was not able to make it run thus far. 

thx in advance for any help, best

matt

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Dennis Murphy

unread,
May 19, 2010, 7:23:34 AM5/19/10
to Bunny, lautloscrew.com, ggp...@googlegroups.com
Hi:

On Wed, May 19, 2010 at 4:06 AM, Bunny, lautloscrew.com <bu...@lautloscrew.com> wrote:
Dear all, 

I am looking for a possibility to create autocorrelation plots comparable to those plotted by acf(). 

I have plotted my ts nicely with qplot, like this:

qplot(date,value,data=result,geom="line") + geom_hline(yintercept=0,color="red",alpha=0.6,size=0.2)+ facet_grid(variable~.) , 

Now there is two things i´d love to do: 

1) add bars of autocorrelation to the same graph. That would really be nice because the original time series is already scaled from -1 to 1. 

Hopefully not on the same display - the visual metaphors are quite different between plots of the series
and plots of the autocorrelation structure. Moreover, the time units represent different things (time units vs.
time lags). I trust you mean separate graphs on the same graphics page.

2) actually I have a data.frame of these 4 time series. I´d like to use series 1 as a reference and have autocorrelation plot with the other 3. If I use acf(mydataframe) I get 16 plots. Is there a way to show only one side of the correlation (just like with covariance matrices) ?

It sounds like you are interested in the cross-correlation of series 2-4 with series 1....

In general it would already nice to do what acf does with ggplot2 because the plots look so much better. 

Currently, my idea for 1) is to acf(x,plot=F) and then try to plot the result into the same graph with some bar geom. But I was not able to make it run thus far. 

You could always write a fortify method for time series :)

HTH,
Dennis

thx in advance for any help, best

matt

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Trevor Davis

unread,
May 19, 2010, 5:23:51 PM5/19/10
to Dennis Murphy, Bunny, lautloscrew.com, ggp...@googlegroups.com
FYI there is a fortify method for "tis" time series in the "tis" package.  See ?fortify.tis

The package also has some functions to convert between some of the other time series classes,
 i.e. you can use fortify(as.tis(some.ts.series)) to fortify the basic "ts" series used in base R.

- Trevor

Trevor Davis

unread,
May 19, 2010, 5:37:01 PM5/19/10
to Dennis Murphy, Bunny, lautloscrew.com, ggp...@googlegroups.com
The "tis" package also has an nberShade.ggplot method to add shaded regions corresponding to US recessions to the background of a prexisting ggplot object.

-Trevor
Reply all
Reply to author
Forward
0 new messages