MicrobiomeStatPlot |环形柱状图教程Ring bar plot tutorial

发布时间:2024-12-01 07:36  浏览量:5

环形柱状图简介

环形柱状图(circular barplot)是将柱状图的柱子排列成环形的一种图表形式。它可以在视觉上给人以更具吸引力和不一样的展示效果,尤其适合展示带有周期性或循环性质的数据。例如,可以用来表示一天24小时内的活动模式、一年12个月的变化趋势等.与传统的柱状图不同,环形柱状图的柱子不是沿着水平或垂直轴排列的,而是沿着一个圆的周围排列。环形柱状图能在有限空间内展示更多的类别。

标签:#

微生物组数据分析 #MicrobiomeStatPlot #环形柱状图 #R语言可视化 #Ring bar chart

作者:

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

源代码及测试数据链接:

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

环形柱状图案例

这是马克斯-德尔布吕赫分子医学中心Gaetano Gargiulo团队2024年发表于NC上的一篇论文用到的图。论文题目为:Logical design of synthetic cis-regulatory DNA for genetic tracing of cell identities and state changes.https://doi.org/10.1038/s41467-024-45069-6

图 6 | 将 LSD、全基因组 CRISPR 激活和患者数据集汇聚在一起,以发现细胞状态特异性驱动因素

结果

利用稳健的等级聚合和对报告基因低水平部分的富集对数据集进行无偏分析,发现了可能被多个sgRNA扩增的基因,这些基因可能驱动报告基因特异性上调(图 6b )。引人注目的是,在MGT4筛选的热门基因中发现了RELA(图6b):RELA是NF-kB转录因子之一,之前在聚焦功能缺失实验和基因组规模的CRISPR KO筛选中都发现它是胶质母细胞瘤启动细胞平衡条件下的MGT1调控因子。

环形柱状图R语言实战

源代码及测试数据链接:

软件包安装

# 基于CRAN安装R包,检测没有则安装 Installing R packages based on CRAN and installing them if they are not detectedp_list = c("ggplot2", "reshape2", "dplyr", "cowplot", "likert", "tidyverse", "export", "RColorBrewer", "readxl")for(p in p_list){if (!requireNamespace(p)){install.packages(p)} library(p, character.only = TRUE, quietly = TRUE, warn.conflicts = FALSE)}# 加载R包 Loading R packagessuppressWarnings(suppressMessages(library(ggplot2)))suppressWarnings(suppressMessages(library(reshape2)))suppressWarnings(suppressMessages(library(cowplot)))suppressWarnings(suppressMessages(library(likert)))suppressWarnings(suppressMessages(library(tidyverse)))suppressWarnings(suppressMessages(library(export)))suppressWarnings(suppressMessages(library(RColorBrewer)))suppressWarnings(suppressMessages(library(readxl)))

实战1

参考:

https://mp.weixin.qq.com/s/jbUe7PvSP9BmldWFyvP9IQ https://mp.weixin.qq.com/s/X4cmEAjCs_ukmolii6tuTQ

# 加载数据 Load datadata # 数据整理 Data processingdata$pheno data$group data$group data$id # 添加空行,避免图表过度拥挤# Add blank lines to avoid overcrowding the chartempty_bar to_add colnames(to_add) to_add$group data # 更新id以适应新数据集# Updating ids for new datasetsdata % arrange(group)data$id # 标签数据# Label datalabel_data number_of_bar angle label_data$hjust label_data$angle # 计算组的起始和终止位置以便绘制网格# Calculate the start and end positions of groups to draw the gridbase_data % group_by(group) %>% dplyr::summarize(start = min(id), end = max(id) - empty_bar) %>% rowwise %>% mutate(title = mean(c(start, end)))# 绘制柱形图# Drawing bar plotp geom_bar(stat = "identity", width = 0.9, alpha = 0.85) + geom_errorbar(aes(ymin = est - se, ymax = est + se), width = 0.2, size = 0.6, alpha = 0.9) + coord_polar + scale_fill_brewer(palette = "Set3") + theme_minimal + theme( legend.position = "right", axis.text = element_blank, axis.title = element_blank, panel.grid = element_blank ) + ylim(-1, max(data$est + data$se) + 1) # 添加网格线# Add grid linegrid_data % mutate(start = start - 0.5, end = end + 0.5)for (i in seq(0, 1, by = 0.2)) { p }# 添加标签# Add labelsp # 保存最终图形# Save plotsggsave(filename = "results/Circle_bar_plot.pdf", plot = p, width = 14, height = 10)

实战2

https://www.nature.com/articles/s41561-023-01293-1#Sec22

