48  gt绘制表格

前面用3篇推文详细介绍了三线表 & 基线资料表的绘制方法,分别介绍了compareGroupstableonetable1三个R包。

三线表是表格中的一种,以上3个R包是专门用来画三线表的,不过对于其他类型的表格就不太擅长了。

今天介绍的gt包则是专门为了表格而生的,gt是rstudio官方的作品,适合制作各式各样好看的表格。继承了tidyverse系列的优点,语法简介易懂,支持管道操作,支持markdown语法和HTML语法!

48.1 安装

# 2种方法选择1种
install.packages("gt")

devtools::install_github("rstudio/gt")

48.2 使用

gt包绘制表格的理念非常先进,和ggplot2绘制图形的理念有点像,都是一点点添加细节。一个完整的表格在gt包的设计理念中可以分为以下几个部分:

48.2.1 基础使用

library(gt)
library(dplyr)

# 使用islands_tbl数据集演示,数据集是关于陆地的大小
islands_tbl <- 
  tibble(
    name = names(islands),
    size = islands
  ) %>%
  arrange(desc(size)) %>% 
  slice(1:10)

islands_tbl
## # A tibble: 10 × 2
##    name           size
##    <chr>         <dbl>
##  1 Asia          16988
##  2 Africa        11506
##  3 North America  9390
##  4 South America  6795
##  5 Antarctica     5500
##  6 Europe         3745
##  7 Australia      2968
##  8 Greenland       840
##  9 New Guinea      306
## 10 Borneo          280

接下来制作一个简单的表格:

gt_tbl <- gt(islands_tbl)
gt_tbl
name size
Asia 16988
Africa 11506
North America 9390
South America 6795
Antarctica 5500
Europe 3745
Australia 2968
Greenland 840
New Guinea 306
Borneo 280

这就是一个简单表格。接下来我们就按照gt包分解表格的理念一步步添加各种细节。

48.2.2 添加标题

gt_tbl <- gt_tbl %>% 
  tab_header(
    title = "Large Landmasses of the World",
    subtitle = "The top ten largest are presented"
  )
gt_tbl
Large Landmasses of the World
The top ten largest are presented
name size
Asia 16988
Africa 11506
North America 9390
South America 6795
Antarctica 5500
Europe 3745
Australia 2968
Greenland 840
New Guinea 306
Borneo 280

更牛逼的是,这个标题支持markdown语法!

gt(islands_tbl[1:2,]) %>%
  tab_header(
    title = md("**Large Landmasses of the World**"),
    subtitle = md("The *top two* largest are presented")
  )
Large Landmasses of the World
The top two largest are presented
name size
Asia 16988
Africa 11506

48.2.3 添加脚注

使用tab_source_note()函数,同样也是支持markdown语法的。

gt_tbl <- 
  gt_tbl %>%
  tab_source_note(
    source_note = "Source: The World Almanac and Book of Facts, 1975, page 406."
  ) %>%
  tab_source_note(
    source_note = md("Reference: McNeil, D. R. (1977) *Interactive Data Analysis*. Wiley.")
  )

gt_tbl
Large Landmasses of the World
The top ten largest are presented
name size
Asia 16988
Africa 11506
North America 9390
South America 6795
Antarctica 5500
Europe 3745
Australia 2968
Greenland 840
New Guinea 306
Borneo 280
Source: The World Almanac and Book of Facts, 1975, page 406.
Reference: McNeil, D. R. (1977) Interactive Data Analysis. Wiley.

添加带交叉引用的脚注:

使用tab_footnote()函数,使用locations参数指定要添加角标的位置。

gt_tbl <- 
  gt_tbl %>%
  tab_footnote(
    footnote = "The Americas.",
    locations = cells_body(columns = name, rows = 3:4) # 在第3/4行,name这一列添加角标
  )

gt_tbl
Large Landmasses of the World The top ten largest are presented name size Asia 16988 Africa 11506 North America1 9390 South America1 6795 Antarctica 5500 Europe 3745 Australia 2968 Greenland 840 New Guinea 306 Borneo 280 Source: The World Almanac and Book of Facts, 1975, page 406. Reference: McNeil, D. R. (1977) Interactive Data Analysis. Wiley. 1 The Americas.

48.2.4 添加左侧边栏

如果还不清楚左侧边栏包含哪些信息,请翻看上面那张图。

gt_tbl <- islands_tbl %>% 
  gt(rowname_col = "name") %>% # 使用name这一列作为左侧边栏
  tab_stubhead(label = "landmass") # 添加左侧边栏的标题

gt_tbl
landmass size
Asia 16988
Africa 11506
North America 9390
South America 6795
Antarctica 5500
Europe 3745
Australia 2968
Greenland 840
New Guinea 306
Borneo 280

