-1

Possible Duplicate:
ggplot2: Adding Regression Line Equation and R2 on graph

I used the following program to plot a scatterplot with regression line. But I would like to calculate the correlation coefficient also. How can I get it with ggplot2?How can I add regression line equation and correlation coefficient (R) on the scatterplot?

library(ggplot2)
d <- data.frame(x=c (1.0325477, 0.6746901, 1.0845737, 1.1123872, 1.1060822, 0.8595918, 0.8512941, 1.0148842, 1.0722369, 0.9019220 , 0.8809147, 1.0358256, 0.9903858, 1.0715174 , 1.1034405, 1.0143966,0.9802365, 0.7177169 , 0.9190783, 0.8408701 ) ,
    y= c (0.8550177, 0.8352162 ,1.0236998, 1.1071665, 0.6768144, 0.8449983 ,0.7616483, 0.8259199, 1.1539598, 1.4125006, 1.0511816, 0.9366184, 1.4101268, 1.2937913, 1.4147219 ,1.2943105 ,0.7859749, 0.6689330, 0.6940164, 0.8093392), names = c("A","C","E","D","G","F","I","H","K","M","L","N","Q","P","S","R","T","W","V","Y"))
ggplot(d, aes(x,y)) + geom_point(shape=1)+geom_smooth(method=lm, se=FALSE, fullrange=T)+geom_text(aes(label=names ,hjust=0))

Please help me.

Community
  • 1
  • 1
lara
  • 503
  • 5
  • 10
  • 15
  • 4
    The R function to calculate correlations is `cor()`. And to find help when you don't know the name of the function, there is always `library(sos); findFn("correlation")` – Andrie Mar 12 '12 at 09:26

1 Answers1

1

The normal way of calculating the correlation coefficient and a linear regression is to that outside ggplot. Under the hood ggplot2 calls the lm, at least when method = lm. The solution to your problem is to use a mix of lm, cor, and geom_text. The post mentiond by @joran in the comments provides some good tips.

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149