MicrobiomeStatPlot |华夫饼图教程Waffle Chart Tutorial

学术   2024-12-21 09:34   广东  

华夫饼图简介

华夫饼图(waffle chart)分为块状华夫饼图和点状华夫饼图。它是展示总数据的组类别情况的一种有效图表。它是西方的一种由小方格组成的面包,所以这种图表因此得名为华夫饼图。

参考:https://mp.weixin.qq.com/s/9zW4EupcGSNoDH_vy23fPw https://mp.weixin.qq.com/s/0iztINEslPhhIyDXbh_I3w

标签:#微生物组数据分析  #MicrobiomeStatPlot   #R语言可视化 #华夫饼图 #Waffle chartt

作者:First draft(初稿):Defeng Bai(白德凤);Proofreading(校对):Ma Chuang(马闯) and Jiani Xun(荀佳妮);Text tutorial(文字教程):Defeng Bai(白德凤)

源代码及测试数据链接:

https://github.com/YongxinLiu/MicrobiomeStatPlot/项目中目录 3.Visualization_and_interpretation/WaffleChart

或公众号后台回复“MicrobiomeStatPlot”领取


华夫饼图案例

这是来自于范德堡大学医学中心Paula J. Hurley课题组2022年发表于Nature Communications上的一篇论文。论文题目为:Single cell analysis of cribriform prostate cancer reveals cell intrinsic and tumor microenvironmental pathways of aggressive disease. https://doi.org/10.1038/s41467-022-33780-1

图 1c |  患者临床特征。

结果

总体而言,患者患有 2-5 级前列腺癌,即 pT3aN0/X 期或 pT3bN0 期(图 1c 和补充表 1)。

华夫饼图R语言实战

源代码及测试数据链接:

https://github.com/YongxinLiu/MicrobiomeStatPlot/

或公众号后台回复“MicrobiomeStatPlot”领取

软件包安装

# 基于CRAN安装R包,检测没有则安装p_list = c("ggplot2", "RColorBrewer", "ggforce", "dplyr", "waffle")for(p in p_list){if (!requireNamespace(p)){install.packages(p)}    library(p, character.only = TRUE, quietly = TRUE, warn.conflicts = FALSE)}
# 加载R包 Load the packagesuppressWarnings(suppressMessages(library(ggplot2)))suppressWarnings(suppressMessages(library(RColorBrewer)))suppressWarnings(suppressMessages(library(ggforce)))suppressWarnings(suppressMessages(library(dplyr)))suppressWarnings(suppressMessages(library(waffle)))

实战

# 1. Block Waffle Chart 块状华夫饼图# Data Preparationnrows <- 10category_counts <- round(table(mpg$class) * (nrows * nrows / length(mpg$class)))sorted_counts <- sort(category_counts)categories <- names(sorted_counts)df <- expand.grid(row = 1:nrows, column = 1:nrows)df$category <- factor(rep(categories, sorted_counts), levels = categories)# Color Palettecolors <- brewer.pal(length(sorted_counts), "Paired")# Plotblock_waffle_plot <- ggplot(df, aes(x = column, y = row, fill = category)) +  geom_tile(color = "grey80", size = 0.1) +  scale_fill_manual(values = colors, name = "Category") +  coord_fixed(ratio = 1) +  scale_x_reverse(expand = c(0, 0)) +  scale_y_reverse(expand = c(0, 0)) +  labs(title = "Block Waffle Chart of Categories") +  theme_minimal(base_size = 14) +  theme(    panel.grid = element_blank(),    axis.text = element_blank(),    axis.title = element_blank(),    axis.ticks = element_blank(),    legend.position = "right",    legend.title = element_text(size = 12),    legend.text = element_text(size = 10)  )# Save Plot# ggsave("block_waffle_chart.png", plot = block_waffle_plot, width = 8, height = 8, dpi = 300)ggsave("results/block_waffle_chart.pdf", plot = block_waffle_plot, width = 8, height = 8)#2.Dot Waffle Chart 点状华夫饼图# Data Preparationdf$category <- factor(rep(categories, sorted_counts))# Plotdot_waffle_plot <- ggplot(df, aes(x0 = column, y0 = row, fill = category, r = 0.4)) +  geom_circle(color = "grey40", size = 0.2) +  scale_fill_manual(values = colors, name = "Category") +  coord_fixed(ratio = 1) +  scale_x_reverse(expand = c(0, 0)) +  scale_y_reverse(expand = c(0, 0)) +  labs(title = "Dot Matrix Distribution by Category") +  theme_minimal(base_size = 14) +  theme(    panel.grid = element_blank(),    legend.position = "right",    legend.title = element_text(size = 12),    legend.text = element_text(size = 10)  )# Save Plot#ggsave("dot_waffle_chart.png", plot = dot_waffle_plot, width = 8, height = 8, dpi = 300)ggsave("results/dot_waffle_chart.pdf", plot = dot_waffle_plot, width = 8, height = 8)#3. Stacked Waffle Chart 堆积华夫饼图# Data Preparationunit_size <- 100category_data <- as.data.frame(table(mpg$class) * (nrows * nrows))colnames(category_data) <- c("category", "count")category_data <- arrange(category_data, desc(count))category_data$count <- category_data$count / unit_size# Expanded Data for Plottingexpanded_data <- expand.grid(row = 1:10, column = seq_len(ceiling(sum(category_data$count) / 10)))category_vector <- rep(category_data$category, category_data$count)expanded_data <- expanded_data[1:length(category_vector), ]expanded_data$category <- factor(category_vector, levels = category_data$category)# Color Palettecolors <- brewer.pal(nrow(category_data), "Set1")# Plotstacked_waffle_plot <- ggplot(expanded_data, aes(x = column, y = row, fill = category)) +  geom_point(color = "black", shape = 21, size = 4) +  scale_fill_manual(values = colors, name = "Category") +  coord_fixed(ratio = 1) +  labs(title = "Stacked Waffle Chart of Categories", x = "Each Square = 100") +  theme_minimal(base_size = 14) +  theme(    panel.grid = element_blank(),    axis.text.x = element_text(size = 10),    axis.title.x = element_text(size = 12),    legend.position = "right"  )# Save Plot#ggsave("stacked_waffle_chart.png", plot = stacked_waffle_plot, width = 8, height = 8, dpi = 300)ggsave("results/stacked_waffle_chart.pdf", plot = stacked_waffle_plot, width = 8, height = 8)#4. Faceted Waffle Chart 分面华夫饼图# Data Preparationstorms_summary <- storms %>%  filter(year >= 2010) %>%  count(year, status)# Plotfaceted_waffle_plot <- ggplot(storms_summary, aes(fill = status, values = n)) +  geom_waffle(color = "white", size = 0.3, n_rows = 10, flip = TRUE) +  facet_wrap(~ year, nrow = 1, strip.position = "bottom") +  scale_fill_brewer(palette = "Paired") +  scale_x_discrete() +  scale_y_continuous(labels = scales::label_number(scale = 1), expand = c(0, 0)) +  labs(    title = "Faceted Waffle Chart of Storms",    subtitle = "Distribution of Storm Statuses (2010 onwards)",    x = "Year",    y = "Count"  ) +  theme_minimal(base_size = 14) +  theme(    panel.grid = element_blank(),    axis.text.x = element_text(size = 10),    axis.title.x = element_text(size = 12),    legend.position = "right",    legend.title = element_text(size = 12),    legend.text = element_text(size = 10)  )# Save Plot#ggsave("faceted_waffle_chart.png", plot = faceted_waffle_plot, width = 12, height = 6, dpi = 300)ggsave("results/faceted_waffle_chart.pdf", plot = faceted_waffle_plot, width = 12, height = 6)