在上面展示的这几个陆地中,有一些是国家,有些是大洲,还有的是地区,下面我们把它分一下组:

gt_tbl <- gt_tbl %>% 
  tab_row_group( 
    label = "continent",
    rows = 1:6 # 1-6行是大洲
  ) %>% 
  tab_row_group(
    label = "country",
    rows = c("Australia", "Greenland")
  ) %>% 
  tab_row_group(
    label = "subregion",
    rows = c("New Guinea", "Borneo")
  )

gt_tbl
landmass size
subregion
New Guinea 306
Borneo 280
country
Australia 2968
Greenland 840
continent
Asia 16988
Africa 11506
North America 9390
South America 6795
Antarctica 5500
Europe 3745

我们把上面展示的元素全都添加在一起:

gt_tbl <- islands_tbl %>% 
  gt(rowname_col = "name") %>% 
  tab_stubhead(label = "landmass") %>% 
  tab_row_group(
    label = "continent",
    rows = 1:6
  ) %>%
  tab_row_group(
    label = "country",
    rows = c("Australia", "Greenland")
  ) %>%
  tab_row_group(
    label = "subregion",
    rows = c("New Guinea", "Borneo")
  ) %>% 
  tab_header(
    title = "Large Landmasses of the World",
    subtitle = "The top ten largest are presented"
  ) %>%
  tab_source_note(
    source_note = "Source: The World Almanac and Book of Facts, 1975, page 406."
  ) %>%
  tab_source_note(
    source_note = md("Reference: McNeil, D. R. (1977) *Interactive Data Analysis*. Wiley.")
  ) %>%
  tab_footnote(
    footnote = md("The **largest** by area."),
    locations = cells_body(
      columns = size, rows = 1
    )
  ) %>%
  tab_footnote(
    footnote = "The lowest by population.",
    locations = cells_body(
      columns = size, rows = contains("arc")
    )
  )
  
gt_tbl
Large Landmasses of the World The top ten largest are presented landmass size subregion New Guinea 306 Borneo 280 country Australia 2968 Greenland 840 continent Asia 1 16988 Africa 11506 North America 9390 South America 6795 Antarctica 2 5500 Europe 3745 Source: The World Almanac and Book of Facts, 1975, page 406. Reference: McNeil, D. R. (1977) Interactive Data Analysis. Wiley. 1 The largest by area. 2 The lowest by population.

48.2.5 增加列组别

对不同的列进行分组是非常常见的操作,gt包提供了tab_spanner()函数实现此功能:

gt_tbl <- 
  gt(airquality) %>%
  tab_header(
    title = "New York Air Quality Measurements",
    subtitle = "Daily measurements in New York City (May 1-10, 1973)"
  ) %>%
  tab_spanner(
    label = "Time",
    columns = c(Month, Day)
  ) %>%
  tab_spanner(
    label = "Measurement",
    columns = c(Ozone, Solar.R, Wind, Temp)
  )

