❝R 语言
❞绘制表格
的意义在于数据可视化和信息传达。表格作为一种结构化的数据呈现方式,可以将复杂的数据整理成清晰、易读的形式,使我们能够更好地理解和分析数据。绘制图中图
的意义在于提供一种更丰富、更有信息密度的数据可视化方式。
图中图
(Inset plot)是将较小的图形嵌入到主要图形中的技术,可以通过在同一图像上展示不同尺度或相关数据,提供更全面的信息呈现。
# 加载 R 包
library(ggpubr) # ggtexttable
# 加载数据
df <- head(iris)
# 移除行名
ggtexttable(df, rows = NULL)
hjust = 0 , x = 0.1
,左对齐;hjust = 1 , x = 0.9
,右对齐。
table_theme <- ttheme(
tbody.style = tbody_style(
hjust = as.vector(matrix(c(0, 1, 1, 1, 1), ncol = 5, nrow = nrow(df), byrow = TRUE)),
x = as.vector(matrix(c(.1, .9, .9,.9, .9), ncol = 5, nrow = nrow(df), byrow = TRUE))
)
)
ggtexttable(df, rows = NULL, theme = table_theme)
# 默认表头和主体文本居中对齐,行名右对齐
tbody.style = tbody_style(color = "black",
fill = c("#e8f3de", "#d3e8bb"), hjust=1, x=0.9)
ggtexttable(head(iris), rows = NULL,
theme = ttheme(
colnames.style = colnames_style(color = "white", fill = "#8cc257"),
tbody.style = tbody.style
)
)
ggtexttable(df, rows = NULL, theme = ttheme("blank"))
# 只显示列名的边框
ggtexttable(df, rows = NULL, theme = ttheme("blank")) %>%
tab_add_hline(at.row = 1:2, row.side = "top", linewidth = 2)
# 表头颜色、表格主体颜色设置
ggtexttable(df, rows = NULL,
theme = ttheme(
colnames.style = colnames_style(color = "white", fill = "#8cc257"),
tbody.style = tbody_style(color = "black", fill = c("#e8f3de", "#d3e8bb"))
)
)
# 使用 RColorBrewer palette
ggtexttable(df,
theme = ttheme(
colnames.style = colnames_style(fill = "white"),
tbody.style = tbody_style(fill = get_palette("RdBu", 6))
)
)
# 单元格边框背景色、边框颜色、以及字体设置
tab <- ggtexttable(head(iris), rows = NULL,
theme = ttheme("classic"))
tab <- table_cell_font(tab, row = 3, column = 2,
face = "bold")
tab <- table_cell_bg(tab, row = 4, column = 3, linewidth = 5,
fill="darkolivegreen1", color = "darkolivegreen4")
tab
# 修改第3列的填充色和字体,除了第1行
tab <- ggtexttable(df, rows = NULL, theme = ttheme("classic"))
tab %>%
table_cell_bg(row = 2:tab_nrow(tab), column = 3, fill = "darkblue") %>%
table_cell_font(row = 2:tab_nrow(tab), column = 3, face = "italic", color = "white")
# 增加分割线
tab <- ggtexttable(df, theme = ttheme("blank"), rows = NULL)
tab %>%
# 第1,2,7行加横线
tab_add_hline(at.row = c(1, 2), row.side = "top", linewidth = 3, linetype = 1) %>%
tab_add_hline(at.row = c(7), row.side = "bottom", linewidth = 3, linetype = 1) %>%
# 第2-5列加分割线
tab_add_vline(at.column = 2:tab_ncol(tab), column.side = "left", from.row = 2, linetype = 2)
# 划掉一些单元格
tab %>%
tbody_add_border() %>%
thead_add_border() %>%
tab_cell_crossout(
row = c(2, 4), column = 3, linecolor = "red",
reduce.size.by = 0.6
)
# 添加标题和脚注
main.title <- "Edgar Anderson's Iris Data"
subtitle <- paste0(
"This famous (Fisher's or Anderson's) iris data set gives the measurements",
" in centimeters of the variables sepal length and width and petal length and width,",
" respectively, for 50 flowers from each of 3 species of iris.",
" The species are Iris setosa, versicolor, and virginica."
) %>%
strwrap(width = 80) %>%
paste(collapse = "\n")
tab <- ggtexttable(head(iris), theme = ttheme("light"))
tab %>%
tab_add_title(text = subtitle, face = "plain", size = 10) %>%
tab_add_title(text = main.title, face = "bold", padding = unit(0.1, "line")) %>%
tab_add_footnote(text = "*Table created using ggpubr", size = 10, face = "italic")
叠放图片:
# 图片组合表格
density.p <- ggdensity(iris, x = "Sepal.Length",
fill = "Species", palette = "jco")
stable <- desc_statby(iris, measure.var = "Sepal.Length",
grps = "Species")
stable <- stable[, c("Species", "length", "mean", "sd")]
stable.p <- ggtexttable(stable, rows = NULL,
theme = ttheme("mOrange"))
ggarrange(density.p, stable.p,
ncol = 1, nrow = 2,
heights = c(1, 0.5))
图中图:
density.p + annotation_custom(ggplotGrob(stable.p),
xmin = 6, ymin = 0.7,
xmax = 8)
ggtexttable() # 绘制文本表格。
ttheme() # 自定义表格主题。
rownames_style(), colnames_style(), tbody_style() # 自定义表行名称、列名称和正文的辅助函数。
table_cell_font() # 访问表格单元格以更改文本字体(大小和字体)。
table_cell_bg() # 访问表格单元格以更改背景(填充、颜色、线宽)。
tab_cell_crossout() # 划掉表格单元格。
tab_ncol(), tab_nrow() # 分别返回 ggtexttable 中的列数和行数。
tab_add_hline() # 在给定指定行的顶部或底部创建水平线或分隔符。
tab_add_vline() # 在给定指定列的右侧或左侧创建垂直线或分隔符。
tab_add_border(), tbody_add_border(), thead_add_border() # 给表格添加边框;tbody 代表表体,thead 代表表头。
tab_add_title(),tab_add_footnote() # 为表格添加标题、副标题和脚注。
OK,今天的分享到此为止,希望能对您有所帮助。
您的关注、点赞、在看、转发
是对益站最大的鼓励和支持
哈。
❝对本篇文章有疑问,可以在益站
❞发消息留言
,也欢迎各位童鞋来837738558
进行交流学习
。
微信扫一扫
关注该公众号