在散点图上添加pearson相关系数

时间:2013-12-29 08:39:13

标签: r plot

我制作了散点图,需要在图表的某处包含年龄和高度的皮尔逊相关系数。这是我已经完成的工作的一部分。

plot(survey$Age, survey$Height,main="A scatter plot of Age against Height",xlab="Age(years)",ylab="Height(cm)")

enter image description here

我使用的数据集在这里。调查

               Name    Sex Age Height Weight Forearm Pulse   Eyes     Pizza
1    Kaito McCarthy   Male  17    182     55      26    88 Purple  Mushroom
2      James Durand   Male  18    185    103      28    90  Brown   Spinach
3       Kaito Hajek   Male  20    171     49      25    92  Brown   Sausage
4      Liam Steiner   Male  18    176     58      25    68  Green   Sausage
5    Kevin Gonzalez   Male  21    158     57      23    68  Brown   Sausage
6     Momoko Durand Female  21    155     43      25    84 Purple   Spinach
7  Julian Takahashi   Male  18    199    114      29    74   Blue    Prawns
8  Fabian Rodriguez   Male  19    176     90      25    76  Brown  Mushroom
9    Samuel Kennedy   Male  17    191     68      29    86 Purple   Sausage
10 Vicente Carrasco   Male  23    176     52      25    88 Purple   Spinach
11       Nina Hajek Female  19    153     34      24    62 Purple Pineapple
12  Nanami Gonzalez Female  19    160     56      26    98 Purple   Spinach
13  Kazuya Carrasco   Male  19    170     47      25    82  Green  Mushroom
14     Tomas Durand   Male  22    178     62      25    76  Brown  Mushroom
15       Mary Simon Female  21    174     49      27    72  Brown Pineapple
16    James Bernard   Male  22    162     40      24    80 Purple Pineapple
17    Ciara Steiner Female  23    172     82      27    86 Purple Pineapple
18   Krishna Durand Female  19    185     99      28    78  Green Pineapple
19    Ana Rodriguez Female  18    168     66      25    72  Brown Pineapple
20  Daiki Takahashi Female  17    155     45      24    84  Brown Pineapple
21  Tomaz Rodriguez   Male  17    182     83      27    74  Brown   Spinach
22   Akane Gonzalez Female  17    164     42      25    90   Blue Pineapple
23      Neli Castro Female  18    162     73      26    72  Green   Spinach
24    Riku Carrasco   Male  18    185     68      28    82  Green   Sausage
25    Leonie Castro Female  18    146     50      23    84 Purple  Mushroom
26  Alejandra Lopez Female  23    169     47      25    74 Purple    Prawns
27      Lea Summers Female  18    160     51      24    58   Blue Pineapple
28       Ava Castro Female  18    170     69      25    72  Brown Pineapple
29    Tyler Steiner   Male  24    185     57      27    76 Purple  Mushroom
30   Jacob Carrasco   Male  24    167     46      24    78  Green   Sausage
31       Nina Lopez Female  25    169     47      26    64  Brown Pineapple
32   Kaeda Gonzalez Female  24    164     50      25    80  Green Pineapple
33     Rebeka Lopez Female  21    155     47     235    50  Brown  Mushroom
34     Mary Kennedy Female  24    158     37      24    76 Purple   Sausage
35      Leah Castro Female  23    177     88      27    80   Blue  Mushroom
36 Michelle Bernard Female  23    155     36      24    80   Blue Pineapple
37       Shin Lopez   Male  19    170     47      24    76  Green Pineapple
38   Rose Takahashi Female  21    170     48      26    60  Brown   Spinach
39   Emilie Steiner Female  23    160     74      25    82  Green  Mushroom
40    Nathan Durand   Male  21    180    100      26    86   Blue    Prawns
41  Jonas Takahashi   Male  19    186     95      27    80   Blue Pineapple
42  Nicolas Summers   Male  21    181     65      26    62  Green Pineapple
      Cheerful Kiss
1   Moderately   No
2   Moderately   No
3   Moderately  Yes
4  Quite a bit  Yes
5   Moderately  Yes
6   Moderately  Yes
7   Moderately  Yes
8   Moderately  Yes
9   Moderately   No
10 Quite a bit   No
11 Quite a bit  Yes
12  Moderately   No
13  Moderately  Yes
14  Moderately  Yes
15  Moderately   No
16 Quite a bit  Yes
17  Moderately   No
18 Quite a bit   No
19  Moderately   No
20  Moderately  Yes
21  Moderately   No
22  Moderately  Yes
23  Moderately   No
24  Moderately  Yes
25  Moderately  Yes
26  Moderately   No
27    A Little  Yes
28 Quite a bit  Yes
29 Quite a bit  Yes
30  Moderately  Yes
31 Quite a bit  Yes
32  Moderately  Yes
33  Moderately  Yes
34  Moderately  Yes
35    A Little  Yes
36  Moderately  Yes
37  Moderately   No
38  Moderately   No
39  Moderately  Yes
40 Quite a bit  Yes
41  Moderately  Yes
42  Moderately   No

2 个答案:

答案 0 :(得分:2)

我通常会使用legendtext向地图添加信息。 例如:

legend(x='bottomright', legend='Cor = 0.34')

在你的情况下,它会更像是这样:

legend(x='bottomright', legend=paste('Cor =',round(cor(survey$Age, survey$Height),2)))

答案 1 :(得分:0)

你可以把它放在标题附近的括号中

plot(survey$Age, survey$Height, main=paste0("Age vs Height (cor= ", formatC(cor(survey$Height,survey$Age), 3, format="f"),")"), xlab="Age(years)", ylab="Height(cm)")

或使用函数

作为副标题
title(sub=paste0("cor= ", formatC(cor(survey$Height,survey$Age), 3, format="f")))

你也可以把它作为文本放在散点图上,但是你应该注意不要与数据重叠