[R语言可视化]--箱线图不同的画法及参数设置 | 学习笔记
本期内容为 [R语言可视化]--箱线图不同的画法及参数设置
--昨天我们分享了 [R语言可视化-精美图形绘制系列]--显著性箱线图 ,在后台有童鞋咨询了一些问题,针对这些问题,我们在来深入的学习一下。不学不知道,一学吓一跳,有很多的玩法呀。
--
老规矩!!
是小杜好友的可以直接和小杜索要源代码和实例数据,我希望的是:交流(Communication)!!
欢迎来扰!!不要客气,要不你的加友费浪费了哦!
---
从分享自己的学习教程至今快有小一年了,我的账号没有使用钞能力推广。因此感谢大家对我的关注。自己是处于“小白”阶段这个层次,自己一直是这样的定义。自己的更新是不定时的,也许你会持续看到我更新,也许你很久看不到我更新文章,这都是正常状态,自己一个人的精力有限,请谅解。主要原因是自己更新的是自己的学习笔记。也希望大家可以投稿,分享自己的学习笔记
此外, 有童鞋问 : 是否有“粉丝群” ,我的天啊!我也会有一天可以建立自己的“群”! 但是,现在我暂时是不会建群! 因为,自己能力有限。不能很好的帮助大家,以及建立这些群管理起来也是比较费时间的。
但是,但是,如果你愿意,可以加小杜的微信 , 【前提:需转账50元(不介意更多)】 。 目前,只要是小杜的微信好友,都可以直接向小杜索要每期的教程的实例数据和代码 (这部分老铁也是后期小杜“粉丝群”的首选人员,哈哈哈哈........) [VX可在公众号界面获得] 。
温馨提示:这个请根据自己的意愿选择,不要盲目添加。小杜也是生信小白,也许无法提供给你想要的帮助,请理智消费 。
---
我们童鞋最关心的一件事:如何获得绘图代码和数据!!
途径一:给本文打赏,小杜看到后立即将本文的实例数据和代码发给你!
途径二:加好友,后期可以获得所有教程代码!
---
教程代码:
01-差异显著性柱状图-昨日教程 [R语言可视化-精美图形绘制系列]--显著性箱线图
## 01 首先,计算P值
pvalues <- sapply(d2$gene, function(x) {
res <- aov(expr ~ stage, data = subset(d2, gene == x))
summary(res)[[1]]$'Pr(>F)'[1] #
pv <- data.frame(gene = d2$gene, pvalue = pvalues)
## Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
pv$sigcode <- cut(pv$pvalue, c(0, 0.001, 0.01, 0.05, 0.1, 1),
labels=c('***', '**', '*', '.', ' '))
## 绘图
ggplot(d2, aes(gene, expr, fill=stage),
palette = 'jco',add = 'jitter',size = 5, axis.line = 2) +
geom_boxplot() +
geom_text(aes(gene, y=max(d2$expr) * 1.1,
label=pv$sigcode),
data=pv, inherit.aes=F) +
xlab(NULL)+ylab("Relative expression (log2)")+
theme_classic()+
## 添加颜色
scale_fill_brewer(palette = "Dark2")+
## 更改字体大小
theme(text = element_text(color = "black",size = 12),
axis.text.x = element_text(color = "black",size = 12),
axis.text.y = element_text(color = "black",size = 10),
axis.title=element_text(size=12))
02. 不需要提前计算P值的画法,添加“stat_compare_means()”函数
ggplot(d2, aes(gene, expr, fill=stage),
palette = 'jco',add = 'jitter',size = 5, axis.line = 2) +
geom_boxplot() +
xlab(NULL)+ylab("Relative expression (log2)")+
theme_classic()+
## 添加颜色
scale_fill_brewer(palette = "Dark2")+
## 更改字体大小
theme(text = element_text(color = "black",size = 12),
axis.text.x = element_text(color = "black",size = 12),
axis.text.y = element_text(color = "black",size = 10),
axis.title=element_text(size=12))+ ## y轴字体大小
theme(legend.text = element_text(size = 12), ## 标签字体大小
legend.title = element_text(size = 12))+ ##标题字体大小
## 计算P值
stat_compare_means(label = "p.signif",aes(group=stage))
#stat_compare_means() ## 显示P值,wilcoxon , p
看出上两图的区别吗????(自己看呀,哈哈哈哈)
03. 新画法,添加了“geom_boxplot(notch = T)”参数
ggplot(d2, aes(gene, expr, fill=stage),
palette = 'jco',add = 'jitter',size = 5, axis.line = 2) +
geom_boxplot() +
xlab(NULL)+ylab("Relative expression (log2)")+
theme_classic()+
## 添加颜色
scale_fill_brewer(palette = "Dark2")+
stat_compare_means()+ ## 差异比较
geom_boxplot(notch = T) ## 变有角哦ggplot(d2, aes(gene, expr, fill=stage),
04. 添加柱状图的误差小横线,及散点的颜色,使用“ stat_boxplot()”函数更改。
ggplot(d2, aes(gene, expr, fill=stage)) +
geom_boxplot() +
xlab(NULL)+ylab("Relative expression (log2)")+
theme_classic()+
## 添加颜色
scale_fill_brewer(palette = "Dark2")+
stat_compare_means()+
geom_boxplot(col = "black", linetpe = 2)+
## 更改点的颜色
stat_boxplot(aes(ymin = ..lower.., ymax = ..upper..),
outlier.shape = 15, outlier.colour = "red", outlier.fill = "blue")+
## 添加误差线
stat_boxplot(geom = "errorbar", aes(ymin = ..ymax..), width = 0.2, color = "black",
position = position_dodge(0.75))+
stat_boxplot(geom = "errorbar", aes(ymax = ..ymin..), width = 0.2, color = "black",
position = position_dodge(0.75))
05. 添加散点Plot,使用“geom_dotplot()”
ggplot(d2, aes(gene, expr, fill=stage)) +
geom_boxplot() +
xlab(NULL)+ylab("Relative expression (log2)")+
theme_classic()+
## 添加颜色
scale_fill_brewer(palette = "Dark2")+
stat_compare_means()+
geom_boxplot(col = "black", linetpe = 2)+
## 更改点的颜色
stat_boxplot(aes(ymin = ..lower.., ymax = ..upper..),
outlier.shape = 15, outlier.colour = "red", outlier.fill = "blue")+
## 添加误差线
stat_boxplot(geom = "errorbar", aes(ymin = ..ymax..), width = 0.2, color = "black",
position = position_dodge(0.75))+
stat_boxplot(geom = "errorbar", aes(ymax = ..ymin..), width = 0.2, color = "black",
position = position_dodge(0.75))+
geom_dotplot(binaxis = "y", stackdir = 'center', dotsize = 0.5)
使用“geom_jitter()”函数,我的这个示范是错误的,也许是我的数据问题哦!!
06. 更改外部线条颜色,使用“scale_color_manual(values = c("#999999", "#E69F00", "#56B4E9"))”自定义颜色,或使用“scale_color_brewer()”函数。
ggplot(d2, aes(x= gene, y = expr, color = stage))+
geom_boxplot()+
#theme_classic()+
#scale_fill_brewer(palette = "Dark2")+
stat_compare_means()+
scale_color_manual(values = c("#999999", "#E69F00", "#56B4E9"))
#scale_color_brewer(palette = "Dark2")
07. 填充箱子内部的颜色,我们最开始是使用“ggplot(d2, aes(gene, expr, fill=stage))”中的“fill = stage ”进行填充。那如果使用自定义颜色怎么弄呢?
ggplot(d2, aes(x= gene, y = expr, color = stage))+