I would like to print the correlation coefficient on scatter plot. How can I achieve with ggplot2. My code is shown below
df = read.csv ("/home/miya/Desktop/data.csv")
p <- ggplot(df, aes(male,female,label=names)) + xlim(0,2) + ylim(0,2) + xlab("x axis") +
ylab("y axis") + geom_point(colour = "red", size = 3) + geom_smooth(method=lm, fullrange=TRUE) + geom_text(hjust=-1,vjust=1)
data.csv
names male female
aa 1.43 1.3
kk 1.24 1.05
cc 1.04 0.94
dd 0.9 1.01
nn 0.93 1.17
mm 1.03 1.22
hh 1.12 1
gg 0.69 0.78
tt 0.92 1.04
For one-off additions like this, use annotate. In your case:
annotate(x=1.5, y=1.5,
label=paste("R = ", round(cor(df$male, df$female),2)),
geom="text", size=5)
Adjust x and y values depending on where you want it located on the plot. To change the font size, use the size argument, as shown above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With