使用ggplot绘制柱状图,在柱子中显示数值和显著性
[![](data/attachment/forum/plugin_zhanmishu_markdown/202406/d889e722f28369d83c0143f863f631af_1719577171_2181.jpg)](http://mp.weixin.qq.com/s?__biz=MzAwODY5NDU0MA==&mid=2455861689&idx=2&sn=bf497c5f96fbccc071751f5e9eb087a5&chksm=8cff27dabb88aecc14142b2712e8bbd0f4ed2d6660b4814cd2031212edf682bd8c61f2fb391a&scene=21#wechat_redirect)**「一边学习,一边总结,一边分享!」**
**由于微信改版,一直有同学反映。存在长时间接收不到公众号的推文。那么请跟随以下步骤,将**「小杜的生信筆記」**设置为**「星标」,不错过每一条推文教程。
![](data/attachment/forum/plugin_zhanmishu_markdown/202406/aa30cc9119a5b7bcf287ee18e14e1b54_1719577171_5191.jpg)
### 本期教程
![](data/attachment/forum/plugin_zhanmishu_markdown/202406/1648d8f072d0992c00cbcadfc5656c89_1719577171_9755.jpg)
**获得本期教程示例数据,后台回复关键词:「20240628」。(PS:在社群中,可获得往期和未来教程所有数据和代码)**
### 往期教程部分内容
![](data/attachment/forum/plugin_zhanmishu_markdown/202406/6b00ac3c266071e09d271fdba0d3a925_1719577171_4850.jpg)![](data/attachment/forum/plugin_zhanmishu_markdown/202406/d1ff5fcbd71e612783c2c9471119f3be_1719577171_1053.jpg)![](data/attachment/forum/plugin_zhanmishu_markdown/202406/e115e4a67bb740ad368f3ab8f8624e92_1719577171_7336.jpg)![](data/attachment/forum/plugin_zhanmishu_markdown/202406/f34655060f89e94d4fb1d1e5a5f9a864_1719577171_9137.jpg)![](data/attachment/forum/plugin_zhanmishu_markdown/202406/06d043929eb608c6d4d40e7b27cbe52c_1719577171_4763.jpg)![](data/attachment/forum/plugin_zhanmishu_markdown/202406/e51b4e8848ce2fd8c6d4cfafa1b8399f_1719577171_9453.jpg)![](data/attachment/forum/plugin_zhanmishu_markdown/202406/7c6b888a392f210881774f51b2756a4c_1719577171_3683.jpg)![](data/attachment/forum/plugin_zhanmishu_markdown/202406/1c998b25bb3e22ff874ad6d0696b1806_1719577171_2732.jpg)![](data/attachment/forum/plugin_zhanmishu_markdown/202406/29333ee57790b333fc69f3105ee88845_1719577171_8772.jpg)![](data/attachment/forum/plugin_zhanmishu_markdown/202406/87126f51d679b67af3de7d25d46581e9_1719577171_3323.jpg)![](data/attachment/forum/plugin_zhanmishu_markdown/202406/72034e061ef4e419160fb33901df0933_1719577171_5772.jpg)![](data/attachment/forum/plugin_zhanmishu_markdown/202406/083c9ef2d2f5942394701247b9fcdc19_1719577171_4906.jpg)![](data/attachment/forum/plugin_zhanmishu_markdown/202406/d431d2d3a0161883b262b137e0146932_1719577171_3971.jpg)
## 写在前面
**基于ggplot绘制柱状图,小技巧,很基础的图形。但是,越到后面,会发现,越是基础的图形,我们使用的频率越高。今天的教程是基于发芽指数来绘制,我们模拟一个发芽数和天数,计算出发芽指数。**
### 代码
```plain
##'@在柱状图中显示数字
##'@2024.06.28
##'@
##'@小杜的生信笔记
library(ggplot2)
library(dplyr)
library(tidyr)
library(ggplot2)
library(agricolae)
setwd("D:\\BioinfoFile\\小杜的生信笔记\\2024\\20240628_柱状图中显示数据")
##'@此次数据基于种子发芽指数计算公式进行,数据具有随机性,不具有任何意义。
##'@加载数据
data <- read.csv("20240628_inputdata.csv",header = T)
data
```
![](data/attachment/forum/plugin_zhanmishu_markdown/202406/7cb1b1da6f2e225c4cc81ab8f9e95b02_1719577171_9442.jpg)
**「相关计算代码」**
![](data/attachment/forum/plugin_zhanmishu_markdown/202406/1c39c39fe8a1682ad219030162a17f0c_1719577171_5365.jpg)
```plain
## 转换数据
long_data <- data %>%
pivot_longer(cols = starts_with("germinated"), names_to = "replicate", values_to = "germinated")
long_data
##计算活力指数
germination_index <- long_data %>%
group_by(treatment, day) %>%
summarise(GI = sum(germinated / day)) %>%
ungroup()
#'@计算各个处理的平均数和标准差
summary_gi <- germination_index %>%
group_by(treatment) %>%
summarise(mean_GI = mean(GI), sd_GI = sd(GI))
#'@单因素方差分析
anova_result <- aov(GI ~ treatment + day, data = germination_index)
summary(anova_result)
#'@进行Tukey HSD检验
tukey_result <- HSD.test(anova_result, "treatment", group = TRUE)
print(tukey_result)
# 提取 Tukey HSD 结果中的字母标记
groups <- tukey_result$groups
df_letters <- data.frame(sample = rownames(groups), letters = groups$groups)
##'@修改名称
colnames(df_letters) <- c("treatment","letters")
# 合并均值和字母标记
df_data <- merge(summary_gi, df_letters, by = "treatment")
```
### 绘图
```plain
ggplot(df_data, aes(x = treatment, y = mean_GI, fill = treatment)) +
geom_bar(stat = "identity", position = "dodge", color = "black", size = 0.5) +
##'@误差线
geom_errorbar(aes(ymin = mean_GI - sd_GI, ymax = mean_GI + sd_GI), width = 0.15, ## 宽度
size = 1##字体大小
) +
##'@显示数字
geom_text(aes(x = treatment, y = mean_GI + sd_GI + 0.5,## “+0.5”显示的高度
label = round(mean_GI, 2)), ### "round(mean_GI, 2))",其中2,表示数字显示的小数点位数
size = 5,
color = 'black') +
##'@显示显著性
geom_text(aes(x = treatment, y = mean_GI + sd_GI + 1.3, label = letters), size = 6, color = "red") +
theme_bw()+
scale_fill_manual(values = c("#1f78b4", "#ff7f00", "#4daf4a")) +
labs(x = NULL, y = "Germination Index")+
theme(#axis.line = element_line(size = 1),## 粗细
text=element_text(#family = "sans",
colour ="black",size = 10),
axis.text.x = element_text(color = "black", size = 12),
axis.text.y = element_text(color = "black",size = 11),
axis.ticks = element_line(colour = "black"),
strip.text = element_text(color = "black",size = 10),
axis.title = element_text(color = "black",size = 12), ##坐标轴字体大小
legend.position = "none",
strip.background = element_blank()
)
ggsave("20240608_柱状图-显示数字.pdf",width = 6, height = 4)
```
![](data/attachment/forum/plugin_zhanmishu_markdown/202406/a4e8aea8fa4ef7fb5a01e0291c453bbf_1719577171_6578.jpg)
**获得本期教程示例数据,后台回复关键词:「20240628」。(PS:在社群中,可获得往期和未来教程所有数据和代码)**
**若我们的教程对你有所帮助,请**点赞+收藏+转发**,这是对我们最大的支持。**
### 往期部分文章
**「1. 最全WGCNA教程(替换数据即可出全部结果与图形)」**
* (https://mp.weixin.qq.com/s?__biz=MzAwODY5NDU0MA==&mid=2455850466&idx=1&sn=6036fc2a19594f7fc38d65271f21e9cc&scene=21#wechat_redirect)
* (https://mp.weixin.qq.com/s?__biz=MzAwODY5NDU0MA==&mid=2455850727&idx=1&sn=1156c2bb0b0c9baff02838f5ffce39bf&scene=21#wechat_redirect)
* (https://mp.weixin.qq.com/s?__biz=MzAwODY5NDU0MA==&mid=2455850742&idx=1&sn=3d3eeedb3b58e536a83dc38d15725cd4&scene=21#wechat_redirect)
* (https://mp.weixin.qq.com/s?__biz=MzAwODY5NDU0MA==&mid=2455854728&idx=5&sn=bc98befb8dd0f0090bdfe69f3ffdb008&scene=21#wechat_redirect)
* (https://mp.weixin.qq.com/s?__biz=MzAwODY5NDU0MA==&mid=2455856103&idx=1&sn=774f8a084d21f757266f35c501c7155d&scene=21#wechat_redirect)
---
**「2. 精美图形绘制教程」**
* [精美图形绘制教程](https://mp.weixin.qq.com/mp/appmsgalbum?__biz=MzAwODY5NDU0MA==&action=getalbum&album_id=2614156000866385923&scene=173&from_msgid=2455848496&from_itemidx=1&count=3&nolastread=1#wechat_redirect)
**「3. 转录组分析教程」**
* **「**[转录组上游分析教程[零基础]](https://mp.weixin.qq.com/mp/appmsgalbum?__biz=MzAwODY5NDU0MA==&action=getalbum&album_id=2870608342451224581&scene=126&uin=&key=&devicetype=Windows+10+x64&version=63090719&lang=zh_CN&ascene=0)**」**
* **「**[一个转录组上游分析流程 | Hisat2-Stringtie](https://mp.weixin.qq.com/s?__biz=MzAwODY5NDU0MA==&mid=2455857417&idx=1&sn=653f3bdb0af386c22a128732ec8a01a4&scene=21#wechat_redirect)**」**
**「4. 转录组下游分析」**
* [批量做差异分析及图形绘制 | 基于DESeq2差异分析](https://mp.weixin.qq.com/s?__biz=MzAwODY5NDU0MA==&mid=2455860684&idx=1&sn=7be489c453cca737ad1092c6e4499827&scene=21#wechat_redirect)
* (https://mp.weixin.qq.com/s?__biz=MzAwODY5NDU0MA==&mid=2455859512&idx=1&sn=bac01a018f8b58afc7e2b3484f476bf4&scene=21#wechat_redirect)
* [单基因GSEA富集分析](https://mp.weixin.qq.com/s?__biz=MzAwODY5NDU0MA==&mid=2455859147&idx=1&sn=b022d80868e4b8014f64d443c82d1668&scene=21#wechat_redirect)
* [全基因集GSEA富集分析](https://mp.weixin.qq.com/s?__biz=MzAwODY5NDU0MA==&mid=2455860201&idx=1&sn=dd65c5b967123a876a6f5a38d4723ca6&scene=21#wechat_redirect)
**「小杜的生信筆記」** ,主要发表或收录生物信息学教程,以及基于R分析和可视化(包括数据分析,图形绘制等);分享感兴趣的文献和学习资料!!
页:
[1]