gt_tbl
New York Air Quality Measurements
Daily measurements in New York City (May 1-10, 1973)
Measurement Time
Ozone Solar.R Wind Temp Month Day
41 190 7.4 67 5 1
36 118 8.0 72 5 2
12 149 12.6 74 5 3
18 313 11.5 62 5 4
NA NA 14.3 56 5 5
28 NA 14.9 66 5 6
23 299 8.6 65 5 7
19 99 13.8 59 5 8
8 19 20.1 61 5 9
NA 194 8.6 69 5 10
7 NA 6.9 74 5 11
16 256 9.7 69 5 12
11 290 9.2 66 5 13
14 274 10.9 68 5 14
18 65 13.2 58 5 15
14 334 11.5 64 5 16
34 307 12.0 66 5 17
6 78 18.4 57 5 18
30 322 11.5 68 5 19
11 44 9.7 62 5 20
1 8 9.7 59 5 21
11 320 16.6 73 5 22
4 25 9.7 61 5 23
32 92 12.0 61 5 24
NA 66 16.6 57 5 25
NA 266 14.9 58 5 26
NA NA 8.0 57 5 27
23 13 12.0 67 5 28
45 252 14.9 81 5 29
115 223 5.7 79 5 30
37 279 7.4 76 5 31
NA 286 8.6 78 6 1
NA 287 9.7 74 6 2
NA 242 16.1 67 6 3
NA 186 9.2 84 6 4
NA 220 8.6 85 6 5
NA 264 14.3 79 6 6
29 127 9.7 82 6 7
NA 273 6.9 87 6 8
71 291 13.8 90 6 9
39 323 11.5 87 6 10
NA 259 10.9 93 6 11
NA 250 9.2 92 6 12
23 148 8.0 82 6 13
NA 332 13.8 80 6 14
NA 322 11.5 79 6 15
21 191 14.9 77 6 16
37 284 20.7 72 6 17
20 37 9.2 65 6 18
12 120 11.5 73 6 19
13 137 10.3 76 6 20
NA 150 6.3 77 6 21
NA 59 1.7 76 6 22
NA 91 4.6 76 6 23
NA 250 6.3 76 6 24
NA 135 8.0 75 6 25
NA 127 8.0 78 6 26
NA 47 10.3 73 6 27
NA 98 11.5 80 6 28
NA 31 14.9 77 6 29
NA 138 8.0 83 6 30
135 269 4.1 84 7 1
49 248 9.2 85 7 2
32 236 9.2 81 7 3
NA 101 10.9 84 7 4
64 175 4.6 83 7 5
40 314 10.9 83 7 6
77 276 5.1 88 7 7
97 267 6.3 92 7 8
97 272 5.7 92 7 9
85 175 7.4 89 7 10
NA 139 8.6 82 7 11
10 264 14.3 73 7 12
27 175 14.9 81 7 13
NA 291 14.9 91 7 14
7 48 14.3 80 7 15
48 260 6.9 81 7 16
35 274 10.3 82 7 17
61 285 6.3 84 7 18
79 187 5.1 87 7 19
63 220 11.5 85 7 20
16 7 6.9 74 7 21
NA 258 9.7 81 7 22
NA 295 11.5 82 7 23
80 294 8.6 86 7 24
108 223 8.0 85 7 25
20 81 8.6 82 7 26
52 82 12.0 86 7 27
82 213 7.4 88 7 28
50 275 7.4 86 7 29
64 253 7.4 83 7 30
59 254 9.2 81 7 31
39 83 6.9 81 8 1
9 24 13.8 81 8 2
16 77 7.4 82 8 3
78 NA 6.9 86 8 4
35 NA 7.4 85 8 5
66 NA 4.6 87 8 6
122 255 4.0 89 8 7
89 229 10.3 90 8 8
110 207 8.0 90 8 9
NA 222 8.6 92 8 10
NA 137 11.5 86 8 11
44 192 11.5 86 8 12
28 273 11.5 82 8 13
65 157 9.7 80 8 14
NA 64 11.5 79 8 15
22 71 10.3 77 8 16
59 51 6.3 79 8 17
23 115 7.4 76 8 18
31 244 10.9 78 8 19
44 190 10.3 78 8 20
21 259 15.5 77 8 21
9 36 14.3 72 8 22
NA 255 12.6 75 8 23
45 212 9.7 79 8 24
168 238 3.4 81 8 25
73 215 8.0 86 8 26
NA 153 5.7 88 8 27
76 203 9.7 97 8 28
118 225 2.3 94 8 29
84 237 6.3 96 8 30
85 188 6.3 94 8 31
96 167 6.9 91 9 1
78 197 5.1 92 9 2
73 183 2.8 93 9 3
91 189 4.6 93 9 4
47 95 7.4 87 9 5
32 92 15.5 84 9 6
20 252 10.9 80 9 7
23 220 10.3 78 9 8
21 230 10.9 75 9 9
24 259 9.7 73 9 10
44 236 14.9 81 9 11
21 259 15.5 76 9 12
28 238 6.3 77 9 13
9 24 10.9 71 9 14
13 112 11.5 71 9 15
46 237 6.9 78 9 16
18 224 13.8 67 9 17
13 27 10.3 76 9 18
24 238 10.3 68 9 19
16 201 8.0 82 9 20
13 238 12.6 64 9 21
23 14 9.2 71 9 22
36 139 10.3 81 9 23
7 49 10.3 69 9 24
14 20 16.6 63 9 25
30 193 6.9 70 9 26
NA 145 13.2 77 9 27
14 191 14.3 75 9 28
18 131 8.0 76 9 29
20 223 11.5 68 9 30

支持随意更改列的位置以及HTML语法:

gt_tbl <- 
  gt_tbl %>%
  cols_move_to_start( # 移到前面去
    columns = c(Month, Day)
  ) %>%
  cols_label(
    Ozone = html("Ozone,<br>ppbV"),
    Solar.R = html("Solar R.,<br>cal/m<sup>2</sup>"),
    Wind = html("Wind,<br>mph"),
    Temp = html("Temp,<br>&deg;F")
  )

