小杜的生信笔记 发表于 2024-6-16 21:08:37

绘制带P值的差异柱状图

[![](https://cdn.nlark.com/yuque/0/2024/png/38892215/1718542956641-50edce16-9dfd-400d-af0a-3fd53a872906.png)](http://mp.weixin.qq.com/s?__biz=MzAwODY5NDU0MA==&mid=2455861689&idx=2&sn=bf497c5f96fbccc071751f5e9eb087a5&chksm=8cff27dabb88aecc14142b2712e8bbd0f4ed2d6660b4814cd2031212edf682bd8c61f2fb391a&scene=21#wechat_redirect)

![](https://cdn.nlark.com/yuque/0/2024/jpeg/38892215/1718542956767-d2c513be-ac91-4b87-a43e-e067c3433486.jpeg)

[ 附件:20240616-封面.jpg](forum.php?mod=attachment&aid=291 "attachment")

「小杜的生信笔记」,自2021年11月开始做的知识分享,主要内容是「R语言绘图教程」、「转录组上游分析」、****「转录组下游分析」**等内容。凡是在社群同学,可免费获得自2021年11月份至今全部教程,教程配备示例数据和相关代码,我们会持续更新中。![](https://cdn.nlark.com/yuque/0/2024/jpeg/38892215/1718542957437-0a450c21-95aa-4316-888c-c7267316dad7.jpeg)![](https://cdn.nlark.com/yuque/0/2024/jpeg/38892215/1718542957571-da36669e-83ec-46a7-9594-935124b3a3d8.jpeg)![](https://cdn.nlark.com/yuque/0/2024/jpeg/38892215/1718542957678-f8c928c4-afb1-404b-bd10-11ee8337c634.jpeg)![](https://cdn.nlark.com/yuque/0/2024/jpeg/38892215/1718542957787-aafdded6-1c9f-4232-af41-b5b734a3b522.jpeg)![](https://cdn.nlark.com/yuque/0/2024/jpeg/38892215/1718542957789-d8b4419d-00c6-4bb1-8965-49739ba0e669.jpeg)![](https://cdn.nlark.com/yuque/0/2024/jpeg/38892215/1718542958232-4bc53b39-e36f-488e-8c76-9456ad87caa6.jpeg)![](https://cdn.nlark.com/yuque/0/2024/jpeg/38892215/1718542958348-753214c9-2ae7-48e5-a692-4166e1fe62a2.jpeg)![](https://cdn.nlark.com/yuque/0/2024/jpeg/38892215/1718542958711-031ada28-8618-4f87-a8fa-e58a218908e0.jpeg)![](https://cdn.nlark.com/yuque/0/2024/jpeg/38892215/1718542958582-d426393b-72b2-487d-aa2b-c8404ade570b.jpeg)![](https://cdn.nlark.com/yuque/0/2024/jpeg/38892215/1718542958572-a6d3805d-873c-4d76-ba76-7fed0d66de94.jpeg)![](https://cdn.nlark.com/yuque/0/2024/jpeg/38892215/1718542958765-108a16b3-b217-4a15-a39d-c33428e03a17.jpeg)![](https://cdn.nlark.com/yuque/0/2024/jpeg/38892215/1718542958927-2404a38d-208e-442f-af21-5aeda9ad763a.jpeg)![](https://cdn.nlark.com/yuque/0/2024/jpeg/38892215/1718542959131-67ae4163-526f-4b2e-81b3-28177bd9a89d.jpeg)

## 绘图代码

1. **绘图**

```plain
# 生成柱状图
ggplot(df_long, aes(x = time, y = value, fill = group)) +
geom_bar(stat = "summary", fun = "mean", position = position_dodge(width = 0.9), width = 0.7, color = "black") +
# 添加误差条,fun.data = mean_cl_normal 指定计算均值和置信区间
geom_errorbar(stat = "summary", fun.data = mean_cl_normal, position = position_dodge(width = 0.9), width = 0.2) +
# 使用 geom_beeswarm 函数添加蜂窝散点
geom_beeswarm(dodge.width = 0.8, size = 3, color = "black") +
scale_fill_manual(values = c("#009F72","#1F78B4")) +# "#096A62"
scale_y_continuous(expand = expansion(mult = c(0,0)),##将Y轴柱子从(0,0)开始
                     limits = c(0,300),# 设置Y轴的取值
                     ##Y轴的取值距离
                     breaks = seq(0,300,50) ##取值0-300,间隔50
                     )+
labs(x = "Time", y = NULL, fill = NULL) +
theme_classic() +
# 添加 p 值
stat_pvalue_manual(p_values, x = "time", label = "label",##label = p.adj.signif,星号
                     y.position = c(50, 240, 270), tip.length = 0.001, inherit.aes = FALSE) +
annotate("segment", x = 0.8, ## X轴距离
         xend = 1.2, ##长度
         y = 48,## y的高度
         yend = 48,
         color = "black") +
annotate("segment", x = 1.8, xend = 2.2, y = 238, yend = 238, color = "black") +
annotate("segment", x = 2.8, xend = 3.2, y = 268, yend = 268, color = "black") +
theme(axis.line = element_line(size = 0.8),## 粗细
    text=element_text(#family = "sans",
      colour ="black",size = 10),
    axis.text.x = element_text(color = "black", size = 10),
    axis.text.y = element_text(color = "black",size = 10),
    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()
)
```

![](https://cdn.nlark.com/yuque/0/2024/png/38892215/1718542959145-072e97e5-1dd1-4ac8-8977-f617ebdf7d51.png)

**若我们的教程对你有所帮助,请**点赞+收藏+转发**,这是对我们最大的支持。**

### 往期部分文章

**「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分析和可视化(包括数据分析,图形绘制等);分享感兴趣的文献和学习资料!!

小杜的生信笔记 发表于 2024-6-16 21:09:13

网站的MarkDown太需要改进了

admin 发表于 2024-6-16 23:22:35

小杜的生信笔记 发表于 2024-6-16 21:09
网站的MarkDown太需要改进了

好滴,我们正在抓紧修改
页: [1]
查看完整版本: 绘制带P值的差异柱状图