R绘图技巧24| 图中图— 在图片中插入表格(或者其它图片)

西瓜站长 生信益站 2023-07-04 08:00 发表于湖北
图片

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(01111), 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"))
图片
blank
图片
light
图片
classic
图片
minimal
图片
mBlue
# 只显示列名的边框
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(12), 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(24), 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(10.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,今天的分享到此为止,希望能对您有所帮助。

您的关注、点赞、在看、转发是对益站最大的鼓励和支持哈。

图片

联系站长

对本篇文章有疑问,可以在益站发消息留言,也欢迎各位童鞋来 QQ837738558 进行交流学习

图片
我们的企鹅群

收录于合集 #绘图专题
 105
上一篇R绘图技巧23| 3D条形图 | 祝站友们端午安康!下一篇R绘图技巧25| 10种方法教你画出最靓的桑基/冲积/河流图!

微信扫一扫
关注该公众号