0228
1.some refernce material
R Cookbook
R in Action
ggplot2
Advanced R
2.Installing and Loading Package
installing:install.packages('ggplot2')
loading:library(ggplot2)
updating:update.packages()
3.R language basics
create a vector: v=(1,4,4,3,2,2,3) or w=c("apple","banana","orange")
return certain elements: v【c(2,3,4)】 or v【2:4】 v【c(2,4,3)】
Delete certain element:v=【-2】 刪除第二個(gè)元素 or v=【-2:-4】 刪除2到4個(gè)元素
Extract element: v【v<3】 提取所有小于3的元素
Find elements: which(v==3) NOTE:the returns are the indices of elements,“=”是賦值臊泌,“==”是相等。
which.max(v)最大值
which.min(v)最小值
0229
4.Numbers隨機(jī)數(shù)
Repeatable Random Numbers:set.seed(250)【12:00】使電腦重復(fù)產(chǎn)生的隨機(jī)數(shù)相同
Random Number:a=runif(3,min=0,max=100)
Rounding of Numbers:floor(2)向下取整 ceiling(a)向上取整 round(a,4)括弧中的數(shù)字為保留的小數(shù)位數(shù)
Random Numbers from Other Distributions:rnorm()正態(tài)分布,rexp()指數(shù)分布,rbinom(),rgeom()幾何分布,rnbinom()負(fù)二項(xiàng)分布
?round 查詢命令
?阅悍?round 產(chǎn)生與查詢關(guān)鍵詞相關(guān)內(nèi)容
5.Data Input
loading local data:?read.csv;read.csv(file="……"); or read.table(file"……")
loading online data: read.csv("http://……")
attach:attach()
6.Graphs
plot:plot()
histograms:hist()直方圖
density plot:plot(density())密度圖
scatter plot:plot()散點(diǎn)圖
box plot:boxplot(time~)箱線圖
Q-Q plot:qqnorm(),qqline()and qqplot() quantiles & quanbles
par設(shè)定繪圖環(huán)境
hist(x,breaks=20,col="blue")繪制直方圖,豎條20根昨稼,填充為藍(lán)色
plot(density(x))繪制曲線圖
plot(x节视,type=“”)繪制散點(diǎn)圖,形式為*
boxplot(x,y)箱線圖 boxplot(time~***)區(qū)分性別
0330
a=c(1,2,3,4,5,6)
b=c("one","two","three")
c=c(TRUE,TRUE,FALSE)
x=matrix(1:20,nrow=5,ncol=4,byrow=TRUE)
y=matrix(1:20,nrow=5,ncol=4,byrow=FALSE)
x[2,]
x[,2]
x[2,c(2,4)]
x=[3:5,2]
rnames=c("apple","banana","orange","melon","corn")
cnanes=v("cat","dog","bird","pig")
rownames(x)=rnames
colnames(x)=cnames
0302
簡書調(diào)成markdown編輯模式
一級(jí)標(biāo)題
二級(jí)標(biāo)題
三級(jí)標(biāo)題
*斜體
我來自湖南
**加粗
黃鶴一去不復(fù)返
代碼引用
大家好
圖片引用
[圖片上傳失敗...(image-cdfa6e-1587743770549)]
引用下文章叭
一片孤城萬仞山
0303
1.putty安裝
從官網(wǎng)下載putty假栓,在putty中輸入ip地址寻行,再點(diǎn)open,再輸入用戶名和密碼
2.在putty上操作Linux系統(tǒng)
思維導(dǎo)圖:
TIM圖片20200303211602.png
0304
下載miniconda
1.搜索conda官網(wǎng),找到下載鏈接
2.ctrl+c復(fù)制鏈接
3.打開putty,cd biosoft
4.wget+右鍵粘貼鏈接
5.bash+剛下的文件
6.enter跳過加yes即可安裝
7.最后激活:source ~/.bashrc
8.添加清華鏡像:conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
conda config --set show_channel_urls yes
運(yùn)行miniconda
1.用conda list來查看
2.用conda search fastqc來搜索fastqc軟件
3.conda install fastqc -y來安裝
4.conda remove fastqc -y來卸載
備注:
我在添加清華鏡像的時(shí)候出現(xiàn)了錯(cuò)誤匾荆,這時(shí)可嘗試rm ~/.condarc來刪除之前的配置拌蜘,這樣就可以繼續(xù)添加了!牙丽!
0305
R語言的基礎(chǔ)
1.安裝R和Rstudio
1.百度搜索R和Rstudio,在相應(yīng)的網(wǎng)站中下好安裝
要確保自己的電腦用戶名是英文的哦
2.用R語言進(jìn)行一些畫圖
-50個(gè)正態(tài)分布隨機(jī)數(shù)的圓點(diǎn)圖:plot(rnorm(50))
箱型圖:boxplot()
3.R語言的基本操作
1.dir(目錄)到達(dá)路徑
2.<-賦值
3.刪除變量:rm(x)
4.列出歷史命令:history()
5.清空控制臺(tái):ctrl+l
0306
R語言數(shù)據(jù)結(jié)構(gòu)
賦值
1.一個(gè)值:v<-8
2.多個(gè)不連續(xù):v<-c(3,5,7)
3.多個(gè)連續(xù)值(1到10):v<-[1:10]
4.數(shù)列:v<-seq(1:10,by=0.5)
5.重復(fù):v<-rep(1:3,time=3)
取值
1.v[2]
2.v[1:3] 選第1到第3
3.v[c(1,3,4)] 選第1第3第4简卧、
4.v[-4] 除了第四個(gè)數(shù)
5.v[v==10] v中等于10的數(shù)
6.v[v<4] v中小于4的數(shù)
7.v[v %in% c(1,3,4,5,5)] v中存在于向量中的數(shù)
讀取本地?cái)?shù)據(jù)和對(duì)數(shù)據(jù)的操作
1.讀取數(shù)據(jù):read.csv()文件應(yīng)放置工作目錄
2.導(dǎo)出數(shù)據(jù):write.table()
3.設(shè)置行列名稱
列名:colnames(v)[1]<-"xxxxx"改第一列的名稱為xxxxx
行名:rownames(v)[1]<-"xxxxx"
4.數(shù)據(jù)保存:save.image(file="xxxx.Rdata")
單個(gè)變量保存:save.image(x,file="xxxx.Rdata")
5.提取數(shù)據(jù)框中元素:
x[x,y] 第x行第y列
x[x,] 第x行
x[,y] 第y列
6.將數(shù)據(jù)框名添加到搜索環(huán)境:attach(x)
問題
save(X,file="test.RData")這句代碼如果報(bào)錯(cuò)X not found,是為什么,應(yīng)該怎么解決剩岳?
答:是環(huán)境變量中沒有X這個(gè)變量贞滨,解決的話應(yīng)該找找是不是變量名弄錯(cuò)了,比如把小寫x弄成大寫
0307
0309
array
dim1=c("A1","A2")
dim2=c("B1","B2","B3")
dim3=c("C1","C2","C3","C4")
dim4=c("D1","D2","D3")
z=array(1:72, c(2,3,4,3),dimnames=list(dim1,dim2,dim3,dim4))
z[1,2,3,]
dataframe
attach(mtcars)
par(mfrow=c(1,4))#設(shè)置圖的個(gè)數(shù)
plot(rnorm(50),pch=17)#pch點(diǎn)的形狀
plot(rnorm(20),type = "l",lty=5)#lty線是形狀
plot(rnorm(100),cex=3)#cex點(diǎn)的大小
plot(rnorm(200),lwd=2)#lwd線的大小
?pch
title(main = "normal list")
axis()
legend()
attach(mtcars)#加入R搜索途徑
layout(matrix(c(1,1,2,3),2,2,byrow=TRUE))#矩陣
0311
對(duì)圖形的設(shè)置
par(mfrow=c(1,4))#設(shè)置圖的個(gè)數(shù)拍棕,partion
plot(rnorm(50),pch=17)#pch點(diǎn)的形狀
plot(rnorm(20),type = "l",lty=5)#lty線是形狀
plot(rnorm(100),cex=3)#cex點(diǎn)的大小
plot(rnorm(200),lwd=2)#lwd線的大小
?pch
title(main = "normal list")
axis()
legend()
attach(mtcars)#加入R搜索途徑
layout(matrix(c(1,1,2,3),2,2,byrow=TRUE))#矩陣
hist(wt)#直方圖
hist(mpg)
hist(disp)
hist(mtcars)
?pch
for和while的循環(huán)語句
for (i in 1:10) {#for loop循環(huán)晓铆,遍歷
print(i)
i=i+1
}
i=1
while(i <= 10){#while loop循環(huán)
print(i)
i=i+1
}
0312
if條件和switch條件
i=1
if(i==1){
print("hello world")
}else{
print("goodbye eorld")
}
i=3
if(i==1){#if的條件語句
print("hello")
}else if (i==3) {#多個(gè)條件可一直else if
print("goodbye")
}else{
print("good game")
}
feelings=c("sad","afraid")
for(i in feelings){
print(
switch(i,#swich的作用相當(dāng)于else if,轉(zhuǎn)換
happy="i am glad",
afraid="something to fear",
sad="cheer up",
angry="calm dowm"
)
)
}
0313
R語言中的user-defined function
myfunction=function(x){#R語言中的user defined function
for(i in feelings){
print(
switch(i,
happy="i am glad",
afraid="something to fear",
sad="cheer up",
angry="calm dowm"
)
)
}
}
myfunction=function(x,a,b,c){
return(asin(x)^2-bx+c)
}
curve(myfunction(x,20,3,4),xlim=c(1,20))#畫出剛剛定義的函數(shù)圖像
myfeeling=function(x){
for(i in feelings){
print(
switch(i,
happy="i am glad",
afraid="something to fear",
sad="cheer up",
angry="calm dowm"
)
)
}
}
feelings=c("sad","afraid")
myfeeling(feelings)
0314
bar plot
library(vcd)
counts
counts <- table(Arthritis$Improved)
image.png
barplot(counts,
main="Simple Bar Plot",
xlab="Improvement", ylab="Frequency")
barplot(counts,
main="Horizontal Bar Plot",
xlab="Frequency", ylab="Improvement",
horiz=TRUE)
counts <- table(Arthritis
barplot(counts,
main="Stacked Bar Plot",
xlab="Treatment", ylab="Frequency",
col=c("red", "yellow","green"),
legend=rownames(counts),
beside = TRUE)
pie plot
install.packages("plotrix")
library(plotrix)
slices <- c(10,12,4, 16, 8)
lbls <- c("US", "UK", "Australia", "Germany", "France")
pie(slices, labels = lbls,main="Simple Pie Chart",edges=300,radius=1)
0315
fan plot
slices <- c(10,12,4, 16, 8)
lbls <- c("US", "UK", "Australia", "Germany", "France")
fan.plot(slices,labels=lbls,main = "fan plot")
dot chart
dotchart(mtcars$mpg,
labels=row.names(mtcars),cex=0.7,
main="Gas Mileage for Car Models",
allon")
對(duì)數(shù)據(jù)的基本操作
head(mtcars)#展示前面六行
summary(mtcars)
attach(mtcars)
table(cyl)#統(tǒng)計(jì)該列數(shù)據(jù)的頻數(shù)
table(cut(mpg,seq(10,34,by=2)))#統(tǒng)計(jì)該列數(shù)據(jù)特定區(qū)間的頻數(shù)
0317
x = rnorm(100, mean = 10, sd = 1)
y = rnorm(100, mean = 30, sd = 10)
t.test(x, y, alt = "two.sided",paired=TRUE)#雙邊檢驗(yàn)
set.seed(123)
A = matrix(sample(100,15), nrow=5, ncol=3)
t(A)#置換行列
A+2
A-2
A2
A/2
set.seed(234)
B = matrix(sample(100,15), nrow=5, ncol=3)
t(A)
t(A) %% B#共有區(qū)域矩陣相乘
colMeans(A)#列的平均數(shù)
colSums(A)#列的和
crossprod(A,B)#A的置換,乘以B
0318
FACTOR
factor=factor(rep(c(1:3),times=5))#對(duì)變量做標(biāo)記
x=sample(100,15)
tapply(x,factor,mean)#用factor來對(duì)x標(biāo)記
rbind(x,factor)
boo=rbind(x,factor)[2,]==2
which(boo)
rbind(x,factor)[1,which(boo)]
mean(rbind(x,factor)[1,which(boo)])
bilibili:AV5625356
柱狀圖:0319
單樣品柱狀圖
file1="Anr.lib.stat.txt"
dat2=read.table(file=file1,check.names=F,header=T,sep="\t",comment.char = "")
對(duì)數(shù)據(jù)進(jìn)行排序處理
dat2=dat2[order(dat2[,2],decreasing=T),]
head(dat2)
畫圖
bar1=ggplot(dat2,aes(x=Species_Name,y=Homologous_Number))+
geom_bar(stat = "identity",position = "dodge",width = 0.8)
bar1
ggsave(bar1,filename = "hello.png",width = 12,height = 9)
dat2
?read.table
修改排序
dat2[,1]=factor(dat2[,1],levels = dat2[,1],order=T)
Other最后
ending=c("Other")
level=as.character(dat2[!dat2[,1]==ending,1])
level=unique(c(level,ending))
dat2[,1]=factor(dat2[,1],levels=level,order=T)
bar1=ggplot(dat2,aes(x=Species_Name,y=Homologous_Number))+
geom_bar(stat = "identity",position = "dodge",width = 0.8)
0320
設(shè)定和映射的差別
p_bar=ggplot(dat2,aes(x=dat2[,1],y=dat2[,2],fill=dat2[,1]))+
geom_bar(stat="identity",position ="dodge",width = 0.8)+
scale_fill_brewer(palette="Paired",direction=-1)
p_bar
調(diào)用R中的顏色包
RColorBrewer::display.brewer.all()
文字標(biāo)記
p_bar=p_bar+
geom_text(aes(label=paste(as.character(dat2[,3]*100),"%",sep="")),vjust = 1,size=3)
p_bar
標(biāo)簽文字的調(diào)整
p_bar=p_bar+labs(x="species",y="helo",title = "nothing")
p_bar
p_bar=p_bar+ggtitle(label="nothing",subtitle = "someshing")
p_bar
theme修改title,legend及背景
p_bar=p_bar+theme(
plot.title = element_text(size = 25,face = "bold", vjust = 0.5, hjust = 0.5),##title位置
axis.text.x=element_text(size = 10,face = "bold", vjust = 1, hjust = 1,angle = 45),##x軸文本位置
panel.background = element_rect(fill = "transparent",color = "black"),##表格內(nèi)背景
plot.background = element_rect(fill = "lightblue",colour = "red"),##圖樣背景
axis.ticks.x=element_blank(),
panel.grid.minor = element_blank(), ##表格內(nèi)格子
panel.grid.major = element_blank())
p_bar
321
多樣品柱狀圖
file2="nr.lib.stat.txt"
讀取數(shù)據(jù)
dat2=read.table(file2,sep="\t",check.names = F,header = T,comment.char = "")
head(dat2)
dat2=dat2[order(dat2[,3],decreasing = T),]
head(dat2)
相關(guān)標(biāo)簽設(shè)置
ending=c("other")
xlab="Species_Name"
ylab="Unigenes_num"
title="Nr"
subtitle="Homologous_Number"
固定順序
level=as.character(dat2[!dat2[,1]==ending,1])
level=unique(c(level,ending))
dat2[,1]=factor(dat2[,1],levels=level,order=T)
dat2[,2]=factor(dat2[,2],order=T)
基礎(chǔ)做圖
library(ggplot2)
p_bar2=ggplot(dat2,aes(x=dat2[,1],y=dat2[,3],fill=dat2[,2]))+
geom_bar(stat="identity",width=0.7,position ="dodge",color="darkgrey")
p_bar2
p_bar2=ggplot(dat2,aes(x=dat2[,1],y=dat2[,3],fill=dat2[,2]))+
geom_bar(stat="identity",width=0.7,position =position_dodge(width=0.9),color="darkgrey")
p_bar2
標(biāo)簽修改绰播,顏色修改#設(shè)置有aes(fill)生成的圖例
p_bar2=p_bar2+labs(x=xlab,y=ylab)+
ggtitle(label=title,subtitle = subtitle)
p_bar2
fill="Cultivar"
p_bar2=p_bar2+labs(x=xlab,y=ylab,fill=fill)+
ggtitle(label=title,subtitle = subtitle)
p_bar2
322
多樣品柱狀圖
file2="nr.lib.stat.txt"
讀取數(shù)據(jù)
dat2=read.table(file2,sep="\t",check.names = F,header = T,comment.char = "")
head(dat2)
dat2=dat2[order(dat2[,3],decreasing = T),]
head(dat2)
相關(guān)標(biāo)簽設(shè)置
ending=c("other")
xlab="Species_Name"
ylab="Unigenes_num"
title="Nr"
subtitle="Homologous_Number"
固定順序
level=as.character(dat2[!dat2[,1]==ending,1])
level=unique(c(level,ending))
dat2[,1]=factor(dat2[,1],levels=level,order=T)
dat2[,2]=factor(dat2[,2],order=T)
基礎(chǔ)做圖
library(ggplot2)
p_bar2=ggplot(dat2,aes(x=dat2[,1],y=dat2[,3],fill=dat2[,2]))+
geom_bar(stat="identity",width=0.7,position ="dodge",color="darkgrey")
p_bar2
p_bar2=ggplot(dat2,aes(x=dat2[,1],y=dat2[,3],fill=dat2[,2]))+
geom_bar(stat="identity",width=0.7,position =position_dodge(width=0.9),color="darkgrey")
p_bar2
標(biāo)簽修改骄噪,顏色修改#設(shè)置有aes(fill)生成的圖例
p_bar2=p_bar2+labs(x=xlab,y=ylab)+
ggtitle(label=title,subtitle = subtitle)
p_bar2
fill="Cultivar"
p_bar2=p_bar2+labs(x=xlab,y=ylab,fill=fill)+
ggtitle(label=title,subtitle = subtitle)
p_bar2
控制順序,基于建立的有序因子
p_bar2=p_bar2+scale_fill_brewer(palette="Set3",direction=1)
p_bar2
p_bar2=p_bar2+scale_fill_manual(values=c("red","turquoise"))
p_bar2
標(biāo)記文字
p_bar2=p_bar2+
geom_text(aes(label=paste(as.character(dat2[,4]*100),"%",sep="")),position=position_dodge(width=0.9),vjust = -0.5,size=2)
p_bar2
細(xì)節(jié)調(diào)整
p_bar2=p_bar2+
theme(
plot.title = element_text(size = 25,face = "bold", vjust = 0.5, hjust = 0.5),
legend.title = element_text(size = 15,face = "bold", vjust = 0.5, hjust = 0.5),
legend.text = element_text(size = 10, face = "bold"),
legend.position = 'right',
legend.key.size=unit(0.5,'cm'),
axis.text.x=element_text(size = 10,face = "bold", vjust = 1, hjust = 1,angle = 45),
axis.text.y=element_text(size = 10,face = "bold", vjust = 0.5, hjust = 0.5),
axis.title.x = element_text(size = 15,face = "bold", vjust = 0.5, hjust = 0.5),
axis.title.y = element_text(size = 15,face = "bold", vjust = 0.5, hjust = 0.5),
panel.background = element_rect(fill = "transparent",colour = "black"),
panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
plot.background = element_rect(fill = "transparent",colour = "black"))
p_bar2
堆疊柱狀圖
p_barS=ggplot(dat2,aes(x=dat2[,1],y=dat2[,3],fill=dat2[,2]))+
geom_bar(stat="identity",width=0.8,position ="stack")
p_barS
反向填充
p_barS=ggplot(dat2,aes(x=dat2[,1],y=dat2[,3],fill=dat2[,2]))+
geom_bar(stat="identity",width=0.8,position =position_stack(reverse = T))
p_barS
文字標(biāo)簽蠢箩,圖例反向等
p_barS=ggplot(dat2,aes(x=dat2[,1],y=dat2[,3],fill=dat2[,2]))+
geom_bar(stat="identity",width=0.8,position =position_stack(reverse = T))+
labs(x=xlab,y=ylab,fill=fill)+
ggtitle(label=title,subtitle = subtitle)+
scale_fill_manual(values=c("red","turquoise"))+
guides(fill=guide_legend(reverse=F))
p_barS
p_barS=p_barS+
geom_text(aes(label=paste(as.character(dat2[,4]*100),"%",sep="")),position=position_stack(vjust = 0.5,reverse = T),size=3)
p_barS
323
基礎(chǔ)餅狀圖
讀取數(shù)據(jù)
file1="Anr.lib.stat.txt"
dat1=read.table(file=file1,check.names=F,header=T,sep="\t",comment.char = "")
head(dat1,3)
dat1=dat1[order(dat1[,2],decreasing = T),]
head(dat1)
排序處理
ending="Other"
fill="Species"
title="Nr"
subtitle="Homologous_Number"
level=as.character(dat1[!dat1[,1]==ending,1])
level=unique(c(level,ending))
dat1[,1]=factor(dat1[,1],levels=level,order=T)
柱狀垛疊
p_pie=ggplot(dat1,aes(x="",y=dat1[,2],fill=dat1[,1]))+
geom_bar(stat="identity",width=1,position = position_stack(reverse = T))
p_pie
設(shè)置y軸極坐標(biāo)链蕊,方向
p_pie=p_pie+
coord_polar(theta="y",direction=-1)
p_pie
顏色填充設(shè)置
p_pie=p_pie+
scale_fill_brewer(palette ="Set3",direction = 1)
p_pie
圖例標(biāo)題修改
p_pie=p_pie+
labs(x="",y="",fill=fill)+
ggtitle(label =title,subtitle=subtitle)
p_pie
文字標(biāo)簽
p_pie=p_pie+geom_text(aes(label=paste(as.character(dat1[,3]*100),"%",sep="")),position =position_stack(vjust=0.5,reverse = T),size=3)
p_pie
刻度背景調(diào)整
p_pie=p_pie+
theme(
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
legend.title = element_text(hjust = 0.5),
axis.ticks = element_blank(),
axis.text.x = element_blank(),
panel.background = element_rect(fill = "transparent",colour = NA),
panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
plot.background = element_rect(fill = "transparent",colour = NA)
)
p_pie
數(shù)據(jù)分布——密度圖,箱線圖谬泌,直方圖
325
讀取數(shù)據(jù)
file="gene_fpkm.xls"
demo_fpkm=read.table(file,header = F,sep = "\t",check.names = F)
對(duì)數(shù)據(jù)進(jìn)行處理
fpkm=melt(demo_fpkm,variable.name = "Sample",value.name = "FPKM")
head(fpkm,10)
畫密度圖
p_density=ggplot(fpkm,aes(x=log(fpkm[,3]),color=fpkm[,2],fill=fpkm[,2]))+
geom_density(alpha=0.25,size=0.5)
p_density
修改標(biāo)度
p_density=p_density+xlim(-3,5)
p_density
標(biāo)題主題修改
p_density=p_density+
ggtitle("Gene expression density")+
labs(x="log10FPKM",color="Samples",fill="Samples")+
theme_bw()
p_density
箱線圖
p_box=ggplot(fpkm,aes(x=fpkm[,2],y=log10(fpkm[,3]),fill=fpkm[,2]))+
geom_boxplot(size=0.5,width=0.8,notch=T,outlier.shape = NA)
p_box
y軸范圍限制
p_box=p_box+ylim(-3,5)
p_box
標(biāo)題主題修改
p_box=p_box+
ggtitle("Gene expression distribution")+
labs(y="log10FPKM",x="",fill="Samples")+
theme_bw()
p_box
直方圖
326
畫點(diǎn)樣品直方圖
p_histogram=ggplot(fpkm,aes(x=log10(fpkm[,3]),fill=fpkm[,2]))+
geom_histogram(binwidth = 1,alpha=0.5,size=1,stat="bin")+
xlim(-3,5)
p_histogram
多樣品直方圖
默認(rèn)stack模式
p_histogram=ggplot(fpkm,aes(x=log10(fpkm[,3]),fill=fpkm[,2]))+
geom_histogram(binwidth = 1,alpha=0.5,size=1,stat="bin")+
xlim(-3,5)
p_histogram
分區(qū)
p_histogram=p_histogram+
facet_grid(.~fpkm[,2],scales = "free")
p_histogram
頻率型直方圖
p_histogram=ggplot(fpkm,aes(x=log10(fpkm[,3]),y=..density..))+
geom_histogram(aes(fill=fpkm[,2]),binwidth = 1,alpha=0.5,size=1,stat="bin")+
xlim(-3,5)
p_histogram=p_histogram+
facet_grid(.~fpkm[,2],scales = "free")
p_histogram
加上密度曲線
p_freqpoly=ggplot(fpkm,aes(x=log10(fpkm[,3]),color=fpkm[,2]))+
geom_freqpoly(binwidth = 1,alpha=0.5,size=1,stat="bin")+
xlim(-3,5)
p_freqpoly
頻數(shù)折線圖
p_h_f=p_histogram+
geom_freqpoly(aes(color=fpkm[,2]),binwidth = 1,alpha=0.5,size=1,stat="bin")
p_h_f
327
讀取數(shù)據(jù)
file="CK-WT_vs_T-WT.xls"
demo_DEG=read.table(file,check.names = F,header = T,sep = "\t")
head(demo_DEG)
設(shè)置閾值信息
line_FC=2
line_FDR=0.01
col=c("red","blue","grey")
AFPKM=c(2:4)
BFPKM=c(5:7)
通過閾值上下調(diào)信息
demo_DEG[demo_DEG[,"FDR"] <line_FDR & demo_DEG[,"log2FC"] >= log2(line_FC),ncol(demo_DEG)+1]="Up"
demo_DEG[demo_DEG[,"FDR"] <line_FDR & -log2(line_FC) >= demo_DEG[,"log2FC"],ncol(demo_DEG)]="Down"
demo_DEG[demo_DEG[,"FDR"] >=line_FDR | log2(line_FC) > abs(demo_DEG[,"log2FC"]),ncol(demo_DEG)]="Normal"
colnames(demo_DEG)[ncol(demo_DEG)]="Regulate"
head(demo_DEG)
火山圖
volcano=demo_DEG
有序因子的創(chuàng)建以便于顏色修改
volcanoRegulate,levels = c("Up","Down","Normal"),order=T)
初步畫火山圖
p_volcano=ggplot(volcano,aes(x=log2FC,y=-log10(FDR)))+
geom_point(aes(color=Regulate),alpha=0.5)
p_volcano
顏色的改變
p_volcano=p_volcano + scale_color_manual(values =col)
p_volcano
在基礎(chǔ)圖上加上閾值線
p_volcano=p_volcano +
geom_hline(yintercept=c(-log10(line_FDR)),linetype=4)+
geom_vline(xintercept=c(-log2(line_FC),log2(line_FC)),linetype=4)
p_volcano
主題修改
p_volcano=p_volcano+theme_bw()
p_volcano
保存
ggsave(p_volcano,filename = "six.png")
MA圖
328
讀取數(shù)據(jù)
head(demo_DEG,10)
設(shè)置xy軸名稱
MA=demo_DEG[,c("ID","log2FC","Regulate")]
head(MA,2)
MA[,4]=1/2log2(rowMeans(demo_DEG[,AFPKM])rowMeans(demo_DEG[,BFPKM]))
colnames(MA)[4]="1/2log2FPKM"
head(MA,3)
有序因子設(shè)置
MARegulate,levels = c("Up","Down","Normal"),order=T)
MA圖繪制
p_MA=ggplot(MA,aes(x=1/2log2FPKM
,y=log2FC))+
geom_point(aes(color=Regulate),alpha=0.5)
p_MA
設(shè)置x軸范圍
p_MA=p_MA+scale_x_continuous(limits = c(-5,15))
p_MA
顏色滔韵,閾值
加主題
p_MA=p_MA+
scale_color_manual(values =c("red","darkgreen", "darkgrey"))+
geom_hline(yintercept=c(-log2(line_FC),log2(line_FC)),linetype=4)+
theme_bw()
p_MA
保存
ggsave(p_MA,filename = "six(2).png")
329
柱狀圖(富集分析結(jié)果可視化)
library(ggplot2)
讀取數(shù)據(jù)
enrich="GO.enrich.txt"
demo_go=read.table(enrich,check.names = F,sep = "\t",header = T,comment.char = "")
head(demo_go)
對(duì)p值進(jìn)行排序,并取P值前20小的數(shù)據(jù)
go=demo_go[order(demo_go[demo_go$Pvalue<enrich,"Pvalue"],decreasing = F),]
head(go)
if(nrow(go)>=20){
go=go[1:20,]
}
head(go)
對(duì)二級(jí)范圍進(jìn)行分別排序
go=go[order(go$Term_type,decreasing = F),]
head(go)
顯著性高的排在前面
goDescription,levels = rev(go$Description),ordered = T)
篩選出P值為0的數(shù),并轉(zhuǎn)換為對(duì)數(shù)
go[go$Pvalue==0,"Pvalue"]=1e-15
畫圖掌实,對(duì)x軸和y軸進(jìn)行反轉(zhuǎn)
go_bar=ggplot(go,aes(x=Description,y=-log10(Pvalue),fill=Term_type))+
geom_bar(stat="identity",width = 0.8)
go_bar
go_bar=go_bar+
coord_flip()
go_bar
對(duì)文字標(biāo)簽的修改,主題修改
go_bar=go_bar+
geom_text(aes(label=as.character(DEGs)),position = "stack",vjust=0,hjust=0,size=3)+
theme_bw()
go_bar
保存
ggsave(go_bar,filename = "seven(1).png")
330
氣泡圖
設(shè)置閾值
enrich=0.01
minPvalue=1e-15
讀取數(shù)據(jù)
demo_go=read.table(file,header = T,check.names = F,comment.char = "",sep = "\t")
head(demo_go)
對(duì)p值進(jìn)行排序,并取P值前20小的數(shù)據(jù)
go=demo_go[order(demo_go[demo_go$Pvalue<enrich,"Pvalue"],decreasing = F),]
head(go)
if(nrow(go)>=20){
go=go[1:20,]
}
head(go)
篩選出P值為0的數(shù)陪蜻,并轉(zhuǎn)換為對(duì)數(shù)
go[go$Pvalue==0,"Pvalue"]=1e-15
畫氣泡圖
go_point=ggplot(go,aes(x=Description,y=Rich_factor))+
geom_point(aes(color=-log10(Pvalue),size=DEGs),alpha=0.8)+coord_flip()
go_point
對(duì)顏色進(jìn)行修改(漸變)
go_point=go_point+
scale_color_gradient(low = "green",high = "red")
go_point
保存
ggsave(go_point,filename = "seven(2).png")
331
用pheatmap來繪制
install.packages("pheatmap")
library(pheatmap)
讀取數(shù)據(jù)
file="All.DEG_final_3000.xls"
mat=read.table(file,check.names = F,header = T,sep = "\t",comment.char = "")
head(mat,3)
dim(mat)
pheatmap簡單畫圖
pheatmap(mat)
options(stringsAsFactors = TRUE)
對(duì)基因結(jié)果標(biāo)準(zhǔn)化(行)
pheatmap(mat,scale = "row")
隱藏行名
pheatmap(mat,scale="row",show_rownames=F)
改變顏色
library(RColorBrewer)
RColorBrewer::display.brewer.all()
col=c("blue","white","red")
color=colorRampPalette(col)(100)
pheatmap(mat2,scale="row",show_rownames=F,color=color)
單元格大小cell
cellheight=300/nrow(mat)
cellwidth=300/ncol(mat)
cellwidth=10
cellheight=10
pheatmap(mat,scale="row",show_rownames=F,color=color,cellwidth = cellwidth,cellheight = cellheight,border_color="black")
424
給熱圖添加注釋
col_file="annotation_col1.xls"
annotation_col=read.table(col_file,header = T,row.names=1,sep="\t",check.names = F,comment.char = "")
annotation_col
pheatmap(mat,scale="row",show_rownames=F,annotation_col = annotation_col)
pheatmap(mat,scale="row",show_rownames=F,annotation_col = annotation_col,annotation_colors = ann_colors)
離散分類取色
brewer.pal.info
qual=rownames(brewer.pal.info[brewer.pal.info[,"category"]=="qual",])
qualColor=c()
for(i in qual){
qualColor=c(qualColor,brewer.pal(brewer.pal.info[i,"maxcolors"], i))
}
length(qualColor)
length(unique(qualColor))
qualColor=unique(qualColor)
qualColor
seq為數(shù)值分類
seqColor=list(Blues=c("#F7FBFF","#08306B"),Reds=c("#FFF5F0","#67000D"),
Greys=c("#FFFFFF","#000000"))
seqColor
設(shè)置注釋顏色
annotation_color=list()
類型
char=1
num=1
for(i in colnames(annotation_col)){
if(is.numeric(annotation_col[,i])){
annotation_color[[i]]=seqColor[[num]]
num=num+1
}else{
n=length(table(annotation_col[,i]))
annotation_color[[i]]=qualColor[char:(char+n-1)]
names(annotation_color[[i]])=names(table(annotation_col[,i]))
char=char+n
}
}
查看
annotation_color
畫圖
pdf(file=paste(workdir,"/gene_heatmap.pdf",sep=""),width = 9,height = 9)
heatmap=pheatmap(mat,color=color,cellwidth = cellwidth,cellheight = cellheight,scale="row",
annotation_col = annotation_col,annotation_colors = annotation_color,
show_rownames=F,fontsize_col=8,fontsize=7)
dev.off()
行重排順序
newOrder=mat[heatmaporder,]
添加cluster
cluster=10
row_cluster=cutree(heatmap$tree_row,k=cluster)
newOrder[,ncol(newOrder)+1]=row_cluster[match(rownames(newOrder),names(row_cluster))]
colnames(newOrder)[ncol(newOrder)]="Cluster"
head(newOrder,2)
write.table(newOrder,file = paste(workdir,"/gene_newOrder_withCluster.xls",sep = ""),sep="\t",row.names = T,col.names = T,quote = F)