分享

使用R语言创建好看的表格

 阿越就是我 2023-10-12 发布于上海
     Try to learn everything about something! 

前面用3篇推文详细介绍了三线表 & 基线资料表的绘制方法,分别介绍了CompareGroupstableonetable1三个R包。点击以下链接直达:

使用compareGroups包1行代码生成基线资料表 

tableone?table1?傻傻分不清楚

使用R语言快速绘制三线表

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

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


  • 安装

  • 使用

    • 基础使用

    • 添加标题

    • 添加脚注

    • 添加左侧边栏

    • 增加列组别

安装

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

devtools::install_github("rstudio/gt")

使用

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

Snipaste_2022-05-13_21-19-28

基础使用

library(gt)
library(dplyr)
## 
## 载入程辑包:'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union

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

islands_tbl
## # A tibble: 10 x 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
image-20220514143936285

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

添加标题

gt_tbl <- gt_tbl %>% 
  tab_header(
    title = "Large Landmasses of the World",
    subtitle = "The top ten largest are presented"
  )
gt_tbl
image-20220514144019071

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

gt(islands_tbl[1:2,]) %>%
  tab_header(
    title = md("**Large Landmasses of the World**"),
    subtitle = md("The *top two* largest are presented")
  )
image-20220514144044188

添加脚注

使用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

添加带交叉引用的脚注:

使用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
image-20220514144151112

添加左侧边栏

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

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

gt_tbl
image-20220514144232560

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

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
image-20220514144311038

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

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
image-20220514144349391

增加列组别

对不同的列进行分组是非常常见的操作,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
image-20220514144443987

支持随意更改列的位置以及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
image-20220514144512588

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

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

以上是基本功能演示,还有更多功能大家可以去官网[1]学习,或者等我更新~

参考资料

[1]

gt官网: https://gt./

以上就是今天的内容,希望对你有帮助哦!欢迎点赞、在看、关注、转发

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多