本期内容为[R语言可视化-精美图形绘制系列--柱状图误差线标记]
--
本期内容是一位童鞋咨询的,虽然是很简单的,他的原话说,一般的柱状图时可以注释上误差线,但是“簇状图”就不会了。那么我们这期就来搞一下。都是使用以前的码就可以实现。
是小杜好友的可以直接和小杜索要源代码和实例数据,我希望的是:交流(Communication)!!
![](https://img.haomeiwen.com/i18865546/657458742aaea2c3.png)
![](https://img.haomeiwen.com/i18865546/df8a5f6398375784.png)
![](https://img.haomeiwen.com/i18865546/44322083cfb738f1.png)
## 2022.10.27
## 柱状图添误差线
##----小杜的生信笔记
## "簇生柱状图“ ????
## -------------------------
## 导入相关的包
library(ggplot2)
## 导入数据
setwd("E:\\小杜的生信筆記\\20221027_柱状图添加误差线")
df <- read.table("inut.data.txt", header = T)
head(df)
## 绘制基础柱状图
ggplot(df, aes(x = Gene, y = Exp, fill = factor(Time)))+
geom_bar(position = "dodge", stat = "identity")
#factor()用于将数据从连续型转为离散型(或者称为“因子型”、“分类变量”);
#"dodge"使组内的柱子“肩并肩”显示;
##----------------------------------------
# 美化图形+添加误差线
ggplot(df, aes(x = Gene, y = Exp, fill = factor(Time)))+
geom_bar(position = position_dodge(0.8), ## position_dodge()增加柱子间隙
width = 0.7, ## width参数可更改柱子宽度
stat = "identity")+
## 添加误差线
geom_errorbar(aes(ymin=Exp-SE, ymax=Exp+SE),
position=position_dodge(0.8), ## 与上一步的position_dodge()参数一致,误差线则居中
width = 0.5, ## 宽度
colour = "black", size = 0.7)
### 美化
ggplot(df, aes(x= Gene, y = Exp, fill = factor(Time)))+
geom_bar(position = position_dodge(0.8),
width = 0.7, stat = "identity")+
geom_errorbar(aes(ymin = Exp-SE, ymax = Exp+SE),
position = position_dodge(0.8), width = 0.5, colour = "black", size = 0.7)+
## 下面的参数可以自己更改
theme_classic()+
# theme_bw()+
scale_fill_brewer(palette = "Accent")+
labs(fill = "Time(h)",x = "", y = "Expression level")+
theme(text = element_text(size=12))+
## 更改横纵坐标轴中的字体颜色、大小
theme(axis.text.x = element_text(color = "black",size = 12),
axis.text.y = element_text(color = "black",size = 12))
![](https://img.haomeiwen.com/i18865546/1f1647cab3f9160e.png)
### -----------
## 前面忘记添加标记差异性了
ggplot(df, aes(x= Gene, y = Exp, fill = factor(Time)))+
geom_bar(position = position_dodge(0.8),
width = 0.7, stat = "identity")+
geom_errorbar(aes(ymin = Exp-SE, ymax = Exp+SE),
position = position_dodge(0.8), width = 0.5, colour = "black", size = 0.7)+
## 添加显著标记
geom_text(aes(y = Exp+SE+0.4, label = maker), size = 6,
position = position_dodge(0.8))+
## 下面的参数可以自己更改
theme_classic()+
# theme_bw()+
scale_fill_brewer(palette = "Accent")+
labs(fill = "Time(h)",x = "", y = "Expression level")+
theme(text = element_text(size=12))+
## 更改横纵坐标轴中的字体颜色、大小
theme(axis.text.x = element_text(color = "black",size = 12),
axis.text.y = element_text(color = "black",size = 12))
ggsave("柱状图03.pdf",height = 8, width = 12)
![](https://img.haomeiwen.com/i18865546/2d7d7ef3964d1023.png)
网友评论