赞
踩
箱线图(bar plot)又叫箱须图(box-whisker plot)在医学科技论文中经常有用到,用于展示数据的大致分布特征,也用于探索异常值和离群点。平行排列的箱线图可以用于比较在某个分类变量各个类别下某指标的分布。R语言使用函数 barplot() 创建箱线图。
下面我将以R语言自带的anorexia数据集为例介绍函数barplot()的用法。该数据集来自于一项关于不同治疗方式下体重变化的临床试验研究。其中的反应变量wt.change记录了每位接受Cont、CBT和FT治疗方式下的治疗效果。
> anorexia
Treat Prewt Postwt wt.change
1 Cont 80.7 80.2 -0.5
2 Cont 89.4 80.1 -9.3
3 Cont 91.8 86.4 -5.4
4 Cont 74.0 86.3 12.3
5 Cont 78.1 76.1 -2.0
6 Cont 88.3 78.1 -10.2
7 Cont 87.3 75.1 -12.2
8 Cont 75.1 86.7 11.6
9 Cont 80.6 73.5 -7.1
10 Cont 78.4 84.6 6.2
11 Cont 77.6 77.4 -0.2
12 Cont 88.7 79.5 -9.2
13 Cont 81.3 89.6 8.3
14 Cont 78.1 81.4 3.3
15 Cont 70.5 81.8 11.3
16 Cont 77.3 77.3 0.0
17 Cont 85.2 84.2 -1.0
18 Cont 86.0 75.4 -10.6
19 Cont 84.1 79.5 -4.6
20 Cont 79.7 73.0 -6.7
21 Cont 85.5 88.3 2.8
22 Cont 84.4 84.7 0.3
23 Cont 79.6 81.4 1.8
24 Cont 77.5 81.2 3.7
25 Cont 72.3 88.2 15.9
26 Cont 89.0 78.8 -10.2
27 CBT 80.5 82.2 1.7
28 CBT 84.9 85.6 0.7
29 CBT 81.5 81.4 -0.1
30 CBT 82.6 81.9 -0.7
31 CBT 79.9 76.4 -3.5
32 CBT 88.7 103.6 14.9
33 CBT 94.9 98.4 3.5
34 CBT 76.3 93.4 17.1
35 CBT 81.0 73.4 -7.6
36 CBT 80.5 82.1 1.6
37 CBT 85.0 96.7 11.7
38 CBT 89.2 95.3 6.1
39 CBT 81.3 82.4 1.1
40 CBT 76.5 72.5 -4.0
41 CBT 70.0 90.9 20.9
42 CBT 80.4 71.3 -9.1
43 CBT 83.3 85.4 2.1
44 CBT 83.0 81.6 -1.4
45 CBT 87.7 89.1 1.4
46 CBT 84.2 83.9 -0.3
47 CBT 86.4 82.7 -3.7
48 CBT 76.5 75.7 -0.8
49 CBT 80.2 82.6 2.4
50 CBT 87.8 100.4 12.6
51 CBT 83.3 85.2 1.9
52 CBT 79.7 83.6 3.9
53 CBT 84.5 84.6 0.1
54 CBT 80.8 96.2 15.4
55 CBT 87.4 86.7 -0.7
56 FT 83.8 95.2 11.4
57 FT 83.3 94.3 11.0
58 FT 86.0 91.5 5.5
59 FT 82.5 91.9 9.4
60 FT 86.7 100.3 13.6
61 FT 79.6 76.7 -2.9
62 FT 76.9 76.8 -0.1
63 FT 94.2 101.6 7.4
64 FT 73.4 94.9 21.5
65 FT 80.5 75.2 -5.3
66 FT 81.6 77.8 -3.8
67 FT 82.1 95.5 13.4
68 FT 77.6 90.7 13.1
69 FT 83.5 92.5 9.0
70 FT 89.9 93.8 3.9
71 FT 86.0 91.7 5.7
72 FT 87.3 98.0 10.7
> attach(anorexia)
> counts <- table(Treat)
> counts
Treat
CBT Cont FT
29 26 17
在 R 语言中创建条形图的基本语法是
## 分类s3方法
boxplot(formula, data = NULL, ..., subset, na.action = NULL,
xlab = mklab(y_var = horizontal),
ylab = mklab(y_var =!horizontal),
add = FALSE, ann = !add, horizontal = FALSE,
drop = FALSE, sep = ".", lex.order = FALSE)
## 默认 S3 方法:
boxplot(x, ..., range = 1.5, width = NULL, varwidth = FALSE,
notch = FALSE, outline = TRUE, names, plot = TRUE,
border = par("fg"), col = "lightgray", log = "",
pars = list(boxwex = 0.8, staplewex = 0.5, outwex = 0.5),
ann = !add, horizontal = FALSE, add = FALSE, at = NULL)
以下是所使用的参数的描述 -
boxplot(wt.change~Treat,data=anorexia,ylab="体重变化(lbs)",xlab="治疗方式",las =1)
> boxplot(wt.change~Treat,notch=TRUE,data=anorexia,ylab="体重变化(lbs)",xlab="治疗方式",las =1)
Warning message:
In (function (z, notch = FALSE, width = NULL, varwidth = FALSE, :
some notches went outside hinges ('box'): maybe set notch=FALSE
> boxplot(wt.change~Treat,varwidth=TRUE,data=anorexia,xlab="体重变化(lbs)",ylab="治疗方式",las =1,col = c("red","yellow","green"),horizontal=T)
>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。