# Load data# 载入数据data2 # Log10转化# Log10-transformationdata3 data3[,3:5][data3[,3:5] head(data3)# 在不同的分组间插入用于分隔的间隙# Insert gaps between different groups to separate thememptybarempty ncol(data3)))# 四个间隙+一个大约为40度大间隔,用于后续放置刻度线# Four gaps + a large gap of about 40 degrees for subsequent placement of tick markscolnames(empty)empty$Group rep("Group5", floor((nrow(data3)+4)/9)))# 将大间隔设置为Group5,后续会将其消除# Set the large interval to Group5, which will be eliminated laterhead(empty)# 整合数据, 将数据空行插入原始数据,按Group排序# Integrate data, insert blank rows into original data, and sort by Groupdata3 % arrange(Group)data3$idhead(data3)data3 # 绘制基础的柱状图# Plotp1 geom_bar(aes(y = PNER), stat="identity", alpha = 1, fill = "#BCAECC")+ geom_bar(aes(y = CNER), stat="identity", alpha = 1, fill = "#5682B5")+ geom_bar(aes(y = EF), stat="identity", alpha = 1, fill = "#E7D3A5")+#三组数据 scale_fill_manual(values = c("#5CB85C", "#337AB7", "#F0AD4E", "#D9534F"))#p1# 添加分组的绿色虚线# Add grouped green dashed lines# 列出每个分组中的第一个和最后一个数据,用于画竖线# List the first and last data in each group for drawing vertical linesgroup_data1 "sample11", "sample18","sample19", "sample82","sample83", "sample139"))# 选出三列数据中最大的一个,避免虚线画进柱子内部# Select the largest of the three columns to avoid drawing dotted lines inside the columngroup_data1$high# 得到5组画横线的数据# Get 5 sets of data for drawing horizontal linesgroup_data2 ncol = 2, nrow = 5), Group = paste0("Group", 1:5)) |> setNames(c("x", "xend", "Group"))head(group_data2)# 绘图# Plotp2 geom_segment(data = group_data1, aes(x = id, y = high, xend = id, yend = 5.5), color = "#1C7875", alpha = 0.8, size = 0.7, lty = 2)+ geom_segment(data = group_data2, aes(x = x, y = 5.5, xend = xend, yend = 5.5), #设置圆环外围和中间空心圆大小 #Set the size of the outer ring and the hollow circle in the middle ylim(-3.5, 6)+ theme_minimal+ theme(legend.position = "none", axis.text = element_blank, axis.title = element_blank, panel.grid = element_blank)+ coord_polar #p2# 添加刻度线# Add tick marksaxis_data y = c(0, 1, 2, 3, 4, 5), yend = c(0, 1, 2, 3, 4, 5), label = c(0.1, 1, 10, 100, 1000, 10000))head(axis_data)# 基于每一组第一个和最后一个位置,确定绿色框线的分组标签位置# Based on the first and last positions of each group, determine the group label position of the green frame linebase_data % group_by(Group) %>% dplyr::summarize(start = min(id), end = max(id) - emptybar) %>% rowwise %>% mutate(title = mean(c(start, end))) %>% filter(Group != "Group5")# 绘图# Plotp3 annotate(geom = "segment", x = 0.5, xend = 0.5, y = 0, yend = 5) + geom_segment(data = axis_data, aes(x = x, xend = xend, y = y, yend = yend), inherit.aes = FALSE)+#添加刻度轴 geom_text(data = axis_data, aes(x = 5.5, y = y, label = label), inherit.aes = T, hjust = 0.9, angle = -10, size = 3)+#添加刻度轴上的数字 annotate(geom = "text", x = 12, y = 2.5, label = expression("Concerntration\n","ng"~g^{-1}~", Dw"), angle = 87, vjust = -0.1)+#y轴标签名 geom_text(x = -80, y = -2.2, label = "Tibetan Plateau", size = 4, angle = 77, color = "black")+ geom_text(x = -80, y = -4.2, label = "270 HOC", size = 4, angle = 77, color = "grey50")+#内圆中的标签 geom_text(data = base_data, aes(x = title, y = 6, label = Group), colour = "#1C7875", alpha = 0.8, size = 3.5, inherit.aes = FALSE)#p3# 保存最终图形# Save plotsggsave(filename = "results/Circle_bar_plot2.pdf", plot = p3, width = 14, height = 10)

实战3

内向环形柱状图

