生信技能樹2021生信入門線上課筆記,需要結(jié)合課程講解服用
1.使用循環(huán),對iris的1到4列分別畫點圖(plot)
方案1:我的答案
> library(patchwork)
> library(ggplot2)
> p=list()
> for(i in 1:4) {
+ p[[i]] = ggplot(data = iris, aes(x = 1:nrow(iris), y = !!iris[, i])) +
+ geom_point(aes(color = Species))+
+ labs(x = "Number", y = colnames(iris)[i], title = "")
+ }
> n=wrap_plots(p,nrow=2,guides = 'collect')
> n
> ggsave(n,filename = "practice1.png")
方案2:老師的參考答案
par(mfrow = c(2,2))
for(i in 1:4){
plot(iris[,i],col = iris[,5])
}
2.生成一個隨機數(shù)(rnorm)組成的10行6列的矩陣适掰,列名為sample1,sample2….sample6抒钱,行名為gene1瞬欧,gene2…gene10,分組為sample1晦鞋、2蹲缠、3屬于A組棺克,sample4、5线定、6屬于B組娜谊。用循環(huán)對每個基因畫ggplot2箱線圖,并嘗試拼圖斤讥。
方案1:導出成單獨的圖再拼圖
m=matrix(rnorm(1:60),nrow = 10);m
colnames(m)=paste0('sample',1:6)
rownames(m)=paste0('gene',1:10)
n=t(m);n
n=as.data.frame(n)
class(n)
#增加列
library(dplyr)
n=mutate(n,group=rep(c('A','B'),each=3));n
#畫圖
library(ggplot2)
plot_list =list()
for (i in 1:(ncol(n)-1)) {
x = ggplot(data=n,aes(x=group, y=n[,i],fill=group)) +
stat_boxplot(geom ='errorbar', width = 0.3)+
geom_boxplot( width = 0.3)
plot_list[[i]] = x+labs(x = "Group", y = colnames(n)[i], title = "")
}
#保存圖
for (i in 1:(ncol(n)-1)) {
file_name = paste("practice2_", i, ".tiff", sep="")
tiff(file_name)
print(plot_list[[i]])
dev.off()
}
方案2:老師的答案參考
#生成矩陣
exp = matrix(rnorm(60),nrow = 10)
colnames(exp) <- paste0("sample",1:6)
rownames(exp) <- paste0("gene",1:10)
exp[1:4,1:4]
#dat = cbind(t(exp),group = rep(c("A","B"),each = 3))
dat = data.frame(t(exp))
dat = mutate(dat,group = rep(c("A","B"),each = 3))
p = list()
library(ggplot2)
for(i in 1:(ncol(dat)-1)){
p[[i]] = ggplot(data = dat,aes_string(x = "group",y=colnames(dat)[i]))+
geom_boxplot(aes(color = group))+
geom_jitter(aes(color = group))+
theme_bw()
}
library(patchwork)
wrap_plots(p,nrow = 2,guides = "collect")
# 分面也行的纱皆。
exp = matrix(rnorm(60),nrow = 10)
colnames(exp) <- paste0("sample",1:6)
rownames(exp) <- paste0("gene",1:10)
exp[1:4,1:4]
dat = data.frame(t(exp))
dat = mutate(dat,group = rep(c("A","B"),each = 3))
library(tidyr)
dat2 = gather(dat,key = "gene",value = "expression",-group)
ggplot(data = dat2)+
geom_boxplot(aes(x = group,y = expression,color = group))+
theme_bw()+
facet_wrap(~gene,nrow = 2)
方案3:基于老師的答案優(yōu)化我的答案
#生成矩陣
m=matrix(rnorm(1:60),nrow = 10);m
colnames(m)=paste0('sample',1:6)
rownames(m)=paste0('gene',1:10)
n=t(m);n
n=as.data.frame(n)
class(n)
#增加列
library(dplyr)
n=mutate(n,group=rep(c('A','B'),each=3));n
#畫圖
library(ggplot2)
plot_list =list()
for (i in 1:(ncol(n)-1)) {
plot_list[[i]] = ggplot(data=n,aes(x=group, y=!!n[,i],fill=group)) +
stat_boxplot(geom ='errorbar', width = 0.3)+
geom_boxplot( width = 0.3)+
labs(x = "Group", y = colnames(n)[i], title = "")
}
#拼圖
library(patchwork)
wrap_plots(plot_list,nrow = 2,guides = "collect")
- 模擬出幾個類似的文件,用R實現(xiàn)批量重命名
> folder<-setwd('D:/Desktop/practice/test')
> files<-list.files(folder)
> for (f in files){
+ newname<-sub('test','practice',f)
+ file.rename(f,newname)
+ }
dir()
[1] "practice2_1.png" "practice2_10.png"
[3] "practice2_2.png" "practice2_3.png"
[5] "practice2_4.png" "practice2_5.png"
[7] "practice2_6.png" "practice2_7.png"
[9] "practice2_8.png" "practice2_9.png"