上次我们简单介绍了gt
包的理念以及基本的用法,今天我们通过一个完整的示例详细说一下gt
包的各种用法!
使用内置的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~
支持和tidyverse
系列,比如使用group_by()
函数:
tab <- gtcars_8 %>%
group_by(ctry_origin) %>%
arrange(mfr, desc(msrp)) %>%
gt()
tab
image-20220514144606209
tab <-
tab %>%
cols_hide(columns = c(drivetrain, bdy_style)) %>% # 隐藏列
cols_move( # 移动列
columns = c(trsmn, mpg_c, mpg_h),
after = trim
)
tab
image-20220514144632159
列分组操作非常常见,有时我们需要一个小表头,把不同的列聚在一起。
可以通过tab_spanner()
函数实现:
tab <- tab %>%
tab_spanner(label = "Performance",
columns = c(mpg_c,mpg_h,hp,hp_rpm,trq,trq_rpm)
)
tab
image-20220514144700364
和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
image-20220514144727694
支持对数字、货币、日期时间等格式进行各种方便的格式化操作,使呈现方式更加专业、美观。
tab <-
tab %>%
fmt_currency(
columns = msrp,
currency = "USD",
decimals = 0
)
tab
image-20220514144752212
使用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
image-20220514144817101
可以使用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
image-20220514144842964
tab <-
tab %>%
tab_header(
title = md("The Cars of **gtcars**"),
subtitle = "These are some fine automobiles"
)
tab
image-20220514144913722
tab <-
tab %>%
tab_source_note(
source_note = md(
"Source: Various pages within the Edmonds website."
)
)
tab
image-20220514144939978
OK,这就是gt
包的常见功能了,通过2篇推文,相信你已经了解了gt
包的优势和强项,以及和之前介绍的comparegroups
、tableone
、table1
的区别!
恭喜你又掌握了一项新技能!
以上就是今天的内容,希望对你有帮助哦!