尔云间 一个专门做科研的团队
原创 小果 生信果 关注我们
今天小果看到一个好看的差异检验箱线图,自己想通过ggplot2来绘制一下,通过这次绘图可以学习到tidyverse数据格式转化,分面和差异检验等绘图知识,代码如下:
01 安装需要的R包
install.packages(“tidyverse”)
install.packages(“reshape2”)
install.packages(“ggsignif”)
install.packages(“ggsci”)
install.packages(“ggplot2”)
02 导入需要的R包
library(tidyverse)
library(reshape2)
library(ggsci)
library(ggsignif)
library(ggplot2)
03 代码展示
#读取数据
Sample<-read.table("Sample.txt",header=T)
data<-read.table("data.txt",header=T)
#数据格式转换
df<-left_join(data,Sample,by="Sample")
df1<-df %>%pivot_longer(-c(Sample,trat1))
df1<-df1%>%rename(group=trat1)
df1<-df1%>%select(-Sample)
#开始绘图
ggplot(data=df1,aes(x=group,y=value,color=group))+
geom_boxplot()+
geom_point()+
#差异检验
geom_signif(comparisons=list(c("A","D"),
c("B","D"),
c("C","D")),
textsize = 3,
test=t.test,
step_increase=0.2,
map_signif_level=T)+
#分面
facet_wrap(~name,scale="free")+
scale_color_npg()+
labs(x="",y="")+
theme_classic()+
theme(strip.background=element_rect(fill="grey"),
strip.text=element_text(color="red",
face="bold",
size=10))
#保存图片
ggsave("boxplot.pdf",height=14,width=12)
最终绘制出了想要的图形效果,图片得到复现,有需要的可以借鉴学习,今天小果的分享就到这里奥。
生信人R语言学习必备
立刻拥有一个Rstudio账号
找小果开启升级模式吧
(56线程,256G内存,个人存储1T)
“生信果”,生信入门、R语言、生信图解读与绘制、软件操作、代码复现、生信硬核知识技能、服务器、生物信息学的教程,以及基于R的分析和可视化等原创内容,一起见证小白和大佬的成长。