Steem 和 R 语言:跟大鹏学 R 语言(4)作图和统计 by dapeng

View this thread on steempeak.com
· @dapeng · (edited)
$10.39
Steem 和 R 语言:跟大鹏学 R 语言(4)作图和统计
前情回顾:
- [预告:“Steem 和 R 语言:跟大鹏学 R 语言”连载即将开始](https://steemit.com/cn/@dapeng/xuer-0)
- [Steem 和 R 语言:跟大鹏学 R 语言(1)下载安装](https://steemit.com/cn/@dapeng/xuer-1)
- [Steem 和 R 语言:跟大鹏学 R 语言(2)简单计算](https://steemit.com/cn/@dapeng/xuer-2)
- [Steem 和 R 语言:跟大鹏学 R 语言(3)变量和向量](https://steemit.com/cn/@dapeng/xuer-3)

本文选自《学 R:零基础学习 R 语言》(研究出版社,大鹏、李怡著,2018)第一章。

![](https://github.com/pzhaonet/keller/raw/master/figdapeng/xuer-cover.jpg)

## 1.3 第一张留影:作图

下面,让我们作出第一个图形来:Mauna Loa观测站的二氧化碳浓度时间序列。这是张全球闻名的明星图。承接前面的数据,我们只需敲7个键就行了(图 1.3):

```
plot(y)  # 作图

```



![](https://github.com/pzhaonet/keller/raw/master/figdapeng/fig-co2-1.png)

图 1.3: Mauna Loa 二氧化碳浓度.

是不是很简单?有没有很激动?简单的东西人见人爱。

本市的月降水量,也可以这样画出来:因为已经保存在变量x里了,所以`plot(x)`就可以了。

再进一步,我们来做统计运算,看看本市月降水量的平均值是多少。

```
(x[1] + x[2] + x[3] + x[4] + x[5] + x[6] + x[7] + x[8] + 
   x[9] + x[10] + x[11] + x[12]) / 12  # 计算平均值

```

```
## [1] 60.41667

```

这个式子很长,我们把这条指令强行写成了两行,R读完第一行发现指令不完整,就会自动读下一行。由于受版式的约束,本书的代码都会采用这种换行方式。实际写代码时不必这样换行。

x的12个元素逐个敲起来太麻烦了,可以用求和函数`sum()`以及求向量长度的函数`length()`,来简化代码:

```
sum(x)/length(x)

```

```
## [1] 60.41667

```

或者直接用平均值函数`mean()`:

```
mean(x)

```

```
## [1] 60.41667

```

更厉害的是`summary()`函数:

```
summary(x)

```

```
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   45.00   55.75   56.50   60.42   63.50   86.00

```

得到的六个数是最小值、25%分位数、中位数、均值、75%分位数和最大值。记住`summary()`这个明星函数吧,将来我们会反复享受使用这个函数的快乐。

上文我们用四种方法计算平均值,算出来的结果都一样,条条大路通罗马,想起哪个用哪个。

常用统计函数有:求和`sum()`,平均值`mean()`,最大值`max()`,最小值 `min()`,范围`range()`,中位数`median()`,分位数`quantile()`,标准差 `sd()`,方差`var()`,总结报告`summary()`。你可以把这些函数用在x上,看看结果都是什么。

**练习 1.2** 2003 年 8 月北京城区测得的 PM2.5 的质量浓度日变化, 从 0 时到 23 时 依次是97, 80, 64, 91, 87, 100, 128, 144, 150, 150, 150, 106, 78, 68, 62, 46, 55, 68, 84, 92, 95, 108, 128, 138 微克每立方米 。做出北京PM2.5的日变化图。计算PM2.5出现的最大值、最小值、平均值。最大值出现在几点钟?

最后,请在RStudio菜单栏点击 *File-Save*,或按快捷键ctrl+s,把刚才输入的代码保存到一个扩展名为.r的文件里,下一节接着用。这个.r文件 其实就是文本文件,用windows记事本打开就能看,只不过里面放的是r代码罢了。如果装了RStudio,双击 .r 文件就会用RStudio打开。

好啦,以上就是 R 的基本操作和运算、作图、统计分析,你全都掌握了! R 就差不多学完了!喝一杯庆祝一下吧。

**小贴士 1.3** 新手学R第一步

| 项目           | 内容                                      |
| -------------- | ----------------------------------------- |
| 安装           | CRAN, RStudio                             |
| 数学基本运算符 | `+`, `-`, `*`, `/`, `^`, `%%`, `%/%`      |
| 常用数学函数   | `round(), trunc(), sqrt(), abs(), exp(),` |
|                | `log(), log10(), sin(), asin()`           |
| 常用统计函数   | `sum(), mean(), max(), min(), range(),`   |
|                | `median(), sd(), var(), summary()`        |
| 作图           | `plot()`                                  |

##
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
post_id43,828,333
authordapeng
permlinkxuer-4
categorycn
json_metadata"{"links": ["https://steemit.com/cn/@dapeng/xuer-0", "https://steemit.com/cn/@dapeng/xuer-1", "https://steemit.com/cn/@dapeng/xuer-2", "https://steemit.com/cn/@dapeng/xuer-3"], "format": "markdown", "app": "steemit/0.1", "image": ["https://github.com/pzhaonet/keller/raw/master/figdapeng/xuer-cover.jpg"], "tags": ["cn", "r", "tutorial", "programming", "cn-programming"]}"
created2018-04-16 07:53:33
last_update2018-04-16 07:55:06
depth0
children4
net_rshares2,035,247,513,940
last_payout2018-04-23 07:53:33
cashout_time1969-12-31 23:59:59
total_payout_value8.306 SBD
curator_payout_value2.082 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length2,590
author_reputation64,235,894,740,263
root_title"Steem 和 R 语言:跟大鹏学 R 语言(4)作图和统计"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (41)
@prch ·
R 跟 python 用作統計 很多可能性及強大。


大鵬哥無私教各位,給你敬禮。
👍  
properties (23)
post_id43,829,868
authorprch
permlinkre-dapeng-xuer-4-20180416t080744942z
categorycn
json_metadata"{"app": "busy/2.4.0", "community": "busy", "tags": ["cn"]}"
created2018-04-16 08:07:48
last_update2018-04-16 08:07:48
depth1
children1
net_rshares2,230,304,503
last_payout2018-04-23 08:07:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length42
author_reputation4,320,714,352,634
root_title"Steem 和 R 语言:跟大鹏学 R 语言(4)作图和统计"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (1)
@dapeng ·
回礼!
properties (22)
post_id44,216,620
authordapeng
permlinkre-prch-re-dapeng-xuer-4-20180418t152327347z
categorycn
json_metadata"{"app": "steemit/0.1", "tags": ["cn"]}"
created2018-04-18 15:23:27
last_update2018-04-18 15:23:27
depth2
children0
net_rshares0
last_payout2018-04-25 15:23:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length3
author_reputation64,235,894,740,263
root_title"Steem 和 R 语言:跟大鹏学 R 语言(4)作图和统计"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@maiyude ·
默默学习中…………
👍  
properties (23)
post_id43,840,898
authormaiyude
permlinkre-dapeng-xuer-4-20180416t094826549z
categorycn
json_metadata"{"app": "steemit/0.1", "tags": ["cn"]}"
created2018-04-16 09:48:36
last_update2018-04-16 09:48:36
depth1
children1
net_rshares2,230,304,503
last_payout2018-04-23 09:48:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length9
author_reputation21,161,943,744,615
root_title"Steem 和 R 语言:跟大鹏学 R 语言(4)作图和统计"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (1)
@dapeng ·
点赞就行了
properties (22)
post_id44,216,729
authordapeng
permlinkre-maiyude-re-dapeng-xuer-4-20180418t152420740z
categorycn
json_metadata"{"app": "steemit/0.1", "tags": ["cn"]}"
created2018-04-18 15:24:21
last_update2018-04-18 15:24:21
depth2
children0
net_rshares0
last_payout2018-04-25 15:24:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length5
author_reputation64,235,894,740,263
root_title"Steem 和 R 语言:跟大鹏学 R 语言(4)作图和统计"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000