# 导入数据# Load datadata head(data)data$value # 每组间添加间隔# Add gapsempty_bar # 将group列转换为因子变量# group as factordata$group # 创建一个空的数据框to_add# A new dataframeto_add # 创建一个空的数据框to_add# A new dataframecolnames(to_add) # 为to_add数据框的group列赋值,使得每个组别之间2个空行# Assign values to the group column of the to_add data frame so that there are 2 blank rows between each groupto_add$group # 将to_add数据框添加到原数据集data中,实现在每组之间插入空行的效果# Add the to_add data frame to the original data set data to insert blank rows between each groupdata # 对数据集按照group列进行排序# Sort the data set by the group columndata % arrange(group)# 为数据集添加一个新的列id,用来重新编号数据集的行# Add a new column id to the dataset to renumber the rows of the datasetdata$id head(data)# 添加各个变量标签在图中的角度信息和对齐方式# Add angle information and alignment of each variable label in the graph# 对数据集data按照group列和value列进行排序,确保每个组内的数据按照value的大小顺序排列# Sort the data set data by the group column and the value column to ensure that the data in each group is arranged in order of valuedata = data %>% arrange(group, value)# 设置添加label标签信息# Add label infolabel_data # 计算数据集中的总行数,用于后续计算标签在图中的角度信息# Calculate the total number of rows in the data set for subsequent calculation of the angle information of the label in the figurenumber_of_bar # 根据数据集中每行的编号计算标签在图中的角度信息,使得标签可以环绕图片# Calculate the angle information of the label in the image according to the number of each row in the dataset, so that the label can surround the imageangle # 根据角度信息判断标签的对齐方式,如果角度大于-90度,则左对齐;否则右对齐# Determine the alignment of the label based on the angle information. If the angle is greater than -90 degrees, it is left-aligned; otherwise, it is right-aligned.label_data$hjust # 根据角度信息调整标签的角度,使得标签在不同位置可以正确显示# Adjust the angle of the label according to the angle information so that the label can be displayed correctly in different positionslabel_data$angle # 添加各组数据在图片上的起止位置,用于刻度线等的定位# Add the start and end positions of each group of data on the image for positioning of scale lines, etc.# 按照group列对数据集进行分组,然后对每个组计算起始位置和结束位置的定位信息,并计算每个组的标题位置# Group the data set according to the group column, then calculate the starting and ending position information for each group, and calculate the title position of each groupbase_data % group_by(group) %>% dplyr::summarize(start=min(id), end=max(id) - empty_bar) %>% rowwise %>% mutate(title=mean(c(start, end)))# 添加各组数据在图片上的间隔定位信息,用于刻度线等的定位# Add the interval positioning information of each group of data on the picture, which is used for positioning of scale lines, etc.# 将之前计算得到的base_data数据框赋值给grid_data,用于添加间隔的起始和结束位置信息# Assign the previously calculated base_data data frame to grid_data to add the start and end position information of the intervalgrid_data # 将grid_data数据框中end列的值向下平移一行,并加上0.7,用于确定间隔的结束位置# Shift the value of the end column in the grid_data data frame down one row and add 0.7 to determine the end position of the intervalgrid_data$end # 将grid_data数据框中start列的值向上平移0.7,用于确定间隔的起始位置# Shift the value of the start column in the grid_data data frame upward by 0.7 to determine the starting position of the intervalgrid_data$start # 删除grid_data数据框中的第一行,因为第一行的起始位置和结束位置已经确定,不需要再添加间隔信息# Delete the first row in the grid_data data frame, because the starting and ending positions of the first row have been determined, and there is no need to add interval informationgrid_data # 绘图(Plot)# 绘制分组环状条形图# Draw a grouped donut bar chartp4 y=-value, fill=group)) + geom_bar(stat="identity", alpha=0.3) + scale_fill_brewer(type = "qual", palette = "Dark2")+ #限制Y轴的上下限,用于调节图片的显示效果 #Limit the upper and lower limits of the Y axis to adjust the display effect of the image ylim(-1,0.45) + geom_segment(data=grid_data, aes(x = end, y = -0.15, xend = start, yend = -0.15), colour = "black", alpha=1, size=0.8 , inherit.aes = FALSE ) + geom_segment(data=grid_data, aes(x = end, y = -0.1, xend = start, yend = -0.1), colour = "black", alpha=1, size=0.8 , inherit.aes = FALSE ) + geom_segment(data=grid_data, aes(x = 1, y = -0.145, xend = 94.7, yend = -0.045), linetype="dashed", colour = "red", alpha=0.1, size=0.5 , inherit.aes = FALSE ) + geom_segment(data=grid_data, aes(x = end, y = 0, xend = start, yend = 0), colour = "black", alpha=1, size=0.8 , inherit.aes = FALSE )+ geom_segment(data=grid_data, aes(x = end, y = -0.045, xend = start, yend = -0.045), annotate("text", x = rep(23.8,4), y = c(-0.145, -0.095, -0.042, 0.005), label = c("0.15", "0.1", "0.045", "0") , family="sans", color="grey", size=2.8 , angle=0, fontface="bold", hjust=0) + theme_minimal + theme( legend.position=c(.5,.5), axis.title = element_blank, panel.grid = element_blank, axis.text.x = element_blank, plot.margin = unit(rep(0,8), "cm")) + labs(family="sans",fill = "Reproductive Outcomes")+ coord_polar + geom_segment(data=base_data, aes(x = start-0.7, y = 0.007, xend = end+0.7, yend = 0.007), colour = "black", alpha=0.5, size=2 , inherit.aes = FALSE ) + geom_segment(data=base_data , aes(x = start-0.7, y = 0.18, xend = end+0.7, yend = 0.18), colour = brewer.pal(4,"Dark2"), alpha=0.3, size=31 , inherit.aes = FALSE )+ geom_text(data=label_data, aes(x=id, y=0.07, label=sample, hjust=hjust), color="black", fontface="bold",alpha=ifelse(label_data$value > 0.245, 0.9, 0.3), size=3.5, angle= label_data$angle, inherit.aes = FALSE )#p4# 保存最终图形# Save plotsggsave(filename = "results/Circle_bar_plot3.pdf", plot = p4, width = 14, height = 10)