# Show the gt table
gt_tbl
New York Air Quality Measurements
Daily measurements in New York City (May 1-10, 1973)
Time Measurement
Month Day Ozone,
ppbV
Solar R.,
cal/m2
Wind,
mph
Temp,
°F
5 1 41 190 7.4 67
5 2 36 118 8.0 72
5 3 12 149 12.6 74
5 4 18 313 11.5 62
5 5 NA NA 14.3 56
5 6 28 NA 14.9 66
5 7 23 299 8.6 65
5 8 19 99 13.8 59
5 9 8 19 20.1 61
5 10 NA 194 8.6 69
5 11 7 NA 6.9 74
5 12 16 256 9.7 69
5 13 11 290 9.2 66
5 14 14 274 10.9 68
5 15 18 65 13.2 58
5 16 14 334 11.5 64
5 17 34 307 12.0 66
5 18 6 78 18.4 57
5 19 30 322 11.5 68
5 20 11 44 9.7 62
5 21 1 8 9.7 59
5 22 11 320 16.6 73
5 23 4 25 9.7 61
5 24 32 92 12.0 61
5 25 NA 66 16.6 57
5 26 NA 266 14.9 58
5 27 NA NA 8.0 57
5 28 23 13 12.0 67
5 29 45 252 14.9 81
5 30 115 223 5.7 79
5 31 37 279 7.4 76
6 1 NA 286 8.6 78
6 2 NA 287 9.7 74
6 3 NA 242 16.1 67
6 4 NA 186 9.2 84
6 5 NA 220 8.6 85
6 6 NA 264 14.3 79
6 7 29 127 9.7 82
6 8 NA 273 6.9 87
6 9 71 291 13.8 90
6 10 39 323 11.5 87
6 11 NA 259 10.9 93
6 12 NA 250 9.2 92
6 13 23 148 8.0 82
6 14 NA 332 13.8 80
6 15 NA 322 11.5 79
6 16 21 191 14.9 77
6 17 37 284 20.7 72
6 18 20 37 9.2 65
6 19 12 120 11.5 73
6 20 13 137 10.3 76
6 21 NA 150 6.3 77
6 22 NA 59 1.7 76
6 23 NA 91 4.6 76
6 24 NA 250 6.3 76
6 25 NA 135 8.0 75
6 26 NA 127 8.0 78
6 27 NA 47 10.3 73
6 28 NA 98 11.5 80
6 29 NA 31 14.9 77
6 30 NA 138 8.0 83
7 1 135 269 4.1 84
7 2 49 248 9.2 85
7 3 32 236 9.2 81
7 4 NA 101 10.9 84
7 5 64 175 4.6 83
7 6 40 314 10.9 83
7 7 77 276 5.1 88
7 8 97 267 6.3 92
7 9 97 272 5.7 92
7 10 85 175 7.4 89
7 11 NA 139 8.6 82
7 12 10 264 14.3 73
7 13 27 175 14.9 81
7 14 NA 291 14.9 91
7 15 7 48 14.3 80
7 16 48 260 6.9 81
7 17 35 274 10.3 82
7 18 61 285 6.3 84
7 19 79 187 5.1 87
7 20 63 220 11.5 85
7 21 16 7 6.9 74
7 22 NA 258 9.7 81
7 23 NA 295 11.5 82
7 24 80 294 8.6 86
7 25 108 223 8.0 85
7 26 20 81 8.6 82
7 27 52 82 12.0 86
7 28 82 213 7.4 88
7 29 50 275 7.4 86
7 30 64 253 7.4 83
7 31 59 254 9.2 81
8 1 39 83 6.9 81
8 2 9 24 13.8 81
8 3 16 77 7.4 82
8 4 78 NA 6.9 86
8 5 35 NA 7.4 85
8 6 66 NA 4.6 87
8 7 122 255 4.0 89
8 8 89 229 10.3 90
8 9 110 207 8.0 90
8 10 NA 222 8.6 92
8 11 NA 137 11.5 86
8 12 44 192 11.5 86
8 13 28 273 11.5 82
8 14 65 157 9.7 80
8 15 NA 64 11.5 79
8 16 22 71 10.3 77
8 17 59 51 6.3 79
8 18 23 115 7.4 76
8 19 31 244 10.9 78
8 20 44 190 10.3 78
8 21 21 259 15.5 77
8 22 9 36 14.3 72
8 23 NA 255 12.6 75
8 24 45 212 9.7 79
8 25 168 238 3.4 81
8 26 73 215 8.0 86
8 27 NA 153 5.7 88
8 28 76 203 9.7 97
8 29 118 225 2.3 94
8 30 84 237 6.3 96
8 31 85 188 6.3 94
9 1 96 167 6.9 91
9 2 78 197 5.1 92
9 3 73 183 2.8 93
9 4 91 189 4.6 93
9 5 47 95 7.4 87
9 6 32 92 15.5 84
9 7 20 252 10.9 80
9 8 23 220 10.3 78
9 9 21 230 10.9 75
9 10 24 259 9.7 73
9 11 44 236 14.9 81
9 12 21 259 15.5 76
9 13 28 238 6.3 77
9 14 9 24 10.9 71
9 15 13 112 11.5 71
9 16 46 237 6.9 78
9 17 18 224 13.8 67
9 18 13 27 10.3 76
9 19 24 238 10.3 68
9 20 16 201 8.0 82
9 21 13 238 12.6 64
9 22 23 14 9.2 71
9 23 36 139 10.3 81
9 24 7 49 10.3 69
9 25 14 20 16.6 63
9 26 30 193 6.9 70
9 27 NA 145 13.2 77
9 28 14 191 14.3 75
9 29 18 131 8.0 76
9 30 20 223 11.5 68

