[英]Print correlation coefficient on scatterplot
I would like to print the correlation coefficient on scatter plot. 我想在散点图上打印相关系数。 How can I achieve with ggplot2.
如何使用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 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
. 对于这样的一次性添加,请使用
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. 根据要在图上的位置调整x和y值。 To change the font size, use the
size
argument, as shown above. 要更改字体大小,请使用
size
参数,如上所示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.