实战4

环状堆叠柱状图

printf("hello world!");# 载入数据# Load datadat01 sheet = "Fig 1b",na="NA")# 给数据集增加一列用来做x# Add a column for x-axisdat01 %>% ggpubr::mutate(new_x = rep(paste0('X',formatC(1:31,width = 2,flag = 0)),each=8)) -> dat01# 最基本的堆积柱形图# Basic stack bar plotp51 geom_bar(stat = "identity",position = "fill")# 变成环状# Circilizep52 geom_bar(stat = "identity",position = "fill")+ coord_polar+ ylim(-1,NA)# 修改细节# Change detailsp53 geom_bar(stat = "identity",position = "fill")+ coord_polar+ scale_x_discrete(expand = expansion(add = 0), labels=coalesce(dat01$orderName[seq(1,248,8)],""))+ scale_y_continuous(limits = c(-1,NA))+ theme(axis.text.x = element_text(angle = cumsum(c(90,-rep(12,15))), vjust=0,hjust = 1), panel.background = element_blank, axis.text.y = element_blank, axis.title = element_blank, axis.ticks = element_blank)#p53# 修改颜色,添加标签# Change color and add labelsp54 annotate(geom = "text",x=2,y=-0.1,label="Birds",angle=-15)+ annotate(geom = "text",x=8,y=-0.1,label="Amphibians",angle=-90)+ annotate(geom = "text",x=14,y=-0.1,label="Mammals",angle=15)+ annotate(geom = "text",x=24,y=-0.1,label="Reptiles",angle=90)+ scale_fill_manual(values = c("LC"="#00c9a7","NT"="#c4fcef", "DD"="#b0a8b9","VU"="#fefedf", "EN"="#ff8066","CR"="#e77381", "EW"="#4b4453","EX"="#8f888b"), limits=c("EX","EW","CR","EN","VU","DD","NT","LC"), name="")# p54# 添加坐标轴# Add axis lablesnew_dat01 x=0.5,xend=0.3, y=c(0,0.25,0.5,0.75,1), yend=c(0,0.25,0.5,0.75,1))new_dat02 label=c(0,0.25,0.5,0.75,1)*100)p55 x=0.5,xend=0.5,y=0,yend=1)+ geom_segment(data=new_dat01, aes(x=x,xend=xend,y=y,yend=yend), inherit.aes = FALSE)+ geom_text(data=new_dat02,aes(x=x,y=y,label=label), inherit.aes = FALSE,hjust=1)+ annotate(geom = "text",x=30,y=0.5,label="Species\nthreatened (%)", angle=90,vjust=-0.1)# 保存最终图形# Save plotsggsave(filename = "results/Circle_bar_plot4.pdf", plot = p55, width = 14, height = 10)

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

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

宏基因组推荐

10月18-20日,微生物组-扩增子16S分析

11月15-17日,微生物组-宏基因组分析

人满即开 | 论文作图和统计分析培训班

一站式论文提升服务,助您顺利发高分论文!

本公众号现全面开放投稿,希望文章作者讲出自己的科研故事,分享论文的精华与亮点。

投稿请联系小编(-genomics)

iMeta高引文章 fastp 复杂热图 ggtree 绘图imageGP 网络iNAP

iMeta网页工具 代谢组MetOrigin 美吉云乳酸化预测DeepKla

iMeta综述 肠菌菌群 植物菌群 口腔菌群 蛋白质结构预测

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

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

外部推荐