Add correlation factors to scatterplot

1,657 views
Skip to first unread message

Johannes Radinger

unread,
Nov 7, 2011, 6:02:37 AM11/7/11
to ggp...@googlegroups.com
Hello,

I'd like to add a text including the corellation factor
into a scatterplot. So far I get the single factors I
want in the text:

r.value <- format(cor(Vs,Vm),digits=2)
p.value <- format(cor.test(Vs,Vm)[3],digits=2)

cor.text <- "Here I want the text"

#and the plot with:

ggplot(data.Vs_Vm, aes(x=Vs,y=Vm))+
geom_point()+
geom_text(aes(0.1,1000,label="test_text"))+
#stat_smooth(method="lm",se=T,colour="black")+
theme_bw()


I want the text as a multiline looking like:

Pearson correlation (Vs ~ Vm):
r = r.value, p = p.value


I think I need to use the substitute function but I don't really understand how to use it correctly. Furthermore I'd like to have "p<=0.001" if it is the case, instead of the real value (like 3.3e-12).

How can I do that?

Best regards,
Johannes

--
NEU: FreePhone - 0ct/min Handyspartarif mit Geld-zurück-Garantie!
Jetzt informieren: http://www.gmx.net/de/go/freephone

Johannes Radinger

unread,
Nov 8, 2011, 6:35:57 AM11/8/11
to Johannes Radinger, ggp...@googlegroups.com
Hello,

to follow up my last question here a short update:

I manage know to get the text into my ggplot but
I am not really happy with the results.

What I did so far:

#get r and p of correlation
r.value <- format(cor(X1,X2),digits=2)
if(cor.test(X1,X2)[3]<=0.001){p.value<-"<=0.001"}else{p.value<-format(cor.test(X1,X2)[3],digits=2)}

#get text for ggplot
cor.text <- paste("Pearson correlation (X1~X2):","\n","r=",r.value,",","p=",p.value)

and then use in ggplot:
geom_text(aes(0.1,1000,label=cor.text,hjust=0, vjust=0,size=5))

The things I am not happy with:
*) It is not the best way to use the paste() because then I get
spaces where I don't wont them (e.g. in the new line etc, before commas)
*) The "<=" sign doesn't look good, I'd like the "mathematical" proper sign
*) I still don't know the way how this can be done with subsitute which might be the better way instead of paste.

Hopefully someone can help me with that...

Thank you
Johannes

-------- Original-Nachricht --------
> Datum: Mon, 07 Nov 2011 12:02:37 +0100
> Von: "Johannes Radinger" <JRad...@gmx.at>
> An: ggp...@googlegroups.com
> Betreff: Add correlation factors to scatterplot

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

--
Ihr GMX Postfach immer dabei: die kostenlose GMX Mail App für Android.
Komfortabel, sicher und schnell: www.gmx.de/android

Mike Lawrence

unread,
Nov 8, 2011, 12:25:45 PM11/8/11
to ggplot2
Have you seen the ezCor() function in the ez package? It may either
suffice for your needs or provide inspiration if you look at the code.

On Nov 8, 7:35 am, "Johannes Radinger" <JRadin...@gmx.at> wrote:
> Hello,
>
> to follow up my last question here a short update:
>
> I manage know to get the text into my ggplot but
> I am not really happy with the results.
>
> What I did so far:
>
> #get r and p of correlation
> r.value <- format(cor(X1,X2),digits=2)
> if(cor.test(X1,X2)[3]<=0.001){p.value<-"<=0.001"}else{p.value<-format(cor.t est(X1,X2)[3],digits=2)}
>
> #get text for ggplot
> cor.text <- paste("Pearson correlation (X1~X2):","\n","r=",r.value,",","p=",p.value)
>
> and then use in ggplot:
> geom_text(aes(0.1,1000,label=cor.text,hjust=0, vjust=0,size=5))
>
> The things I am not happy with:
> *) It is not the best way to use the paste() because then I get
> spaces where I don't wont them (e.g. in the new line etc, before commas)
> *) The "<=" sign doesn't look good, I'd like the "mathematical" proper sign
> *) I still don't know the way how this can be done with subsitute which might be the better way instead of paste.
>
> Hopefully someone can help me with that...
>
> Thank you
> Johannes
>
> -------- Original-Nachricht --------
>
>
>
>
>
>
>
>
>
> > Datum: Mon, 07 Nov 2011 12:02:37 +0100
> > Von: "Johannes Radinger" <JRadin...@gmx.at>

Dennis Murphy

unread,
Nov 8, 2011, 2:55:57 PM11/8/11
to Johannes Radinger, ggp...@googlegroups.com
Firstly, you're rarely if ever going to get a p-value of exactly
0.001, so just use < instead of <=. Secondly, to avoid the = < 0.001
possibility, change r = and p = to r: and p:, respectively. You can
always add spacing in paste() either with the sep = argument or adding
spaces at the end of a text string. If you dearly want the
mathematical <=, then you're going to have to use expression() instead
of paste().

HTH,
Dennis

Brandon Hurr

unread,
Nov 8, 2011, 3:16:20 PM11/8/11
to Dennis Murphy, Johannes Radinger, ggp...@googlegroups.com
Does expression still not play nice with /n line breaks?

Dennis Murphy

unread,
Nov 8, 2011, 3:22:25 PM11/8/11
to Brandon Hurr, Johannes Radinger, ggp...@googlegroups.com
> Does expression still not play nice with /n line breaks?

It does not (I assume you meant \n :). I wouldn't hold my breath
waiting for that to happen, either. Some of Baptiste's past examples
in the archives of this group using grid are probably the best
workaround at present.

D.

Johannes Radinger

unread,
Nov 9, 2011, 5:11:57 AM11/9/11
to Dennis Murphy, brando...@gmail.com, ggp...@googlegroups.com
Hi,

first thank you for your tips...I'll have a look at that.

Now I there is an other problem related to display an equation
on my regression plot. I have several plots with two regressions each.

For my two equations which should be placed in the plot I modified a code snippet from the internet and got following function:

lm_eqn = function(x){
m.Vs <- lm(log(Vs) ~ x, data = move);
m.Vm <- lm(log(Vm) ~ x, data = move);
eq.Vs <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,
list(a = format(coef(m.Vs)[1], digits = 2),
b = format(coef(m.Vs)[2], digits = 2),
r2 = format(summary(m.Vs)$r.squared, digits = 3)));
eq.Vm <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,
list(a = format(coef(m.Vm)[1], digits = 2),
b = format(coef(m.Vm)[2], digits = 2),
r2 = format(summary(m.Vm)$r.squared, digits = 3)));
rbind(as.character(as.expression(eq.Vs)),as.character(as.expression(eq.Vm)));
}


and then I use it in ggplot:

text.time <- lm_eqn(log(TIME))

ggplot(data.time, aes(x=vector.time,y=vector.distance,shape=groupcode))+
geom_point(size=3.5)+
geom_text(aes(0.2,100,label=text.time[1]))+
geom_text(aes(1,10,label=text.time[2])))

I get the equations to ggplot so it seems my function is working. But the result isn't satisfying... there are two problems:

1) The text is the code (italic(y)...) instead of the real text
2) The text isn't displayed nice...it is very blurry and it seems more like
pixcels than a text...

I attached a screenshot that you see what I see. I don't know what is causing the problem?

thank you

johannes

Bildschirmfoto 2011-11-09 um 11.08.51.png

Brandon Hurr

unread,
Nov 9, 2011, 5:24:32 AM11/9/11
to Johannes Radinger, Dennis Murphy, ggp...@googlegroups.com
I believe you need parse=TRUE in your geom_text() outside of the aes() and it should work. 

Brandon

Johannes Radinger

unread,
Nov 9, 2011, 5:44:09 AM11/9/11
to Brandon Hurr, ggp...@googlegroups.com, djm...@gmail.com
Hi Brandon,

thank you for that. parse=TRUE was the solution to my problem.

The problem with the rendering of the text seems to be a problem
of my display device. I am working on a Mac and in the standard
Quartz display the equation look terrible. It is getting better
when I use X11 but still no really nice. In comparison to X11, Quartz
is now very very slow in displaying the plot with parsed text.
When exporting to pdf, everything looks okay.

Best regards
Johannes

-------- Original-Nachricht --------
> Datum: Wed, 9 Nov 2011 10:24:32 +0000
> Von: Brandon Hurr <brando...@gmail.com>
> An: Johannes Radinger <JRad...@gmx.at>
> CC: Dennis Murphy <djm...@gmail.com>, ggp...@googlegroups.com
> Betreff: Re: Add correlation factors to scatterplot

Brandon Hurr

unread,
Nov 9, 2011, 5:54:34 AM11/9/11
to Johannes Radinger, ggp...@googlegroups.com, djm...@gmail.com
Got pictures?

Johannes Radinger

unread,
Nov 9, 2011, 6:31:39 AM11/9/11
to Brandon Hurr, djm...@gmail.com, ggp...@googlegroups.com
Hi again,


yes I got pictures in the display device. Attached there are the screenshots from Quartz and X11. X11 looks as I want it...just Quartz
behaves strange in displaying and is very slow.

Just some additional questions concerning my function:

lm_eqn = function(x){
m.Vs <- lm(log(Vs) ~ x, data = move);
m.Vm <- lm(log(Vm) ~ x, data = move);

eq.Vs <- substitute(italic(Vs) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,
list( a = format(coef(m.Vs)[1], digits = 2),

b = format(coef(m.Vs)[2], digits = 2),
r2 = format(summary(m.Vs)$r.squared, digits = 3)));

eq.Vm <- substitute(italic(Vm) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,

list(a = format(coef(m.Vm)[1], digits = 2),
b = format(coef(m.Vm)[2], digits = 2),
r2 = format(summary(m.Vm)$r.squared, digits = 3)));
rbind(as.character(as.expression(eq.Vs)),as.character(as.expression(eq.Vm)));
}

1) I'd like to use rather "italic(log(Vs))" instead of "italic(Vs)" as text in the equation, but it seems I need an additional thing so that log isn't interpreted as function log but as the word "log"

2) What exactly means the * after italic(x)? What does it?

3) I'd like to replace x in "italic(x)" by the expression I use when calling the function. E.g In the case of lm_eqn(log(TIME)), x in the equation text should be replaced by "log(TIME))

-------- Original-Nachricht --------
> Datum: Wed, 9 Nov 2011 10:54:34 +0000


> Von: Brandon Hurr <brando...@gmail.com>
> An: Johannes Radinger <JRad...@gmx.at>

> CC: ggp...@googlegroups.com, djm...@gmail.com

Display_Quartz.png
Display_X11.png
Reply all
Reply to author
Forward
0 new messages