怎么样,绘制表格是不是非常方便呢?在进行数据展示的时候又多了一大利器!

还可以添加各种格式,比如更改颜色背景、数字增加标点符号、格式化日期等。

使用内置的gtcars数据集进行演示,这个数据集是根据mtcars数据改编而来。

library(gt)
library(dplyr)

glimpse(gtcars)
## Rows: 47
## Columns: 15
## $ mfr         <chr> "Ford", "Ferrari", "Ferrari", "Ferrari", "Ferrari", "Ferra…
## $ model       <chr> "GT", "458 Speciale", "458 Spider", "458 Italia", "488 GTB…
## $ year        <dbl> 2017, 2015, 2015, 2014, 2016, 2015, 2017, 2015, 2015, 2015…
## $ trim        <chr> "Base Coupe", "Base Coupe", "Base", "Base Coupe", "Base Co…
## $ bdy_style   <chr> "coupe", "coupe", "convertible", "coupe", "coupe", "conver…
## $ hp          <dbl> 647, 597, 562, 562, 661, 553, 680, 652, 731, 949, 573, 545…
## $ hp_rpm      <dbl> 6250, 9000, 9000, 9000, 8000, 7500, 8250, 8000, 8250, 9000…
## $ trq         <dbl> 550, 398, 398, 398, 561, 557, 514, 504, 509, 664, 476, 436…
## $ trq_rpm     <dbl> 5900, 6000, 6000, 6000, 3000, 4750, 5750, 6000, 6000, 6750…
## $ mpg_c       <dbl> 11, 13, 13, 13, 15, 16, 12, 11, 11, 12, 21, 16, 11, 16, 12…
## $ mpg_h       <dbl> 18, 17, 17, 17, 22, 23, 17, 16, 16, 16, 22, 22, 18, 20, 20…
## $ drivetrain  <chr> "rwd", "rwd", "rwd", "rwd", "rwd", "rwd", "awd", "awd", "r…
## $ trsmn       <chr> "7a", "7a", "7a", "7a", "7a", "7a", "7a", "7a", "7a", "7a"…
## $ ctry_origin <chr> "United States", "Italy", "Italy", "Italy", "Italy", "Ital…
## $ msrp        <dbl> 447000, 291744, 263553, 233509, 245400, 198973, 298000, 29…

为了方便演示,我们截取部分数据:

gtcars_8 <-
  gtcars %>%
  group_by(ctry_origin) %>%
  slice_head(n = 2) %>%
  ungroup() %>%
  filter(ctry_origin != "United Kingdom")

glimpse(gtcars_8)
## Rows: 8
## Columns: 15
## $ mfr         <chr> "BMW", "BMW", "Ferrari", "Ferrari", "Acura", "Nissan", "Fo…
## $ model       <chr> "6-Series", "i8", "458 Speciale", "458 Spider", "NSX", "GT…
## $ year        <dbl> 2016, 2016, 2015, 2015, 2017, 2016, 2017, 2016
## $ trim        <chr> "640 I Coupe", "Mega World Coupe", "Base Coupe", "Base", "…
## $ bdy_style   <chr> "coupe", "coupe", "coupe", "convertible", "coupe", "coupe"…
## $ hp          <dbl> 315, 357, 597, 562, 573, 545, 647, 650
## $ hp_rpm      <dbl> 5800, 5800, 9000, 9000, 6500, 6400, 6250, 6400
## $ trq         <dbl> 330, 420, 398, 398, 476, 436, 550, 650
## $ trq_rpm     <dbl> 1400, 3700, 6000, 6000, 2000, 3200, 5900, 3600
## $ mpg_c       <dbl> 20, 28, 13, 13, 21, 16, 11, 15
## $ mpg_h       <dbl> 30, 29, 17, 17, 22, 22, 18, 22
## $ drivetrain  <chr> "rwd", "awd", "rwd", "rwd", "awd", "awd", "rwd", "rwd"
## $ trsmn       <chr> "8am", "6am", "7a", "7a", "9a", "6a", "7a", "7m"
## $ ctry_origin <chr> "Germany", "Germany", "Italy", "Italy", "Japan", "Japan", …
## $ msrp        <dbl> 77300, 140700, 291744, 263553, 156000, 101770, 447000, 883…

48.3 分组操作

