第 6 章 gt包画表

# 安装
remotes::install_github("rstudio/gt")
library('gt')
library('dplyr')

6.1 获取数据

islands_tbl <- dplyr::tibble(name=names(islands),size = islands) %>%
dplyr::arrange(desc(size)) %>%
dplyr::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

6.2 创建gt表格

gt_tbl <- gt(data = 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

6.3 添加标题

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
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.

6.4 添加脚注

# 添加带交叉引用的脚注
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.

6.5 添加左侧边栏

# 添加左侧边栏
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

6.6 分组

# 分组
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

6.7 总体呈现

# 全部放入一起
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.

6.8 其他

# 增加列组别
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")
  )

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
# 创建根
gt_tbl <-
islands_tbl %>%
gt(rowname_col = "name")
# 展示
gt_tbl
size
Asia 16988
Africa 11506
North America 9390
South America 6795
Antarctica 5500
Europe 3745
Australia 2968
Greenland 840
New Guinea 306
Borneo 280