美文网首页
2021生信技能树R语言终极练习题

2021生信技能树R语言终极练习题

作者: 超级可爱的懂事长鸭 | 来源:发表于2021-03-21 17:18 被阅读0次

    生信技能树2021生信入门线上课笔记,需要结合课程讲解服用

    1.使用循环,对iris的1到4列分别画点图(plot)
    方案1:我的答案

    参考https://www.jianshu.com/p/4b7f3b9e4cf2

    > 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")
    
    方案1.png

    方案2:老师的参考答案

    par(mfrow = c(2,2))
    for(i in 1:4){
      plot(iris[,i],col = iris[,5])
    }
    
    方案2.png

    2.生成一个随机数(rnorm)组成的10行6列的矩阵,列名为sample1,sample2….sample6,行名为gene1,gene2…gene10,分组为sample1、2、3属于A组,sample4、5、6属于B组。用循环对每个基因画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() 
    }
    
    方案1.png

    方案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)
    
    image.png
    分面.png

    方案3:基于老师的答案优化我的答案

    #生成矩阵
    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")
    
    方案3.png
    1. 模拟出几个类似的文件,用R实现批量重命名
    > 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"
    
    修改前.png
    修改后.png

    相关文章

      网友评论

          本文标题:2021生信技能树R语言终极练习题

          本文链接:https://www.haomeiwen.com/subject/ozmecltx.html