支持和tidyverse系列,比如使用group_by()函数:

tab <- gtcars_8 %>% 
  group_by(ctry_origin) %>% 
  arrange(mfr, desc(msrp)) %>% 
  gt()

tab
mfr model year trim bdy_style hp hp_rpm trq trq_rpm mpg_c mpg_h drivetrain trsmn msrp
Japan
Acura NSX 2017 Base Coupe coupe 573 6500 476 2000 21 22 awd 9a 156000
Nissan GT-R 2016 Premium Coupe coupe 545 6400 436 3200 16 22 awd 6a 101770
Germany
BMW i8 2016 Mega World Coupe coupe 357 5800 420 3700 28 29 awd 6am 140700
BMW 6-Series 2016 640 I Coupe coupe 315 5800 330 1400 20 30 rwd 8am 77300
United States
Chevrolet Corvette 2016 Z06 Coupe coupe 650 6400 650 3600 15 22 rwd 7m 88345
Ford GT 2017 Base Coupe coupe 647 6250 550 5900 11 18 rwd 7a 447000
Italy
Ferrari 458 Speciale 2015 Base Coupe coupe 597 9000 398 6000 13 17 rwd 7a 291744
Ferrari 458 Spider 2015 Base convertible 562 9000 398 6000 13 17 rwd 7a 263553

48.4 隐藏、移动某些列

tab <- 
  tab %>%
  cols_hide(columns = c(drivetrain, bdy_style)) %>% # 隐藏列
  cols_move( # 移动列
    columns = c(trsmn, mpg_c, mpg_h),
    after = trim
  )

tab
mfr model year trim trsmn mpg_c mpg_h hp hp_rpm trq trq_rpm msrp
Japan
Acura NSX 2017 Base Coupe 9a 21 22 573 6500 476 2000 156000
Nissan GT-R 2016 Premium Coupe 6a 16 22 545 6400 436 3200 101770
Germany
BMW i8 2016 Mega World Coupe 6am 28 29 357 5800 420 3700 140700
BMW 6-Series 2016 640 I Coupe 8am 20 30 315 5800 330 1400 77300
United States
Chevrolet Corvette 2016 Z06 Coupe 7m 15 22 650 6400 650 3600 88345
Ford GT 2017 Base Coupe 7a 11 18 647 6250 550 5900 447000
Italy
Ferrari 458 Speciale 2015 Base Coupe 7a 13 17 597 9000 398 6000 291744
Ferrari 458 Spider 2015 Base 7a 13 17 562 9000 398 6000 263553

48.5 列分组操作

列分组操作非常常见,有时我们需要一个小表头,把不同的列聚在一起。

可以通过tab_spanner()函数实现:

tab <- tab %>% 
  tab_spanner(label = "Performance",
              columns = c(mpg_c,mpg_h,hp,hp_rpm,trq,trq_rpm)
              )

tab
mfr model year trim trsmn Performance msrp
mpg_c mpg_h hp hp_rpm trq trq_rpm
Japan
Acura NSX 2017 Base Coupe 9a 21 22 573 6500 476 2000 156000
Nissan GT-R 2016 Premium Coupe 6a 16 22 545 6400 436 3200 101770
Germany
BMW i8 2016 Mega World Coupe 6am 28 29 357 5800 420 3700 140700
BMW 6-Series 2016 640 I Coupe 8am 20 30 315 5800 330 1400 77300
United States
Chevrolet Corvette 2016 Z06 Coupe 7m 15 22 650 6400 650 3600 88345
Ford GT 2017 Base Coupe 7a 11 18 647 6250 550 5900 447000
Italy
Ferrari 458 Speciale 2015 Base Coupe 7a 13 17 597 9000 398 6000 291744
Ferrari 458 Spider 2015 Base 7a 13 17 562 9000 398 6000 263553

48.6 合并列 & 添加标签

dplyr包中的union()函数功能差不多。

一次合并2列,第一列的列名会被保留,第2列的列名会被丢弃,默认使用{1} & {2}代替第一列、第二列,支持HTML语法

tab <- tab %>% 
  cols_merge(columns = c(mpg_c,mpg_h),
             pattern = "{1}c<br>{2}h" # html语法添加空格
             ) %>% 
  cols_merge(
    columns = c(hp, hp_rpm),
    pattern = "{1}<br>@{2}rpm" # html语法添加空格和文字
  ) %>%
  cols_merge(
    columns = c(trq, trq_rpm),
    pattern = "{1}<br>@{2}rpm"
  ) %>%
  cols_label(
    mpg_c = "MPG",
    hp = "HP",
    trq = "Torque",
    year = "Year",
    trim = "Trim",
    trsmn = "Transmission",
    msrp = "MSRP"
  )

