Google Groups

Re: Correlation values in each cell of facet grid


Hideyoshi Maeda Jul 30, 2012 10:29 PM
Posted in group: ggplot2
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...

On 24 Jul 2012, at 17:24, Brandon Hurr wrote:

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. 

On Tue, Jul 24, 2012 at 6:36 AM, hlm <hideyos...@gmail.com> wrote:
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.


On Tuesday, 24 July 2012 11:41:04 UTC+10, winston wrote:
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


On Mon, Jul 23, 2012 at 7: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


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