首页 文章

在散点图上打印相关系数

提问于
浏览
1

我想在散点图上打印相关系数 . 我怎样才能用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

1 回答

  • 2

    对于像这样的一次性添加,请使用 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 参数,如上所示 .

相关问题