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
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
HTH,
Dennis
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.
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
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
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