display regression equation with facet_wrap

879 views
Skip to first unread message

Denis Chabot

unread,
May 8, 2011, 10:10:31 AM5/8/11
to ggplot2
Hi, 

On scatter plots, I would like to display the regression equation in addition to the regression line obtained with stat_smooth.

I found a few examples on the web, such as:

This involves calculating the regression line with lm() and then using geom_text() to display it on the plot.

While not particularly hard, it is a bit of a waste since the regression is calculated twice (stat_smooth and lm).

More importantly, I am working on a case where I use facet_wrap to display several groups, each in one panel, each with a regression line. I can calculate as many regressions with lm() as there are panels (thus compounding the "waste" problem just raised) but I don't know how to tell ggplot2 which equation to display in which panel. 

Is there a way that I have not discovered to tell ggplot2 to display the equation for the fit produced with stat_smooth (assuming the smooth method does produce an equation)? If this option exists, ggplot2 would automatically display the appropriate equation in each panel of a facet_wrap.

Thanks in advance,

Denis

Hadley Wickham

unread,
May 8, 2011, 5:39:49 PM5/8/11
to Denis Chabot, ggplot2
> Is there a way that I have not discovered to tell ggplot2 to display the
> equation for the fit produced with stat_smooth (assuming the smooth method
> does produce an equation)? If this option exists, ggplot2 would
> automatically display the appropriate equation in each panel of a
> facet_wrap.

No, because there is no way, in general, to figure out that equation.

If you are worried about waste, just do all the computation outside of
ggplot2. See the example in geom_abline, for a start.

Hadley


--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Denis Chabot

unread,
May 8, 2011, 7:06:35 PM5/8/11
to Hadley Wickham, ggplot2
Hi Hadley,

the "waste" worry is just philosophical and my computer does not seem to hold it against me. I have seen how to calculate a regression and display it with geom_abline.

But coming back to displaying different equations with facets, maybe in the future we could have a way to direct annotations to the different facets? For instance, prepare a vector of text strings of the same length as the variable used for faceting and a switch in annotate to specify we do not want to annotate one element of the vector in each facet.

Denis

Kohske Takahashi

unread,
May 8, 2011, 8:22:37 PM5/8/11
to Denis Chabot, Hadley Wickham, ggplot2
Hi

You can put strings on each facet. Do you mean like this?
here is a minimal example:

d <- data.frame(x=1, y=1, f=1:3)
s <- data.frame(f=1:3, x=1, y=1, l=c("tic","tac", "toe"))

ggplot() +
geom_point(aes(x, y), data=d) +
geom_text(aes(x, y, label=l), data=s) +
facet_wrap(~f)


--
Kohske Takahashi <takahash...@gmail.com>

Research Center for Advanced Science and Technology,
The University of  Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html

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

Denis Chabot

unread,
May 8, 2011, 9:01:37 PM5/8/11
to Kohske Takahashi, Hadley Wickham, ggplot2
Great, this should work for my regression equations and keep me out of trouble! I won't be able to try this for a few days as I am travelling, but I'll report with an example if I succeed.

As always, very useful list!

Sincerely,

Denis

Reply all
Reply to author
Forward
0 new messages