tab
mfr model Year Trim Transmission Performance MSRP
MPG HP Torque
Japan
Acura NSX 2017 Base Coupe 9a 21c
22h
573
@6500rpm
476
@2000rpm
156000
Nissan GT-R 2016 Premium Coupe 6a 16c
22h
545
@6400rpm
436
@3200rpm
101770
Germany
BMW i8 2016 Mega World Coupe 6am 28c
29h
357
@5800rpm
420
@3700rpm
140700
BMW 6-Series 2016 640 I Coupe 8am 20c
30h
315
@5800rpm
330
@1400rpm
77300
United States
Chevrolet Corvette 2016 Z06 Coupe 7m 15c
22h
650
@6400rpm
650
@3600rpm
88345
Ford GT 2017 Base Coupe 7a 11c
18h
647
@6250rpm
550
@5900rpm
447000
Italy
Ferrari 458 Speciale 2015 Base Coupe 7a 13c
17h
597
@9000rpm
398
@6000rpm
291744
Ferrari 458 Spider 2015 Base 7a 13c
17h
562
@9000rpm
398
@6000rpm
263553

48.7 使用格式化功能

支持对数字、货币、日期时间等格式进行各种方便的格式化操作,使呈现方式更加专业、美观。

tab <- 
  tab %>%
  fmt_currency(
    columns = msrp,
    currency = "USD",
    decimals = 0
  )

tab
mfr model Year Trim Transmission Performance MSRP
MPG HP Torque
Japan
Acura NSX 2017 Base Coupe 9a 21c
22h
573
@6500rpm
476
@2000rpm
$156,000
Nissan GT-R 2016 Premium Coupe 6a 16c
22h
545
@6400rpm
436
@3200rpm
$101,770
Germany
BMW i8 2016 Mega World Coupe 6am 28c
29h
357
@5800rpm
420
@3700rpm
$140,700
BMW 6-Series 2016 640 I Coupe 8am 20c
30h
315
@5800rpm
330
@1400rpm
$77,300
United States
Chevrolet Corvette 2016 Z06 Coupe 7m 15c
22h
650
@6400rpm
650
@3600rpm
$88,345
Ford GT 2017 Base Coupe 7a 11c
18h
647
@6250rpm
550
@5900rpm
$447,000
Italy
Ferrari 458 Speciale 2015 Base Coupe 7a 13c
17h
597
@9000rpm
398
@6000rpm
$291,744
Ferrari 458 Spider 2015 Base 7a 13c
17h
562
@9000rpm
398
@6000rpm
$263,553

48.8 对齐方式及风格

使用cols_align()函数更改对齐方式; 使用tab_style()函数更改主题风格、颜色背景等

tab <- 
  tab %>%
  cols_align( # 某些列使用居中对齐
    align = "center",
    columns = c(mpg_c, hp, trq)
  ) %>%
  tab_style( # 更改字体外观
    style = cell_text(size = px(12),color="black"),
    locations = cells_body(
      columns = c(trim, trsmn, mpg_c, hp, trq)
    )
  )

tab
mfr model Year Trim Transmission Performance MSRP
MPG HP Torque
Japan
Acura NSX 2017 Base Coupe 9a 21c
22h
573
@6500rpm
476
@2000rpm
$156,000
Nissan GT-R 2016 Premium Coupe 6a 16c
22h
545
@6400rpm
436
@3200rpm
$101,770
Germany
BMW i8 2016 Mega World Coupe 6am 28c
29h
357
@5800rpm
420
@3700rpm
$140,700
BMW 6-Series 2016 640 I Coupe 8am 20c
30h
315
@5800rpm
330
@1400rpm
$77,300
United States
Chevrolet Corvette 2016 Z06 Coupe 7m 15c
22h
650
@6400rpm
650
@3600rpm
$88,345
Ford GT 2017 Base Coupe 7a 11c
18h
647
@6250rpm
550
@5900rpm
$447,000
Italy
Ferrari 458 Speciale 2015 Base Coupe 7a 13c
17h
597
@9000rpm
398
@6000rpm
$291,744
Ferrari 458 Spider 2015 Base 7a 13c
17h
562
@9000rpm
398
@6000rpm
$263,553

48.9 主体的字体美化

可以使用text_transform()函数继续美化cell_body部分的字体。

tab <- 
  tab %>%
  text_transform(
    locations = cells_body(columns = trsmn), # 定位需要美化的位置transmission列
    fn = function(x) {
      
      # transmission这一列中每行的第一个字符表示speed
      speed <- substr(x, 1, 1)
      
      # 第2-3个字符表示type,共分成4中type
      type <-
        dplyr::case_when(
          substr(x, 2, 3) == "am" ~ "Automatic/Manual",
          substr(x, 2, 2) == "m" ~ "Manual",
          substr(x, 2, 2) == "a" ~ "Automatic",
          substr(x, 2, 3) == "dd" ~ "Direct Drive"
        )
      
      # 把speed和type拼在一起
      paste(speed, " Speed<br><em>", type, "</em>")
    }
  )

