其他
R如何优雅的绘制精美表格
最近在做数据分析需要整理一张很复杂的表格信息,就尝试着能不能用R语言来实现,果然它没有让我失望,下面就来介绍如何通过R代码来绘制一张精美的表格
加载R包
library(tidyverse)
library(gt)
绘制表格
这里还是使用我们熟悉的 iris 数据集,直接将表格数据传递给 gt() 会绘制一张简单的线图;下面让我们在此基础上一步一步来进行美化加工
p <- iris %>% head(10) %>% gt()
加粗标题
tab_style() 对所有标题都进行加粗,tab_spanner() 对标题进行汇总
p1 <- p %>% tab_style(style = list(cell_text(weight = "bold")),
locations = cells_column_labels(everything())) %>%
tab_spanner(label=md("**<span style = 'color:#0072B2;'>Sepal</span><sup>[1]</sup>**"),
columns=1:2) %>%
tab_spanner(label=md("**Petal.Length**"),columns=3:4) %>%
cols_label(Sepal.Length= md("Sepal.Length<sup>[1]</sup>"),Species = md("Species<br> "))
修改线条类型
p1 %>% fmt_number(columns=1:4,decimals= 2) %>%
cols_align(align = "center",columns = 1:5) %>%
cols_width(everything() ~ px(120)) %>%
tab_style(style = list(cell_text(color = "white"),
cell_fill(color = scales::alpha("#1E90FF",0.7))),
locations = list(cells_body(columns = 2,rows = `Sepal.Width` < 3),
cells_body(columns = 3,rows = `Petal.Width` < 0.4))) %>%
tab_header(title = md("**iris**"),subtitle = md("**R优雅的绘制表格**")) %>%
tab_options(
column_labels.border.top.color = "grey",
column_labels.border.top.width = px(2.5),
column_labels.border.bottom.color = "black",
column_labels.border.bottom.width=px(2.5),
table_body.hlines.color = "black",
table_body.hlines.width=px(1),
table.border.bottom.color = "grey",
table.border.bottom.width = px(2.5),
data_row.padding = px(10)) %>%
tab_footnote(
"Data: iris 2021-10-04",
locations = cells_column_labels(5)) %>%
tab_source_note(md("**Table: R语言分析指南**"))```
fmt_number() 控制数值刻度 cols_align() 列字符位置,可以设置居中或左对齐 cols_width() 设置列宽 tab_options() 设置线条粗细
完整版代码
iris %>% head(10) %>% gt() %>%
tab_style(style = list(cell_text(weight = "bold")),
locations = cells_column_labels(everything())) %>%
tab_spanner(label=md("**<span style = 'color:#0072B2;'>Sepal</span><sup>[1]</sup>**"),
columns=1:2) %>%
tab_spanner(label=md("**Petal.Length**"),columns=3:4) %>%
cols_label(Sepal.Length= md("Sepal.Length<sup>[1]</sup>"),
Species = md("Species<br> ")) %>%
fmt_number(columns=1:4,decimals= 2) %>%
cols_align(align = "center",columns = 1:5) %>%
cols_width(everything() ~ px(120)) %>%
tab_style(style = list(cell_text(color = "white"),
cell_fill(color = scales::alpha("#1E90FF",0.7))),
locations = list(cells_body(columns = 2,rows = `Sepal.Width` < 3),
cells_body(columns = 3,rows = `Petal.Width` < 0.4))) %>%
tab_header(title = md("**iris**"),subtitle = md("**R优雅的绘制表格**")) %>%
tab_options(
column_labels.border.top.color = "grey",
column_labels.border.top.width = px(2.5),
column_labels.border.bottom.color = "black",
column_labels.border.bottom.width=px(2.5),
table_body.hlines.color = "black",
table_body.hlines.width=px(1),
table.border.bottom.color = "grey",
table.border.bottom.width = px(2.5),
data_row.padding = px(10)) %>%
tab_footnote(
"Data: iris 2021-10-04",
locations = cells_column_labels(5)) %>%
tab_source_note(md("**Table: R语言分析指南**"))
喜欢的小伙伴欢迎扫描下方二维码加入我的qq交流群,或者加我微信邀请你加入微信交流群,后台回复20211004获取本文代码
关注下方公众号下回更新不迷路,如需要加入微信交流群,请在菜单栏处添加作者微信,备注单位+方向+姓名即可邀您进群