MicrobiomeStatPlot | 柱状图教程Basic bar plot tutorial

发布时间:2024-10-07 17:39  浏览量:2

柱状图简介

柱状图(Bar Chart)是一种常用于显示分类变量数据的图形表示方式,主要通过矩形柱的长度或高度来反映各分类的数值大小。柱状图能够有效展示不同类别之间的对比情况,并直观地体现各类目之间的差异。其横轴通常用于表示分类变量,而纵轴则代表某种度量(如频率、百分比或其他数值)。每一个分类的柱子长度与其对应的数值成正比,从而使得不同类别之间的数据量可视化。

标签:#

微生物组数据分析 #MicrobiomeStatPlot #基本柱状图 #R语言可视化 #Basic bar Plot

作者:

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

源代码及测试数据链接:

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

柱状图案例

这是Xuehui Huang课题组2021年发表于Nature Genetics上的文章,第一作者为Xin Wei,题目为:A quantitative genomics map of rice provides genetic insights and guides breedinghttps://doi.org/10.1038/s41588-020-00769-9

图 4 | QTNs 的遗传调查。

e,东亚、南亚和东南亚籼稻 QTNs 的等位基因频率;f,东亚、东南亚和东北亚粳稻 QTNs 的等位基因频率。所示的所有 QTG 都具有高度差异的等位基因频率(AF > 0.4)。各 QTG 的详细功能等位基因频率见补充数据集 4 和 5。

结果

在籼稻方面,东亚地区具有较多的早穗期等位基因、较多的抗稻瘟病等位基因和较强的抗低温发芽能力,但较少的高矿质营养利用效率等位基因(图4e),这与该地区昼长、病害胁迫严重、低温和大量施肥的特点相一致。同时,东北亚地区的粳稻耐寒性和早穗期等位基因频率较高(图4f),这与东北亚地区种植季节气温低、昼长的特点相一致。

柱状图R语言实战

源代码及测试数据链接:

软件包安装

# 基于CRAN安装R包,检测没有则安装p_list = c("ggplot2", "dplyr", "readxl", "cols4all", "patchwork", "scales", "patchwork", "reshape2", "ggsignif","tidyverse","ggh4x","grid","showtext","Cairo","ggpattern","ggpubr","ggbreak","dagitty","brms","broom","broom.mixed","ggdag","MetBrewer","latex2exp","readxl","ggsci")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(dplyr)))suppressWarnings(suppressMessages(library(readxl)))suppressWarnings(suppressMessages(library(cols4all)))suppressWarnings(suppressMessages(library(patchwork)))suppressWarnings(suppressMessages(library(scales)))suppressWarnings(suppressMessages(library(reshape2)))suppressWarnings(suppressMessages(library(ggsignif)))suppressWarnings(suppressMessages(library(tidyverse)))suppressWarnings(suppressMessages(library(ggh4x)))suppressWarnings(suppressMessages(library(MetBrewer)))suppressWarnings(suppressMessages(library(latex2exp)))suppressWarnings(suppressMessages(library(ggsci)))suppressWarnings(suppressMessages(library(ggdag)))suppressWarnings(suppressMessages(library(broom.mixed)))suppressWarnings(suppressMessages(library(broom)))suppressWarnings(suppressMessages(library(grid)))suppressWarnings(suppressMessages(library(showtext)))suppressWarnings(suppressMessages(library(Cairo)))suppressWarnings(suppressMessages(library(ggpattern)))suppressWarnings(suppressMessages(library(ggpubr)))suppressWarnings(suppressMessages(library(ggbreak)))suppressWarnings(suppressMessages(library(dagitty)))suppressWarnings(suppressMessages(library(brms)))

实战1

参考:

