R語言繪圖系列:
- R語言可視化及作圖1--基礎(chǔ)繪圖(par函數(shù)征讲,散點(diǎn)圖,盒形圖,條形圖立美,直方圖)
- R語言可視化及作圖2--低級繪圖函數(shù)
- R語言可視化及作圖3--圖形顏色選取
- R語言可視化及作圖4--qplot和ggplot2美學(xué)函數(shù)
- R語言可視化及作圖5--ggplot2基本要素和幾何對象匯總
- R語言可視化及作圖6--ggplot2之點(diǎn)圖脖苏、條形圖、盒形圖滤祖、直方圖筷狼、線圖
- R語言可視化及作圖7--ggplot2之標(biāo)簽、圖例和標(biāo)題繪制
- R語言可視化及作圖8--坐標(biāo)軸自定義和坐標(biāo)系轉(zhuǎn)換
- R語言可視化及作圖9--主題函數(shù)
- R語言可視化及作圖10--ggplot2的theme函數(shù)
- R語言可視化及作圖11--圖片分面函數(shù)和一頁多圖
venn圖:展示不同的分類變量之間互相重疊的關(guān)系
火山圖:展示基因表達(dá)差異
熱圖:展示不同基因表達(dá)聚類
1. venn圖
1.1 limma包中的venn
library(limma)
#隨機(jī)生成一個矩陣(沒有實(shí)際意義)
Y <- matrix(rnorm(100*6),100,6)
Y[1:10,3:4] <- Y[1:10,3:4]+3
Y[1:20,5:6] <- Y[1:20,5:6]+3
desigh <- cbind(1,c(0,0,1,1,0,0),c(0,0,0,0,1,1))
fit <- eBayes(lmFit(Y,desigh)) #進(jìn)行貝葉斯回歸匠童,生成fit擬合模型埂材,fit作為venn圖繪圖對象。
results <- decideTests(fit) #decideTests函數(shù)在不同的基因和樣本間進(jìn)行多重對照
a <- vennCounts(results)
print(a)
mfrow.old <- par()$mfrow
op <- par(mfrow=c(1,2))
vennDiagram(results,
include = c('up','down'),
counts.col = c('red','blue'),
circle.col = c('red','blue','green3'))
par(op)
1.2 venn.diagram()函數(shù)
library(VennDiagram)
#Then generate 3 sets of words, There I generate 3 times 200 SNPs names.
SNP_pop_1=paste(rep('SNP_',200),sample(c(1:1000),200,replace = F),sep = '')
SNP_pop_2=paste(rep('SNP_',200),sample(c(1:1000),200,replace = F),sep = '')
SNP_pop_3=paste(rep('SNP_',200),sample(c(1:1000),200,replace = F),sep = '')
venn.diagram(
x=list(SNP_pop_1,SNP_pop_2,SNP_pop_3),
category.names =c('SNP_pop_1','SNP_pop_2','SNP_pop_3'),
filename = 'venn_diagramm.png', #輸出路徑汤求,最好是完整路徑俏险。
output=TRUE,
imagetype = 'png',
height = 800,
width = 800,
resolution = 300,
compression = 'lzw',
lwd=2,
lty='blank',
fill=c('yellow','purple','green'),
cex=1,
fontface='bold',
fontfamily='sans',
cat.cex=0.6, #cat設(shè)置分類標(biāo)簽的格式
cat.fontface='bold',
cat.default.pos='outer',
cat.pos=c(-27,27,135),
cat.dist=c(0.055,0.055,0.085),
cat.fontfamily='sans',
rotation=1
)
2. 火山圖
2.1 limma包volcanoplot()
sd <- 0.3*sqrt(4/rchisq(100,df=4))
y <- matrix(rnorm(100*6,sd=sd),100,6)
rownames(y) <- paste('Gene',1:100)
y[1:2,4:6] <- y[1:2,4:6]+2
design <- cbind(Grp1=1,Grp2vs1=c(0,0,0,1,1,1))
options(degits=3)
fit <- lmFit(y,design)
fit <- eBayes(fit)
topTable(fit,coef = 2)
dim(fit)
colnames(fit)
rownames(fit)[1:10]
names(fit)
#Fold change thresholding
fit2 <- treat(fit,lfc=0.1)
topTreat(fit2,coef = 2)
#volcano plot
volcanoplot(fit,coef = 2,highlight = 5)
2.2 ggplot2
data_df <- fit2$coefficients
class(data_df)
data_df <- as.data.frame(data_df)
data_df$fac <- as.factor(ifelse(data_df$Grp1>0,1,0))
class(data_df$fac)
library(ggplot2)
p <- ggplot()+geom_point(aes(Grp2vs1,Grp1,color=fac),data=data_df)
#對前五個差異最大的基因標(biāo)上名字
data_df$name <- paste('gene',1:100)
data_df <- data_df[order(data_df$Grp1,decreasing = T),]
data_df$name[6:100] <- NA
p+geom_text(aes(Grp2vs1,Grp1,label=name),data = data_df,nudge_y = -0.02,nudge_x = 0.05)+theme_classic()
3. 熱圖
data=as.matrix(mtcars)
head(data)
heatmap(data)
可以看到有些值過高,因此需要標(biāo)準(zhǔn)化
heatmap(data,scale = 'column')
heatmap(data,Colv = NA, Rowv = NA, scale = 'column')
更改顏色:
1: native palette from R
heatmap(data,scale = 'column',col=cm.colors(256))
heatmap(data,scale = 'column',col=terrain.colors(256))
- RColorBrewer palette
library(RColorBrewer)
coul=colorRampPalette(brewer.pal(8,'PiYG'))(25)
heatmap(data,scale = 'column',col=coul)
**Custom x and y labels with CexRow and labRow (col respectively)
heatmap(data,scale = 'column',cexRow = 1.5,labRow = paste('new_',rownames(data),sep=''),col=colorRampPalette(brewer.pal(8,'Blues'))(25))