繁体   English   中英

在散点图上打印相关系数

[英]Print correlation coefficient on scatterplot

我想在散点图上打印相关系数。 如何使用ggplot2实现。 我的代码如下所示

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

对于这样的一次性添加,请使用annotate 在您的情况下:

annotate(x=1.5, y=1.5, 
         label=paste("R = ", round(cor(df$male, df$female),2)), 
         geom="text", size=5)

根据要在图上的位置调整x和y值。 要更改字体大小,请使用size参数,如上所示。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM