R语言数据可视化-条形图

一、概述

条形图(bar chart)在医学科技论文中经常有用到,它通过垂直的或水平的矩形展示分类变量的频数分布。 R语言使用函数 barplot() 创建条形图。 R 语言可以在条形图中绘制垂直和水平条。 在条形图中,每个条可以给予不同的颜色。

二、数据集

下面我将以vcd包里的Arthritis数据集为例介绍函数barplot()的用法。该数据集来自于一项关于治疗类风湿性关节炎新方法的成组对照双盲临床试验研究。其中的反应变量Improved记录了每位接受药物治疗(Treated,41例)或安慰剂(Placebo,43例)的患者的治疗效果,分为3个级别(None,Some和Marked)。

1. 引入vcd包

> install.packages('vcd')
--- 在此連線階段时请选用CRAN的鏡子 ---
还安装相依关系‘lmtest’

试开URL’https://mirrors.bfsu.edu.cn/CRAN/bin/windows/contrib/4.1/lmtest_0.9-39.zip'
Content type 'application/zip' length 414754 bytes (405 KB)
downloaded 405 KB

试开URL’https://mirrors.bfsu.edu.cn/CRAN/bin/windows/contrib/4.1/vcd_1.4-9.zip'
Content type 'application/zip' length 1294118 bytes (1.2 MB)
downloaded 1.2 MB

package ‘lmtest’ successfully unpacked and MD5 sums checked
package ‘vcd’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
        C:\Users\86166\AppData\Local\Temp\RtmpAVCrxk\downloaded_packages
> library(vcd)
载入需要的程辑包:grid
Warning message:
程辑包‘vcd’是用R版本4.1.2 来建造的

2. 引用和查看数据集

> Arthritis  #完整数据集数据查看
   ID Treatment    Sex Age Improved
1  57   Treated   Male  27     Some
2  46   Treated   Male  29     None
3  77   Treated   Male  30     None
4  17   Treated   Male  32   Marked
5  36   Treated   Male  46   Marked
6  23   Treated   Male  58   Marked
7  75   Treated   Male  59     None
8  39   Treated   Male  59   Marked
9  33   Treated   Male  63     None
10 55   Treated   Male  63     None
11 30   Treated   Male  64     None
12  5   Treated   Male  64     Some
13 63   Treated   Male  69     None
14 83   Treated   Male  70   Marked
15 66   Treated Female  23     None
16 40   Treated Female  32     None
17  6   Treated Female  37     Some
18  7   Treated Female  41     None
19 72   Treated Female  41   Marked
20 37   Treated Female  48     None
21 82   Treated Female  48   Marked
22 53   Treated Female  55   Marked
23 79   Treated Female  55   Marked
24 26   Treated Female  56   Marked
25 28   Treated Female  57   Marked
26 60   Treated Female  57   Marked
27 22   Treated Female  57   Marked
28 27   Treated Female  58     None
29  2   Treated Female  59   Marked
30 59   Treated Female  59   Marked
31 62   Treated Female  60   Marked
32 84   Treated Female  61   Marked
33 64   Treated Female  62     Some
34 34   Treated Female  62   Marked
35 58   Treated Female  66   Marked
36 13   Treated Female  67   Marked
37 61   Treated Female  68     Some
38 65   Treated Female  68   Marked
39 11   Treated Female  69     None
40 56   Treated Female  69     Some
41 43   Treated Female  70     Some
42  9   Placebo   Male  37     None
43 14   Placebo   Male  44     None
44 73   Placebo   Male  50     None
45 74   Placebo   Male  51     None
46 25   Placebo   Male  52     None
47 18   Placebo   Male  53     None
48 21   Placebo   Male  59     None
49 52   Placebo   Male  59     None
50 45   Placebo   Male  62     None
51 41   Placebo   Male  62     None
52  8   Placebo   Male  63   Marked
53 80   Placebo Female  23     None
54 12   Placebo Female  30     None
55 29   Placebo Female  30     None
56 50   Placebo Female  31     Some
57 38   Placebo Female  32     None
58 35   Placebo Female  33   Marked
59 51   Placebo Female  37     None
60 54   Placebo Female  44     None
61 76   Placebo Female  45     None
62 16   Placebo Female  46     None
63 69   Placebo Female  48     None
64 31   Placebo Female  49     None
65 20   Placebo Female  51     None
66 68   Placebo Female  53     None
67 81   Placebo Female  54     None
68  4   Placebo Female  54     None
69 78   Placebo Female  54   Marked
70 70   Placebo Female  55   Marked
71 49   Placebo Female  57     None
72 10   Placebo Female  57     Some
73 47   Placebo Female  58     Some
74 44   Placebo Female  59     Some
75 24   Placebo Female  59   Marked
76 48   Placebo Female  61     None
77 19   Placebo Female  63     Some
78  3   Placebo Female  64     None
79 67   Placebo Female  65   Marked
80 32   Placebo Female  66     None
81 42   Placebo Female  66     None
82 15   Placebo Female  66     Some
83 71   Placebo Female  68     Some
84  1   Placebo Female  74   Marked
> data(Arthritis)
> attach(Arthritis)
> counts <- table(Improved)
> counts # 分组统计数据
Improved
  None   Some Marked 
    42     14     28 

三、基本语法

在 R 语言中创建条形图的基本语法是 -

barplot(data, xlab, ylab, main, names.arg, col,horiz)

以下是所使用的参数的描述 -

  • data 是包含在条形图中使用的数值的向量或矩阵。
  • xlab 是 x 轴的标签。
  • ylab 是 y 轴的标签。
  • main 是条形图的标题。
  • names.arg 是在每个条下出现的名称的向量。
  • col 用于向图中的条形提供颜色。
  • horiz 是否垂直

1. 普通条形图

> library(vcd)
> data(Arthritis)
> attach(Arthritis)
> counts <- table(Improved)
> counts # 分组统计数据
Improved
  None   Some Marked 
    42     14     28 
barplot(counts,xlab="Improvment",ylab="Freqency",las=1)

R语言数据可视化-条形图_第1张图片

2. 二维列联表

> counts <- table(Improved,Treatment)
> counts
        Treatment
Improved Placebo Treated
  None        29      13
  Some         7       7
  Marked       7      21
> barplot(counts,col=c("red","yellow","green"),xlab="Improvment",ylab="Freqency",beside=TRUE,las=1) #画图
> legend("top",legend=rownames(counts),fill=c("red","yellow","green")) # 加标识

R语言数据可视化-条形图_第2张图片
R语言数据可视化-条形图_第3张图片

3. 组合条形图和堆积条形图

barplot(counts,main = "类风湿关节对照图",xlab = "Improvment",ylab = "Freqency",col = c("red","yellow","green"))

R语言数据可视化-条形图_第4张图片

4. 横向条形图

barplot(counts,main = "类风湿关节对照图",xlab = "Improvment",ylab = "Freqency",col = c("red","yellow","green"),horiz=T)
text(seq(from=0.7,length.out=20,by=1.2),x=-0.5,label=rev(z$word)) 

R语言数据可视化-条形图_第5张图片

5. 条形图展示均值、中位数、标准差和置信区间

> install.packages('epiDisplay')
试开URL’https://mirrors.bfsu.edu.cn/CRAN/bin/windows/contrib/4.1/epiDisplay_3.5.0.1.zip'
Content type 'application/zip' length 666111 bytes (650 KB)
downloaded 650 KB

package ‘epiDisplay’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
        C:\Users\86166\AppData\Local\Temp\RtmpAVCrxk\downloaded_packages
> library(epiDisplay)
载入需要的程辑包:foreign
载入需要的程辑包:survival
载入需要的程辑包:MASS
载入需要的程辑包:nnet
Warning message:
程辑包‘epiDisplay’是用R版本4.1.2 来建造的 
> aggregate.plot(anorexia$Postwt,by=list(anorexia$Treat),error="sd",legend=FALSE,bar.col=c("red","yellow","green"),ylim=c(0,100),las=1,main="")
> 

R语言数据可视化-条形图_第6张图片

你可能感兴趣的:(R语言,R语言,条形图)