Re: Correlation values in each cell of facet grid

1,601 views
Skip to first unread message

Ben Bond-Lamberty

unread,
Jul 23, 2012, 9:41:19 PM7/23/12
to hlm, ggp...@googlegroups.com
A couple months ago someone asked about putting regression equations
in each facet:

https://groups.google.com/d/topic/ggplot2/me5hL8XOxTo/discussion

Your question is a variant on this, so read the above link. Basically
you want to write a short function that will compute and return the
correlation (or whatever you want) for a subset of data, use ddply on
your data to generate a new data frame, and then annotate the facets
with geom_text.

Ben


On Mon, Jul 23, 2012 at 8:51 PM, hlm <hideyos...@gmail.com> wrote:
> Hi guys,
>
> This is a copy of a question I put up on stack overflow, as I didn't realise
> there was a dedicated group for ggplot2 before I posted it, but thought I
> should let you guys know.
>
> Anyway here is the question and the link is below.
>
> When using a facet_grid in ggplot2 I would like to be able to have value of
> the correlation for the subsetted data for each grid cell in the top right
> corner of the specific plot.
>
> e.g. if running:
>
> p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
> p + facet_grid(vs ~ am, margins=TRUE)
>
> I would like to see the value for correlation for each of the 9 plots in the
> grid somewhere. In this specific case from the example, I would expect each
> to be close to -0.9 or so from visual inspection.
>
> Or perhaps an output table to go with the plot that gives the correlation
> values for each of the cells in the table matching up with the
> facet_grid...(this is less desirable but also an option).
>
> Ideally I would like to extend this to any other function I choose that so
> that it can use either or both of the two variables plotted to calculate
> statistics.
>
> Is this possible?
>
> Thanks in advance
>
>
> http://stackoverflow.com/questions/11622526/correlation-values-in-a-facet-grid-from-ggplot2
>
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2

Winston Chang

unread,
Jul 23, 2012, 9:41:04 PM7/23/12
to hlm, ggp...@googlegroups.com
You could do something like this:

p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()

# Calculate correlation for each group
cors <- ddply(mtcars, c("vs", "am"), summarise, cor = round(cor(mpg, wt), 2))

p + facet_grid(vs ~ am) +
  geom_text(data=cors, aes(label=paste("r=", cor, sep="")), x=30, y=4)


I don't think it's possible to make this come out correctly with margins=TRUE, though. If you want the margins, you may need to preprocess your data to add an ALL value for each faceting variable.

-Winston

hlm

unread,
Jul 24, 2012, 1:36:41 AM7/24/12
to ggp...@googlegroups.com, hlm
That looks great, thanks, and as a bit of a follow up question, is there anyway to determine where the "space" is for each facet grid cell, so that the correlation figure could be put there, rather than having it hard coded.

Thanks for the speedy reply.

Brandon Hurr

unread,
Jul 24, 2012, 3:24:02 AM7/24/12
to hlm, ggp...@googlegroups.com
I usually use -Inf or Inf to put it in one of the corners, but you'll have to use vjust and hjust to make sure it is visible. 

Hideyoshi Maeda

unread,
Jul 31, 2012, 1:29:56 AM7/31/12
to Brandon Hurr, ggp...@googlegroups.com
for the below example of showing correlation values in a facet plot, how can you alter the colour of the text according to the correlation value and specifically i would like it to be a scale which goes brighter red for more negative correlations and a brighter blue for more positive correlations...is this possible?

this seems simple but not quite sure how to do it....any help would be much appreciated

my aes for the geom_text() part is currently: 

+geom_text(data=cors, aes(label=paste("r= ",cor,sep=""), size=abs(cor), colour=cor), x=Inf, y=Inf, vjust=1, hjust=1, show_guide=FALSE)

but this currently only goes from black to light blue...i just want to change the colours that the gradient works though...
Reply all
Reply to author
Forward
0 new messages