排版Combo plots

组合多个子图为发表格式

library(cowplot)width = 89height = 59p0 = plot_grid(block_waffle_plot, dot_waffle_plot, stacked_waffle_plot, faceted_waffle_plot, labels = c("A", "B", "C", "D"), ncol = 2)ggsave("results/Waffle_plot.pdf", p0, width = width * 5, height = height * 4, units = "mm")

使用此脚本,请引用下文:

Yong-Xin Liu, Lei Chen, Tengfei Ma, Xiaofang Li, Maosheng Zheng, Xin Zhou, Liang Chen, Xubo Qian, Jiao Xi, Hongye Lu, Huiluo Cao, Xiaoya Ma, Bian Bian, Pengfan Zhang, Jiqiu Wu, Ren-You Gan, Baolei Jia, Linyang Sun, Zhicheng Ju, Yunyun Gao, Tao Wen, Tong Chen. 2023. EasyAmplicon: An easy-to-use, open-source, reproducible, and community-based pipeline for amplicon data analysis in microbiome research. iMeta 2: e83. https://doi.org/10.1002/imt2.83

Copyright 2016-2024 Defeng Bai baidefeng@caas.cn, Chuang Ma 22720765@stu.ahau.edu.cn, Jiani Xun 15231572937@163.com, Yong-Xin Liu liuyongxin@caas.cn

宏基因组推荐
本公众号现全面开放投稿,希望文章作者讲出自己的科研故事,分享论文的精华与亮点。投稿请联系小编(微信号:yongxinliu 或 meta-genomics)

猜你喜欢

iMeta高引文章 fastp 复杂热图 ggtree 绘图imageGP 网络iNAP
iMeta网页工具 代谢组MetOrigin 美吉云乳酸化预测DeepKla
iMeta综述 肠菌菌群 植物菌群 口腔菌群 蛋白质结构预测

10000+:菌群分析 宝宝与猫狗 梅毒狂想曲 提DNA发Nature

系列教程:微生物组入门 Biostar 微生物组  宏基因组

专业技能:学术图表 高分文章 生信宝典 不可或缺的人

一文读懂:宏基因组 寄生虫益处 进化树 必备技能:提问 搜索  Endnote

扩增子分析:图表解读 分析流程 统计绘图

16S功能预测   PICRUSt  FAPROTAX  Bugbase Tax4Fun

生物科普:  肠道细菌 人体上的生命 生命大跃进  细胞暗战 人体奥秘  

写在后面

为鼓励读者交流快速解决科研困难,我们建立了“宏基因组”讨论群,己有国内外6000+ 科研人员加入。请添加主编微信meta-genomics带你入群,务必备注“姓名-单位-研究方向-职称/年级”。高级职称请注明身份,另有海内外微生物PI群供大佬合作交流。技术问题寻求帮助,首先阅读《如何优雅的提问》学习解决问题思路,仍未解决群内讨论,问题不私聊,帮助同行。

点击阅读原文


宏基因组
宏基因组/微生物组是当今世界科研最热门的研究领域之一,为加强本领域的技术交流与传播,推动中国微生物组计划发展,中科院青年科研人员创立“宏基因组”公众号,目标为打造本领域纯干货技术及思想交流平台。
 最新文章