[R语言可视化]--箱线图不同的画法及参数设置 | 学习笔记

本期内容为[R语言可视化]--箱线图不同的画法及参数设置

--昨天我们分享了[R语言可视化-精美图形绘制系列]--显著性箱线图,在后台有童鞋咨询了一些问题,针对这些问题,我们在来深入的学习一下。不学不知道,一学吓一跳,有很多的玩法呀。


教程代码:

01-差异显著性柱状图-昨日教程[R语言可视化-精美图形绘制系列]--显著性箱线图

## 01 首先,计算P值
pvalues <- sapply(d2$gene,  function(x)  { res <- aov(expr ~ stage,  data  = subset(d2, gene == x))  summary(res)[[1]]/pre>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))
  1. 不需要提前计算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


看出上两图的区别吗????(自己看吧!!)


  1. 新画法,添加了“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)  ## 变有角哦
  1. 添加柱状图的误差小横线,及散点的颜色,使用“ 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))
  1. 添加散点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()”函数,我的这个示范是错误的,也许是我的数据问题哦!!


  1. 更改外部线条颜色,使用“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")
  1. 填充箱子内部的颜色,我们最开始是使用“ggplot(d2, aes(gene, expr, fill=stage))”中的“fill = stage ”进行填充。那如果使用自定义颜色怎么弄呢?
ggplot(d2, aes(x= gene, y = expr, color = stage))+
  geom_boxplot(fill = c('#A4A4A4','#A4A4A4','#A4A4A4'))+
  theme_classic()
  #########################################################
ggplot(d2, aes(x= gene, y = expr, fill = stage))+
  geom_boxplot()+
  scale_fill_brewer(palette = "Dark2")


往期文章(总汇)

01-[R语言可视化-精美图形绘制系列]--精美火山图

[02-R语言可视化-精美图形绘制系列--柱状图

[03-R语言可视化-精美图形绘制系列--功能富集分析

[04-R语言可视化-精美图形绘制系列—多组GO富集可视化

05-[R语言可视化-精美图形绘制系列--堆积图]

06-[R语言可视化-精美图形绘制系列--组间相关性分析]

07-[R语言可视化-精美图形绘制系列]--Mental分析

08-[R语言可视化-精美图形绘制系列--复杂热图+两图渐变连线]-【转载】

09-[R语言可视化-精美图形绘制系列--桑基图(Sankey)]

10-[R语言可视化-精美图形绘制系列--柱状图误差线标记]

11-跟着NC学作图 | 柱状图与相关性图

12-[R语言可视化-精美图形绘制系列--GO、KEGG富集通路关联图]

13-[跟着“基迪奥生物学”作图]--截断图

014-[R语言可视化-精美图形绘制系列]--显著性箱线图

--

都看到最后了,点个赞再走吧!!

--

小杜的生信筆記 ,主要发表或收录生物信息学的教程,以及基于R的分析和可视化(包括数据分析,图形绘制等);分享感兴趣的文献和学习资料!

你可能感兴趣的:([R语言可视化]--箱线图不同的画法及参数设置 | 学习笔记)