R

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))

TIM圖片20200305193010.png

箱型圖:boxplot()
TIM截圖20200305193635.png

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

image.png

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 <- table(Arthritis$Improved)

counts
image.png

barplot(counts,
main="Simple Bar Plot",
xlab="Improvement", ylab="Frequency")
barplot(counts,
main="Horizontal Bar Plot",
xlab="Frequency", ylab="Improvement",
horiz=TRUE)

image.png

counts <- table(ArthritisImproved, ArthritisTreatment)
barplot(counts,
main="Stacked Bar Plot",
xlab="Treatment", ylab="Frequency",
col=c("red", "yellow","green"),
legend=rownames(counts),
beside = TRUE)
image.png

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)


image.png

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")


image.png

dot chart

dotchart(mtcars$mpg,
labels=row.names(mtcars),cex=0.7,
main="Gas Mileage for Car Models",

xlab="Miles Per G
image.png

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)


image.png

t(A)#置換行列


image.png

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=factor(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")


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=factor(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")


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=factor(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")


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")


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")


image.png

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()


TIM圖片20200424173038.jpg

行重排順序

newOrder=mat[heatmaptree_roworder,]

添加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)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市贱鼻,隨后出現(xiàn)的幾起案子宴卖,更是在濱河造成了極大的恐慌滋将,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,185評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件症昏,死亡現(xiàn)場離奇詭異随闽,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)肝谭,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門掘宪,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人分苇,你說我怎么就攤上這事添诉。” “怎么了医寿?”我有些...
    開封第一講書人閱讀 163,524評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵栏赴,是天一觀的道長。 經(jīng)常有香客問我靖秩,道長须眷,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,339評(píng)論 1 293
  • 正文 為了忘掉前任沟突,我火速辦了婚禮花颗,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘惠拭。我一直安慰自己扩劝,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,387評(píng)論 6 391
  • 文/花漫 我一把揭開白布职辅。 她就那樣靜靜地躺著棒呛,像睡著了一般。 火紅的嫁衣襯著肌膚如雪域携。 梳的紋絲不亂的頭發(fā)上甘萧,一...
    開封第一講書人閱讀 51,287評(píng)論 1 301
  • 那天俊啼,我揣著相機(jī)與錄音窍帝,去河邊找鬼稻艰。 笑死,一個(gè)胖子當(dāng)著我的面吹牛锋边,可吹牛的內(nèi)容都是我干的皱坛。 我是一名探鬼主播,決...
    沈念sama閱讀 40,130評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼豆巨,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼剩辟!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,985評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤抹沪,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后瓤球,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體融欧,經(jīng)...
    沈念sama閱讀 45,420評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,617評(píng)論 3 334
  • 正文 我和宋清朗相戀三年卦羡,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了噪馏。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,779評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡绿饵,死狀恐怖欠肾,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情拟赊,我是刑警寧澤刺桃,帶...
    沈念sama閱讀 35,477評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站吸祟,受9級(jí)特大地震影響瑟慈,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜屋匕,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,088評(píng)論 3 328
  • 文/蒙蒙 一葛碧、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧过吻,春花似錦进泼、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至廓推,卻和暖如春刷袍,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背樊展。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評(píng)論 1 269
  • 我被黑心中介騙來泰國打工呻纹, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人专缠。 一個(gè)月前我還...
    沈念sama閱讀 47,876評(píng)論 2 370
  • 正文 我出身青樓雷酪,卻偏偏與公主長得像,于是被迫代替她去往敵國和親涝婉。 傳聞我的和親對(duì)象是個(gè)殘疾皇子哥力,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,700評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容