tab
mfr model Year Trim Transmission Performance MSRP
MPG HP Torque
Japan
Acura NSX 2017 Base Coupe 9 Speed
Automatic
21c
22h
573
@6500rpm
476
@2000rpm
$156,000
Nissan GT-R 2016 Premium Coupe 6 Speed
Automatic
16c
22h
545
@6400rpm
436
@3200rpm
$101,770
Germany
BMW i8 2016 Mega World Coupe 6 Speed
Automatic/Manual
28c
29h
357
@5800rpm
420
@3700rpm
$140,700
BMW 6-Series 2016 640 I Coupe 8 Speed
Automatic/Manual
20c
30h
315
@5800rpm
330
@1400rpm
$77,300
United States
Chevrolet Corvette 2016 Z06 Coupe 7 Speed
Manual
15c
22h
650
@6400rpm
650
@3600rpm
$88,345
Ford GT 2017 Base Coupe 7 Speed
Automatic
11c
18h
647
@6250rpm
550
@5900rpm
$447,000
Italy
Ferrari 458 Speciale 2015 Base Coupe 7 Speed
Automatic
13c
17h
597
@9000rpm
398
@6000rpm
$291,744
Ferrari 458 Spider 2015 Base 7 Speed
Automatic
13c
17h
562
@9000rpm
398
@6000rpm
$263,553

48.10 标题和副标题

tab <- 
  tab %>%
  tab_header(
    title = md("The Cars of **gtcars**"),
    subtitle = "These are some fine automobiles"
  )

tab
The Cars of gtcars
These are some fine automobiles
mfr model Year Trim Transmission Performance MSRP
MPG HP Torque
Japan
Acura NSX 2017 Base Coupe 9 Speed
Automatic
21c
22h
573
@6500rpm
476
@2000rpm
$156,000
Nissan GT-R 2016 Premium Coupe 6 Speed
Automatic
16c
22h
545
@6400rpm
436
@3200rpm
$101,770
Germany
BMW i8 2016 Mega World Coupe 6 Speed
Automatic/Manual
28c
29h
357
@5800rpm
420
@3700rpm
$140,700
BMW 6-Series 2016 640 I Coupe 8 Speed
Automatic/Manual
20c
30h
315
@5800rpm
330
@1400rpm
$77,300
United States
Chevrolet Corvette 2016 Z06 Coupe 7 Speed
Manual
15c
22h
650
@6400rpm
650
@3600rpm
$88,345
Ford GT 2017 Base Coupe 7 Speed
Automatic
11c
18h
647
@6250rpm
550
@5900rpm
$447,000
Italy
Ferrari 458 Speciale 2015 Base Coupe 7 Speed
Automatic
13c
17h
597
@9000rpm
398
@6000rpm
$291,744
Ferrari 458 Spider 2015 Base 7 Speed
Automatic
13c
17h
562
@9000rpm
398
@6000rpm
$263,553

48.11 添加脚注

tab <- 
  tab %>%
  tab_source_note(
    source_note = md(
      "Source: Various pages within the Edmonds website."
    )
  )

tab
The Cars of gtcars
These are some fine automobiles
mfr model Year Trim Transmission Performance MSRP
MPG HP Torque
Japan
Acura NSX 2017 Base Coupe 9 Speed
Automatic
21c
22h
573
@6500rpm
476
@2000rpm
$156,000
Nissan GT-R 2016 Premium Coupe 6 Speed
Automatic
16c
22h
545
@6400rpm
436
@3200rpm
$101,770
Germany
BMW i8 2016 Mega World Coupe 6 Speed
Automatic/Manual
28c
29h
357
@5800rpm
420
@3700rpm
$140,700
BMW 6-Series 2016 640 I Coupe 8 Speed
Automatic/Manual
20c
30h
315
@5800rpm
330
@1400rpm
$77,300
United States
Chevrolet Corvette 2016 Z06 Coupe 7 Speed
Manual
15c
22h
650
@6400rpm
650
@3600rpm
$88,345
Ford GT 2017 Base Coupe 7 Speed
Automatic
11c
18h
647
@6250rpm
550
@5900rpm
$447,000
Italy
Ferrari 458 Speciale 2015 Base Coupe 7 Speed
Automatic
13c
17h
597
@9000rpm
398
@6000rpm
$291,744
Ferrari 458 Spider 2015 Base 7 Speed
Automatic
13c
17h
562
@9000rpm
398
@6000rpm
$263,553
Source: Various pages within the Edmonds website.

OK,手工,这就是gt包的常见功能了。