# 数据# Creat datadf group = c("A", "B", "C","D","F"), value1 = c(10, 20, 30, 20, 15), value2 = c(15, 25, 35, 15, 20))df1 # 添加误差数据# Add error datadf1$sd # 误差线位置# Error bar positiondf1 % group_by(group) %>% mutate(xx=cumsum(value))# 保证误差线可以对应其正确位置# Ensure that the error bars correspond to their correct positionsdf1$variable # 转换为因子,指定绘图顺序# Convert to factors and specify drawing orderdf1$group # 美化与主题调整# Beautification and theme adjustmentmycol1 mycol2 # 分组柱形图,添加误差棒# Grouping bar graphs, adding error barsp1 geom_bar(position = position_dodge, stat = "identity", color = NA, width = 0.8) + scale_fill_manual(values = mycol1) + scale_y_continuous(expand = c(0, 0)) + theme_classic + geom_errorbar(aes(ymin = value - sd, ymax = value + sd), position = position_dodge(width = 0.8), width = 0.4) + labs(y = 'Value') + theme(axis.text = element_text(size = 12), axis.title = element_text(size = 14))# 堆叠柱形图,添加误差棒# Stacked bar chart with error bars addeddf1 % group_by(group) %>% mutate(position = cumsum(value))p2 geom_bar(position = "stack", stat = "identity", color = NA, width = 0.8) + scale_fill_manual(values = mycol2) + scale_y_continuous(expand = c(0, 0)) + theme_classic + geom_errorbar(aes(ymin = position - sd, ymax = position + sd), width = 0.4) + labs(y = 'Value') + theme(axis.text = element_text(size = 12), axis.title = element_text(size = 14))# 保存图形为PDF文件# Save graphics as PDF filesggsave("results/p1.pdf", plot = p1, width = 6, height = 4)ggsave("results/p2.pdf", plot = p2, width = 6, height = 4)# 组合图library(cowplot)width = 89height = 59p0 = plot_grid(p1, p2, labels = c("A", "B"), ncol = 2)ggsave("results/Simple_bar_plot1.pdf", p0, width = width * 3, height = height * 2, units = "mm")

实战2

利用ggpattern软件包绘制柱状图

# 测试使用内置数据集ToothGrowth# Test using the dataset ToothGrowthhead(ToothGrowth)table(ToothGrowth$supp)#> #> OJ VC #> 30 30table(ToothGrowth$dose)#> #> 0.5 1 2 #> 20 20 20# gradient/plasma 渐变色# gradientp21 geom_bar_pattern(aes(pattern_fill = factor(dose)), stat = "summary", fun = mean, position = "dodge", linewidth = 0.7, pattern = 'gradient', pattern_key_scale_factor = 1.5, pattern_fill2 = NA, fill = NA) + stat_summary(fun.data = 'mean_sd', geom = "errorbar", width = 0.15, linewidth = 0.6) + #theme_minimal(base_size = 16) + theme_classic+ scale_y_continuous(expand = c(0,0), limits = c(0, 35), breaks = seq(0, 35, 5)) + scale_pattern_fill_manual(values = c("#89A66D", "#538DB7", "#D8898A")) + labs(x = , title = "Gradient Pattern", subtitle = 'Pattern Fill = Dose') + theme(legend.position = 'none') + geom_signif(comparisons = list(c("0.5", "1"), c("0.5", "2")), y_position = c(24, 30), map_signif_level = TRUE, test = wilcox.test, textsize = 6)#p21# plasmap22 geom_bar_pattern(aes(pattern_fill = factor(dose)), stat = "summary", fun = mean, position = "dodge", linewidth = 0.7, pattern = 'plasma', pattern_key_scale_factor = 1.5, pattern_fill2 = NA, fill = NA) + stat_summary(fun.data = 'mean_sd', geom = "errorbar", width = 0.15, linewidth = 0.6) + #theme_minimal(base_size = 16) + theme_classic+ scale_y_continuous(expand = c(0, 0), limits = c(0, 35), breaks = seq(0, 35, 5)) + scale_pattern_fill_manual(values = c("#89A66D", "#538DB7", "#D8898A")) + labs(x = , title = "Plasma Pattern", subtitle = 'Pattern Fill = Dose') + theme(legend.position = 'none') + geom_signif(comparisons = list(c("0.5", "1"), c("0.5", "2")), y_position = c(24, 30), map_signif_level = TRUE, test = wilcox.test, textsize = 6)# p22# 保存为PDF# Save as PDFggsave("results/gradient_plasma.pdf", p21 + p22, width = 8, height = 5)# 柱子上添加图片# placeholder p23 geom_bar_pattern(aes(pattern_type = factor(dose)), pattern = 'placeholder', pattern_type = 'bear', stat = "summary", fun = mean, position = "dodge", pattern_spacing = 0.08, pattern_density = 0.9, linewidth = 0.6, fill = 'white', colour = 'black') + stat_summary(fun.data = 'mean_sd', geom = "errorbar", width = 0.15, linewidth = 0.6) + #theme_minimal(base_size = 16) + theme_classic+ scale_y_continuous(expand = c(0, 0), limits = c(0, 35), breaks = seq(0, 35, 5)) + labs(x = , title = "Placeholder Pattern", subtitle = 'Pattern Type = Dose') + theme(legend.position = 'none') + geom_signif(comparisons = list(c("0.5", "1"), c("0.5", "2")), y_position = c(24, 30), map_signif_level = TRUE, test = wilcox.test, textsize = 6)# 保存为PDF# Save as PDFggsave("results/placeholder_pattern.pdf", p23, width = 6, height = 5)# 条纹样式# magickp24 geom_bar_pattern(aes(pattern_type = factor(dose)), pattern = 'magick', pattern_fill = "black", stat = "summary", fun = mean, position = "dodge", pattern_spacing = 0.08, pattern_density = 0.9, linewidth = 0.6, fill = 'white', colour = 'black') + stat_summary(fun.data = 'mean_sd', geom = "errorbar", width = 0.15, linewidth = 0.6) + #theme_minimal(base_size = 16) + theme_classic+ scale_y_continuous(expand = c(0, 0), limits = c(0, 35), breaks = seq(0, 35, 5)) + scale_pattern_type_discrete(choices = c('vertical2','hs_bdiagonal','gray100','gray0')) + labs(x = , title = "Magick Pattern", subtitle = 'Pattern Type = Dose') + theme(legend.position = 'none') + geom_signif(comparisons = list(c("0.5", "1"), c("0.5", "2")), y_position = c(24, 30), map_signif_level = TRUE, test = wilcox.test, textsize = 6)# 保存为PDF# Save as PDFggsave("results/magick_pattern.pdf", p24, width = 6, height = 5)# 组合图library(cowplot)width = 89height = 59p0 = plot_grid(p21, p22, p23, p24, labels = c("A", "B", "C", "D"), ncol = 2)ggsave("results/ggpattern_bar_plot1.pdf", p0, width = width * 3, height = height * 3, units = "mm")

