day7—xiaode

作者: 1c9f38e0bb6b | 来源:发表于2021-03-01 10:01 被阅读0次

    1.基础数据结构

    1.1 向量

    # 创建向量a <-c(1, 2, 3, 4, 5, 6)

    b<-c("one", "two", "three")

    c<-c(TRUE, FALSE, TRUE, TRUE, FALSE)#向量索引a[2] #第二个元素a[-2] #删除第二个元素a[c(2:4)] #取出第二到第四个元素[1] 2[1] 2[1] 1 3 4 5 6

    图片

    1.2 矩阵

    #创建矩阵mymat <- matrix(c(1:10), nrow=2, ncol=5, byrow=TRUE)#矩阵索引mymat[2,] #取第二行mymat[,2] #取第二列mymat[1,5] #第一行第五列的元素

    图片

    1.3 数组

    #创建数组myarr <- array(c(1:12),dim=c(2,3,2))

    dim(myarr) #取矩阵或数组的维度myarr[1,2,1] #取第一个矩阵的第一行第二列

    1.4 数据框

    图片

    图片

    # 创建数据框kids <- c("Wang", "Li")

    age <- c("18", "16")

    df <- data.frame(kids, age)#数据框索引df[1,] #第一行df[,2] #第二列df[1:2,1:2]#前两行,前两列df$kids #根据列名称#数据框常用函数str(df) #数据框的结构rownames(df) #行名称colnames(df) #列名称

    1.4.1 因子变量

    变量:类别变量,数值变量

    类别数据对于分组数据研究非常有用。(男女,高中低)

    R中的因子变量类似于类别数据。

    #向量因子化status<-c("Poor", "Improved", "Excellent", "Poor")

    status<-factor(status,ordered=TRUE,

        levels= c("Poor","Improved", "Excellent"),

        labels=c("P","I","E"))

    index <- sample(1:100,75)

    plotdata <- data.frame(index,status)

    attach(plotdata)

    boxplot(index~status,col="red")

    图片类别变量,有序变量称为因子,决定了数据的分析方式和视觉呈现形式

    Attach()可以将数据框添加到R的搜索路径中,当R遇到一个变量名后,将检测搜索路径中的数据框,定位这个变量

    1.5 列表

    列表以一种简单的方式组织和调用不相干的信息,R函数的许多运行结果都是以列表的形式返回

    #创建列表lis <- list(name='fred',

        wife='mary',

        no.children=3,

        child.ages=c(4,7,9))#列表索引lis$name #列表组件名lis[[1]] #列表位置访问

    常用函数

    图片

    图片

    R流程控制

    图片

    p <- 0.1

    if(p<=0.05){  print("p<=0.05!")

    }else{  print("p>0.05!")

    }

    图片

    for(i in 1:10) {  print(i)

    }

    i <- 1while(i<10)

      {    print(i)

        i <- i + 1

      }

    图片

    v <- LETTERS[1:6]for (i in v){  if(i == 'D'){

        next

      }  print(i)

    }

    图片

    v <- LETTERS[1:6]for (i in v){  if(i == 'D'){    break

      }  print(i)

    }

    2.5 R函数

    函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段

    rcal<-function(x,y){

      z <- x^2 + y^2;

      result<-sqrt(z) ;

      result;

    }

    rcal(3,4)# 调用函数

    3. 读写数据

    #数据读入

    getwd()

    setwd('C:/Users/Administrator/Desktop/file')

    dir()

    top<-read.table("otu_table.p10.relative.tran.xls",header=T,row.names=1,sep='\t',stringsAsFactors = F)

    top10<-t(top)

    head(top10, n=2)#数据写出logtop10<-log(top10+0.000001)

    write.csv(logtop10,file="logtop10.csv", quote=FALSE,  row.names = TRUE)

    write.table(logtop10,file="logtop10.xls",sep="\t", quote=FALSE,

        row.names = TRUE, col.names = TRUE)

    其他常用函数

    图片

    4.数据清理

    图片

    4.1 tidyr包

    tidyr包的四个函数

    宽数据转为长数据:gather()

    长数据转为宽数据:spread()

    多列合并为一列: unite()

    将一列分离为多列:separate()

    library(tidyr)

    gene_exp <- read.table('geneExp.csv',header = T,sep=',',stringsAsFactors = F)

    head(gene_exp) #gather 宽数据转为长数据gene_exp_tidy <- gather(data = gene_exp, key = "sample_name", value = "expression", -GeneID)

    head(gene_exp_tidy)#spread 长数据转为宽数据gene_exp_tidy2<-spread(data = gene_exp_tidy, key = "sample_name", value = "expression")

    head(gene_exp_tidy2)

    图片

    4.2 dplyr包

    dplyr包五个函数用法:

    筛选: filter

    排列: arrange()

    选择: select()

    变形: mutate()

    汇总: summarise()

    分组: group_by()

    library(tidyr)

    library(dplyr)

    gene_exp <- read.table("geneExp.csv",header=T,sep=",",stringsAsFactors = F)

    gene_exp_tidy <- gather(data = gene_exp, key = "sample_name", value = "expression", -GeneID)#arrange 数据排列gene_exp_GeneID <- arrange(gene_exp_tidy, GeneID)#降序加deschead(gene_exp_GeneID )#filter 数据按条件筛选gene_exp_fiter <- filter(gene_exp_GeneID ,expression>10)

    head(gene_exp_fiter)#select 选择对应的列gene_exp_select <- select(gene_exp_fiter ,sample_name,expression)

    head(gene_exp_select)

    图片

    5. 绘图

    图片

    5.1 长数据与宽数据

    图片

    library(tidyr)

    library(ggplot2)#基础绘图file <- read.table("geneExp.csv",header=T,sep=",",stringsAsFactors = F,row.names = 1)#宽数据file

    barplot(as.matrix(file),names.arg = colnames(file), beside =T ,col=terrain.colors(6))

    legend("topleft",legend = rownames(file),fill = terrain.colors(6))#ggplot2绘图gene_exp <- read.table("geneExp.csv",header=T,sep=",",stringsAsFactors = F)

    gene_exp_tidy <- gather(data = gene_exp, key = "sample_name", value = "expression", -GeneID)#长数据head(gene_exp_tidy)

    ggplot(gene_exp_tidy,aes(x=sample_name,y=expression,fill=GeneID)) + geom_bar(stat='identity',position='dodge')

    图片

    5.2 图形参数位置

    图片

    x <- rnorm(20, 2, 1)

    y <- rnorm(20, 4, 2)

    plot(x, y, cex=c(1:3), type="p", pch=19, col = "blue",

        cex.axis=1.5, col.axis="darkgreen", font.axis=2,

        main="这是主标题:plot初试", font.main=2, cex.main=2, col.main="green",

        sub="这是副标题:图1", font.sub=3, cex.sub=1.5, col.sub="red",

        xlab="这是x轴标签", ylab="这是y轴标签",cex.lab=1.5, font.lab=2, col.lab="grey20",

        xlim=c(0,3), ylim=c(0,7))

    abline(h=2, v=3, lty=1:2, lwd=2,col="red")

    legend("topright", legend="我是图例\n我在这儿",

          text.col="red", text.width=0.5)#Rnorm正态分布 个数 平均值 标准差 plot是泛型函数,根据输入类型的不同而变化#Type p 代表点 l 代表线 b 代表两者叠加

    图片

    图形参数:

    符号和线条:pch、cex、lty、lwd

    颜色:col、col.axis、col.lab、col.main、col.sub、fg、bg

    文本属性:cex、cex.axis、cex.lab、cex.main、cex.sub、font、font.axis、font.lab、font.main、font.sub

    文本添加、坐标轴的自定义和图例

    title()、main、sub、xlab、ylab、text()

    axis()、abline()

    legend()

    多图绘制时候,可使用par()设置默认的图形参数

    par(lwd=2, cex=1.5)

    图形参数设置:

    par(optionname=value,…)

    par(pin=c(width,height)) 图形尺寸

    par(mfrow=c(nr,nc)) 图形组合,一页多图

    layout(mat) 图形组合,一页多图

    par(mar=c(bottom,left,top,right)) 边界尺寸

    par(fig=c(x1,x2,y1,y2),new=TURE) 多图叠加或排布成一幅图

    #图形组合:attach(mtcars)

    opar <- par(no.readonly=TRUE) #复制当前图形参数设置par(mfrow=c(2,2))#设置图形参数#layout(matrix(c(1,2,2,3),2,2,byrow=TRUE))plot(wt,mpg,main="Scatterplot of wt vs mpg")

    hist(wt,main="Histogram of wt")

    boxplot(wt,main="Boxplot of wt")

    par(opar) #返回原始图形参数detach(mtcars)

    图片

    5.3 柱形图

    file <- read.table("barData.csv",header=T,row.names=1,sep=",",stringsAsFactors = F)

    dataxx <- as.matrix(file) #转化为矩阵cols <- terrain.colors(3) #抽取颜色#误差线函数plot.error <- function(x, y, sd, len = 1, col = "black") {

      len <- len * 0.05

        arrows(x0 = x, y0 = y, x1 = x, y1 = y - sd, col = col, angle = 90, length = len)

        arrows(x0 = x, y0 = y, x1 = x, y1 = y + sd, col = col, angle = 90, length = len)

    }

    x <- barplot(dataxx, offset = 0, ylim=c(0, max(dataxx) * 1.1),axis.lty = 1, names.arg = colnames(dataxx), col = cols, beside = TRUE)

    box()

    legend("topright", legend = rownames(dataxx), fill = cols, box.col = "transparent")

    title(main = "An example of barplot", xlab = "Sample", ylab = "Value")

    sd <- dataxx * 0.1 for (i in 1:3) {

      plot.error(x[i, ], dataxx[i, ], sd = sd[i, ])

    }

    图片

    5.4 二元图

    图片

    matdata <- read.table("plot_observed_species.xls", header=T)

    tbl_df(matdata) #查看数据属性和结构y<-matdata[,2:145]

    attach(matdata)

    matplot(series,y,

            ylab="Observed Species Number",xlab="Sequences Number",

            lty=1,lwd=2,type="l",col=1:145,cex.lab=1.2,cex.axis=0.8)

    legend("topleft",lty=1, lwd=2, legend=names(y)[1:8],

          cex=0.5,col=1:145)

    detach(matdata)

    图片

    5.5 饼状图

    relative<-c(0.270617,0.177584,0.194911,0.054685,0.048903,0.033961, 0.031195,0.188143)

    taxon<-c("Sordariales","Pleosporales","Agaricales","Hypocreales",  "Pezizales","Eurotiales","Helotiales","Others")

    ratio<-round(relative*100,2)

    ratio<-paste(ratio,"%",sep="")

    label<-paste(taxon,ratio,sep=" ")

    pie(relative,labels=label, main="ITS1-Sample S1",  radius=1,col=rainbow(length(label)),cex=1.3)

    library(plotrix)

    fan.plot(relative,labels=label,main="Fan plot")

    pie3D(relative,labels=label, height=0.2, theta=pi/4, explode=0.1, col=rainbow(length(label)),  border="black",font=2,radius=1,labelcex=0.9)

    图片

    5.6 直方图

    seqlength<-rnorm(1000, 350, 30)hist(seqlength,breaks=100,

        col="red",freq=FALSE,

        main="Histogram with dengsitycurve",    ylab="Density", xlab="Sequence length")lines(density(seqlength),col="blue4",lwd=2)

    图片

    5.7 聚类图

    clu <- read.table("unweighted_unifrac_dm.txt", header=T, row.names=1, sep="\t")

    head(clu)

    dis <- as.dist(clu)

    h <- hclust(dis, method="average")

    plot(h, hang = 0.1, axes = T, frame.plot = F, main="Cluster Dendrogram based on unweighted_unifrac", sub="UPGMA")

    图片

    5.8 维恩图

    library(VennDiagram)

    ven<-list(sample1=20:50,  sample2=c(1:30,50:80), sample3=40:90, sample4=c(10:30,70:100))

    venn.diagram(ven, filename='venn.png', cex=1.2, col="black", alpha= 0.50,lwd =1.2, cat.cex=1.4,

                  fill=c("cornflowerblue", "green", "Gold1","darkorchid1"),  margin=0.15)

    图片

    图片输出

    直接导出

    图片

    命令

    pdf(file="file.pdf", width=7, height=10)

    png(file="file.png",width=480,height=480)

    jpeg(file="file.png",width=480,height=480)

    tiff(file="file.png",width=480,height=480)

    dev.off()

    相关文章

      网友评论

        本文标题:day7—xiaode

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