实战3

渐变色分组柱状图

# 渐变色分组柱状图# Gradient color grouped bar chart#datadf ~pattern, ~treat,~value,~sd, ~color, 'S', 'blank', 0.253, 0.02,"brown", 'S', '20', 0.243, 0.005,"green", 'S', '40', 0.193, 0.02, "yellow", 'P', 'blank', 0.17, 0.001,"brown", 'P', '20', 0.08, 0.01,"green", 'P', '40', 0.06, 0.001, "yellow")# 绘图# plotpdf(file='results/Bar_plot_change_color01.pdf', height=10,width=12, family='sans')ggplot(df) + geom_col_pattern(aes(x = factor(pattern, levels = c('S','P')), y = value, group = factor(treat, levels = c('blank', '20', '40')), pattern_fill = color), pattern = 'gradient', color = NA, fill = NA, position = position_dodge2(padding = 0.3), width = 0.6) + geom_errorbar(aes(x = factor(pattern, levels = c('S','P')), ymin = value - sd, ymax = value + sd, color = factor(treat, levels = c('blank', '20', '40'))), position = position_dodge(width = 0.6), linewidth = 1.1, width = 0.1) + annotate("text", x = 'S', y = 0.29, label = "Anti-interference", size = 7, color = '#395529') + annotate("text", x = 'P', y = 0.21, label = "Interference", size = 7, color = '#691000') + geom_curve(aes(x = 0.7, y = 0.26, xend = 1.3, yend = 0.205), curvature = -0.3, linewidth = 1.2, color = '#324F28', arrow = arrow(length = unit(0.03, "npc"), ends = "last", type = "open")) + annotate(geom = 'line', x = c(1.8, 2.1, 2.2, 2.3), y = c(0.2, 0.1, 0.11, 0.09), linewidth = 1.2, color = "#AD480D", scale_x_discrete(labels = c(expression(MnFe[

外部推荐