收藏 分销(赏)

R语言入门和使用技巧PPT学习课件.ppt

上传人:a199****6536 文档编号:10036899 上传时间:2025-04-18 格式:PPT 页数:50 大小:2.15MB
下载 相关 举报
R语言入门和使用技巧PPT学习课件.ppt_第1页
第1页 / 共50页
R语言入门和使用技巧PPT学习课件.ppt_第2页
第2页 / 共50页
点击查看更多>>
资源描述
单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,#,R,语言入门和,使用技巧,Lijun Jin,1,Introduction,Parameters,Graphic samples,Contents,2,Chapter 1.Introduction,3,Introduction,R,语言的概述,定义,:,R,是用于统计分析、绘图的语言和操作环境。,R,是属于,GNU,系统的一个自由、免费、源代码开放的软件,它是一个用于统计计算和统计制图的优秀工具。,功能,:,R,是一套完整的数据处理、计算和制图软件系统。,R,语言的发展,1980,年(贝尔实验室),R,完善(,MathSoft,公司的统计科学部),R,系 统(,Auckland,大学的,Robert Gentleman,和,Ross Ihaka,),R is free,!,R,语言的运用,免费,开源,统计模块齐全,避免了像商业软件在固定的分析过程中存在的问题,用户可以得知其中的计算会暗含着何种漏洞或错误,可自由计算任何想计算的统计量(包括图形),4,Introduction,资源,网站资源:,R,主页:,www.r-project.org/,统计之都:,cos.name/,The R Graph Gallery,:,addictedtor.free.fr/graphiques/,Bioconductor,:,www.bioconductor.org/,R Graphical Manua,:,www.oga-with R,R for Beginners(,中文版),Statistics and R Reading Notes(,统计学与,R,读书笔记,),5,R charts,6,基本运算符号,符号,命令或运算提示符,+,续行符,基本算术运算,例子,+4+5,-5-4,*5*4,/4/5,45,赋值符,例子,=x=5,xx-5,assignassign(“x”,5),求助符,例子,?par,help()help(par),整除,%/%5%/%3,余数,%5%3,7,向量,数值型,&,整型,&,单精度实型,&,双精度实型,逻辑型,复值型,字符型,向量构建,c(),没有什么规律,seq()seq(from,to,by,length.out),rep()rep(x,.),8,数值型向量,例子,1:10,1 1 2 3 4 5 6 7 8 9 10,x,seq,(10)#same as 1:10,1 1 2 3 4 5 6 7 8 9 10,seq,(1,10,by,=1.5),#,步长为,1.5,1 1.0 2.5 4.0 5.5 7.0 8.5 10.0,seq,(1,6,by,=3),#,步长为,3,1 1 4,seq,(0,5,length.out,=11),#,生成向量长度为,11,1 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0,rep,(1:10),1 1 2 3 4 5 6 7 8 9 10,rep,(1:10,2),#,整个向量重复,2,次,1 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10,rep,(1:3,each=5,),#,每个元素重复,5,次,1 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3,rep,(1:3,1:3),#,每个元素分别重复,1,、,2,、,3,次,1 1 2 2 3 3 3,rep,(1:4,each=2,len=10,),1 1 1 2 2 3 3 4 4 1 1,9,数组,x=,array,(1:24,3,),1 1 2 3,x=,array,(1:24,3,4,),1 1 2 3,x=,array,(1:24,c(3,4),1,2,3,4,1,1 4 7 10,2,2 5 8 11,3,3 6 9 12,t(x),1,2,3,1,1 2 3,2,4 5 6,3,7 8 9,4,10 11 12,x=,array,(1:24,c(3,4,2,),1,1,2,3,4,1,1 4 7 10,2,2 5 8 11,3,3 6 9 12,2,1,2,3,4,1,13 16 19 22,2,14 17 20 23,3,15 18 21 24,10,矩阵,matrix(1:15,nrow=3,ncol=5,byrow=TRUE),1,2,3,4,5,1,1 2 3 4 5,2,6 7 8 9 10,3,11 12 13 14 15,x,is.matrix(x),1 TRUE,x=array(1:24,c(3,4,2),is.matrix(x),1 FALSE,y x=c(42,7,64,9),y=1:4,z.df=,data.frame,(INDEX=y,VALUE=x),INDEX VALUE,1 1 42,2 2 7,3 3 64,4 9,as.data.frame,(z.df),INDEX VALUE,1 1 42,2 2 7,3 3 64,4 4 9,12,Xming usage,Step 1:install Xming in Window,Step 2:clink Xming in Linux,Step 3:display picture,13,Xming usage,14,Chapter 2.Parameters,15,Draw element,par(),函数的参数详解,plot(),及相关函数的参数说明,par plot,adj,bg,lend,bty,cex,col,font,las,lty,family,lwd,ann,new,mar,mex,mgp,mfool,mfrow,oma,pch,pty,tck,tcl,las,xaxt,yaxt,lab,srt,xpd,type,main,sub,xlab,ylab,xlim,ylim,axes,asp,log,col,pch,cex,lty,lwd,16,Par charts,17,Par charts,Col:,图中符号(点、线等)的颜色,col.axis,坐标轴刻度标记的颜色,col.lab,坐标轴标题的颜色,col.main,图主标题的颜色,col.sub,图副标题的颜色,18,Par charts,mfrow,mfcol,mar,oma,x-c(1:5),par(ann=F,mar=c(4,4,4,6),plot(x,pch=10,col=4,cex=3,axes=F)mtext(side=1,line=0,side=1,line=0,cex=2),mtext(side=2,line=1,side=2,line=1,cex=2),mtext(side=3,line=2,side=3,line=2,cex=2),mtext(side=4,line=3,side=4,line=3,cex=2),box(),19,plot charts,plot(c(10,20),c(1,13),col=white,xlab=,ylab=,main=type=,xaxt=n,yaxt=n,cex.main=2.5),ltypes=c(l,p,b,c,o,s,S,h),lpos=c(seq(0,10,2),13,15),for(i in seq_along(ltypes)lines(lposi+1:20,1:20,type=ltypesi,lwd=3),text(9.5+1:8,12-1:8,c(l,p,b,c,o,s,S,h),cex=2,col=red),plot(c(10,20),c(0,10),col=white,xlab=,ylab=,main=lty=,xaxt=n,ylim=c(0,9.6),cex.main=2.5),lnames=c(blank,solid,dashed,dotted,dotdash,longdash,twodash,51,9396,848481),for(i in 1:10)abline(h=i-1,lty=lnamesi,lwd=3)text(15,i-0.5,lnamesi,cex=2),20,plot charts,plot(c(0.5,1.5),c(0,3),col=white,xlab=,ylab=,main=cex=,xaxt=n,yaxt=n,cex.main=2.5),for(i in seq(0,3,0.2)points(0,i,pch=16,cex=i)text(1,i,paste(i),cex=i),plot(c(10,20),c(23,25.5),col=white,xlab=,ylab=,main=lwd=,xaxt=n,log=y,cex.main=2.5,yaxp=c(0.1,50,2),for(i in 1:9)lines(c(11,19),c(2(i-4),2(i-4),lwd=2(i-4)text(15,2(i-4+0.5),2(i-4),cex=2),Cex:,图上元素(文本和符号等)的缩放倍数;取值为一个相对于,1,的数值,cex.axis,坐标轴刻度标记的缩放倍数,cex.lab,坐标轴标题的缩放倍数,cex.main,图主标题的缩放倍数,cex.sub,图副标题的缩放倍数,21,plot,charts,plot(c(0,4.5),c(0,4),col=white,xlab=,ylab=,main=pch=,xaxt=n,yaxt=n,cex.main=2.5),for(i in 0:24)points(i%5,i%/%5,pch=i,cex=2)text(0.3+i%5,i%/%5,i,cex=2),22,Text,title,title(main=NULL,sub=NULL,xlab=NULL,ylab=NULL,line=NA,outer=FALSE,.),text,text(x,y=NULL,labels=seq_along(x),adj=NULL,pos=NULL,offset=0.5,vfont=NULL,cex=1,col=NULL,font=NULL,.),mtext,mtext(text,side=3,line=0,outer=FALSE,at=NA,adj=NA,padj=NA,cex=NA,col=NA,font=NA,.),23,Text,plot(1:10,(-4:5)2,main=Parabola Points,xlab=xlab),mtext(10 of them),for(s in 1:4),+mtext(paste(mtext(.,line=-1,side,col,font=,s,+,cex=,(1+s)/2,),line=-1,+side=s,col=s,font=s,cex=(1+s)/2),mtext(mtext(.,line=-2),line=-2),mtext(mtext(.,line=-2,adj=0),line=-2,adj=0),plot(-1:1,-1:1,type=n,xlab=Re,ylab=Im),K-16;text(exp(1i*2*pi*(1:K)/K),col=2,cex=2),par(ann=F,new=T),plot(1:10,1:10,main=text(.)examplesn,+sub=R is GNU,but not .),mtext(Latin-1 accented chars:,side=3),points(c(6,2),c(2,1),pch=3,cex=5,col=green),text(6,2,the text is CENTERED around(x,y)=(6,2)by default,col=4,cex=.8),text(2,1,or Left/Bottom-JUSTIFIED at(2,1)by adj=c(0,0),+adj=c(0,0),text(4,9,expression(hat(beta)=(Xt*X)-1*Xt*y),col=8),text(4,8.4,expression(hat(beta)=(Xt*X)-1*Xt*y),col=11,cex=2),text(4,7,expression(bar(x)=sum(frac(xi,n),i=1,n),col=12,cex=2),title(text(.)examplesn),24,legend and grid,legend(x,y=NULL,legend,fill=NULL,col=par(col),lty,lwd,pch,angle=45,density=NULL,bty=o,bg=par(bg),box.lwd=par(lwd),box.lty=par(lty),box.col=par(fg),pt.bg=NA,cex=1,pt.cex=cex,pt.lwd=lwd,xjust=0,yjust=1,x.intersp=1,y.intersp=1,adj=c(0,0.5),text.width=NULL,text.col=par(col),merge=do.lines&has.pch,trace=FALSE,plot=TRUE,ncol=1,horiz=FALSE,title=NULL,inset=0,xpd,title.col=text.col),plot(1:3),grid(NA,5,lwd=4,col=3,lty=6),grid(5,NA,lwd=4,col=3,lty=6),grid(nx=NULL,ny=nx,col=lightgray,lty=dotted,lwd=par(lwd),equilogs=TRUE),25,axis,plot,(1:7,rnorm(7),main=axis()examples,type=s,xaxt=n,frame=FALSE,col=red),axis,(1,1:7,LETTERS1:7,col.axis=blue)#unusual options:,axis,(4,col=violet,col.axis=dark violet,lwd=2),axis,(3,col=gold,lty=2,lwd=0.5),plot(1:10,xaxt=n),axis(1,xaxp=c(2,9,7),26,Chapter 3.Graphic samples,27,Graphics,pie,pie3D,venn,rect,1,lines,density,hist,matplot,barplot,boxplot,points,log,segments,abline,arrows,polygon,3,4,2,Common functions,28,Pie charts,pie.sales-c(0.12,0.3,0.26,0.16,0.04,0.12),names(pie.sales)-c(Blueberry,Cherry,Apple,Boston Cream,Other,Vanilla Cream),pie(pie.sales,col=c(purple,violetred1,green3,cornsilk,cyan,white),a-c(58.13,21.64,20.24),ratio-sprintf(%.2f,100*a/sum(a),label=paste(ratio,%,sep=),pie(a,col=c(red,blue,green),labels=label),legend(topright,c(mCG,mCHG,mCHH),col=c(2,4,green),pch=15,bty=n),29,Pie3D charts,library(plotrix),a-c(58.13,21.64,20.24),c-paste(a,%,sep=),label=paste(c(mCG,mCHG,mCHH),c,sep=n),pie3D(a,labels=label,explode=0.1,radius=0.9,border=black),explode,border,labels,radius,clockwise,col,angle,density,init.angle,.,30,Venn charts,install.packages(plotrix),library(plotrix),par(ann=F),plot(0:10,seq(0,10,length=11),type=n,axes=F),draw.circle(2,5,2),draw.circle(4,5,2),text(1,5,labels=10.12%,col=black,font=2),text(3,5,labels=49.5%,col=black,font=2),text(5,5,labels=40.38%,col=black,font=2),text(2,2,Sample1),text(5,2,Samlpe2),text(3.5,8.5,labels=Venn picture“,font=2,cex=1.5),31,axis charts,x-c(0.00,0.40,0.86,0.85,0.69,0.48,0.54,1.09,1.11,1.73,2.05,2.02),par(bg=lightgray),plot(x,type=n,axes=FALSE,ann=FALSE),usr hist(sqrt(islands),breaks=12,+col=lightblue,border=pink),36,hist,charts,w-c(75.0,64.0,47.4,66.9,62.2,62.2,58.7,63.5,66.6,64.0,57.0,69.0,56.9,50.0,72.0),par(mfrow=c(2,2),ann=F),hist(w,freq=F,col=blue,main=freq=F),hist(w,freq=T,col=green,main=freq=T),hist(w,freq=T,col=red,main=breaks=3,density=10,breaks=3,density=10),hist(w,freq=T,col=red,main=breaks=20,breaks=20),37,density charts,核密度估计函数,已知样本,估计其密度。,density,(x,bw=nrd0,adjust=1,kernel=c(gaussian,epanechnikov,rec,triangular,biweight,cosinwindow=kernel,width,give.Rkern=FALSE,n=512,from,to,cut=3,na.rm=FALSE),例子:,(kernels-eval(formals(density.default)$kernel),plot(density(0,bw=1),xlab=,main=Rs density()kernels with bw=1),for(i in 2:length(kernels),lines(density(0,bw=1,kernel=kernelsi),col=i,lwd=2),legend(topright,legend=kernels,col=seq(kernels),lty=1,cex=.8,y.intersp=1,lwd=2),38,density charts,par(ann=F),m-read.table(Inbred.0.8,head=T),n-read.table(Wild.0.8,head=T),x-m,10m,10 0,y-n,10n,10 0,plot(density(x,bw=0.02),col=2,axes=F),par(ann=F,new=T),plot(density(y,bw=0.02),col=3),mtext(Methylation level of gene,side=1,line=3,cex=1.3),mtext(Density,side=2,line=3,cex=1.3),legend(topright,lwd=c(2,2),cex=1,col=c(2:3),legend=c(Inbred,Wild),title(Oyster),39,barplot charts,library(RColorBrewer),par(mfrow=c(2,2),mar=c(3,2.5,0.5,0.1),death=t(VADeaths),5:1,barplot(death,col=brewer.pal(4,Set1),barplot(death,col=brewer.pal(4,Set1),beside=TRUE,legend=TRUE),x-c(1,2,-3,4,-9,10,-1,2,0,-8),r-barplot(x,col=rainbow(20),tN-c(6,6,19,16,17,14,8,8,3,3),r-barplot(tN,col=rainbow(20),horiz=T),40,barplot charts,mtread.table(overlap.CF_ANT.Methylation.level.region.relation,head=F);,pdf(overlap.CF_ANT.Methylation.level.region.relation.pdf,height=8,width=12);,x=0:10,y=4+8*x,par(bty=7,mar=c(9,8,2,0.5),mgp=c(5,1,0);,n-data.frame(mt,c(2:8),barplot(t(as.matrix(n),col=c(2:8),axes=F,ylim=c(0,0.12),width=1,beside=T,ylab=Relative methylaion level,cex.lab=2);,axis(1,at=y,lab=F),text(y-1,-0.017,labels=mt,1,srt=45,xpd=T,cex=1.7,font.lab=2,cex.lab=2),axis(2,las=1,cex.axis=1.8,font.axis=2),legend(topright,legend=c(CF_Egg,CF_larva,CF_Major,CF_Minor,CF_male,CF_Q,CF_VQ),pch=15,col=c(2:8),cex=1.5);,41,barplot2,charts,ci.u=upper bound,ci.l=lower bound,library(gplots),hh-t(VADeaths)1,1,mybarcol-gray20,ci.l-hh*0.85,ci.u-hh*1.15,barplot2(hh,ci.l=ci.l,ci.u=ci.u,plot.ci=TRUE,col=3,axes=F),42,barplot2,charts,library(gplots),hh-t(VADeaths),5:1,mybarcol-gray20,ci.l-hh*0.85,ci.u-hh*1.15,mp-barplot2(hh,beside=TRUE,col=c(lightblue,mistyrose,lightcyan,lavender),legend=colnames(VADeaths),ylim=c(0,100),main=Death Rates in Virginia,font.main=4,sub=Faked 95 percent error bars,col.sub=mybarcol,cex.names=1.5,plot.ci=TRUE,ci.l=ci.l,ci.u=ci.u,plot.grid=TRUE),mtext(side=1,at=colMeans(mp),line=2,text=paste(Mean,formatC(colMeans(hh),col=red),box(),Rural Male Rural Female Urban Male Urban Female,50-54 11.7 8.7 15.4 8.4,55-59 18.1 11.7 24.3 13.6,60-64 26.9 20.3 37.0 19.3,65-69 41.0 30.9 54.6 35.1,70-74 66.0 54.3 71.1 50.0,43,boxplot charts,箱线图,直接简洁的展示数据分布的特征。,boxplot,(,x,.,range=1.5,width=NULL,varwidth=FALSE,notch=FALSE,outline=TRUE,names,plot=TRUE,border=par(fg),col=NULL,log=,pars=list(boxwex=0.8,staplewex=0.5,outwex=0.5),horizontal=FALSE,add=FALSE,at=NULL),例子:,Notch:,凹槽所表示的实际上是中位数的一个区间估计,.,计算式:,Q2+/1.58IQR/,区间置信水平,:95%,在比较两组数据中位数差异时,我们只需要观察箱线图的凹槽是否有重叠部分,若两个凹槽互不交叠,那么说明这两组数据的中位数有显著差异(,P,值小于,0.05).,x x-c(1,2,6,8,11);dim(x)-c(5,1);d hc1-hclust(d,single);hc2 hc3-hclust(d,median);hc4 opar plot(hc1,hang=-1);plot(hc2,hang=-1),plot(hc3,hang=-1);plot(hc4,hang=-1),par(opar),d-dist(x),Dij,表示第,i,个样本与第,j,个样本的距离,,G1,G2.,表示类,,DKL,表示,GK,与,GL,的距离,46,heatmap chart,library(pheatmap),test=matrix(rnorm(200),20,10),test1:10,seq(1,10,2)=test1:10,seq(1,10,2)+3,test11:20,seq(2,10,2)=test11:20,seq(2,10,2)+2,colnames(test)=paste(Test,1:10,sep=),rownames(test)=paste(Gene,1:20,sep=),pheatmap(test),47,heatmap chart,x-read.table(D:csem.met.txt,head=T),y-data.frame(x,4,x,3,x,2,x,1),y-data.frame(x,4*100,x,3*100,x,2*100,x,1*100),y-data.frame(0.2+1/log10(x,4+2),0.2+1/log10(x,3+2),0.2+1/log10(x,2+2),0.2+1/log10(x,1+2),z0.4),col=cm.colors,trace=none,density.info=none,labRow=,labCol=c(FEM,NFEM,SRM,NSRM),heatmap.2(z,xlab=,ylab=,na.rm=T,revC=T,keysize=2,symkey=min(x 0.4),col=terrain.colors,trace=none,density.info=none,Rowv=NA,labRow=,labCol=c(FEM,NFEM,SRM,NSRM),heatmap.2(z,xlab=,ylab=,na.rm=T,revC=T,keysize=2,symkey=min(x 0.4),col=topo.colors,trace=none,density.info=none,Rowv=NA,labRow=,labCol=c(FEM,NFEM,SRM,NSRM),heatmap.2(z,xlab=,ylab=,na.rm=T,revC=T,keysize=1.8,symkey=min(x 0.4),col=rainbow,trace=none,density.info=none,Rowv=NA,labRow=,labCol=c(FEM,NFEM,SRM,NSRM),heatmap.2(z,xlab=,ylab=,na.rm=T,revC=T,keysize=1.8,symkey=min(x 0.4),col=heat.colors,trace=none,density.info=none,Rowv=NA,labRow=,labCol=c(FEM,NFEM,SRM,NSRM),heatmap.2(z,xlab=,ylab=,na.rm=T,revC=T,keysize=1.8,symkey=min(x 0.4),col=gray.colors,trace=none,density.info=none,Rowv=NA,labRow=,labCol=c(FEM,NFEM,SRM,NSRM),48,练习题,1./ifs5/PC_PA_UN/ANIMAL/USER/GROUP2/jinlijun/Rcourse/barplot.txt,将这个文件的数据可视化。(,barplot,),2./ifs5/PC_PA_UN/ANIMAL/USER/GROUP2/jinlijun/Rcourse/pie.txt,将这个文件的数据可视化。(,pie,),3./ifs5/PC_PA_UN/ANIMAL/USER/GROUP2/jinlijun/Rcourse/join,将这个目录下的,5,个,.stat,文件的数据画在一张图上。(,plot,),要求图形效果跟,/ifs5/PC_PA_UN/ANIMAL/USER/GROUP2/jinlijun/Rcourse/join/rpkm_bin.CG.RML.pdf,差不多,,颜色可以有差别,其中两条灰色的垂直线分别在坐标,21,和,40,的位置上。,49,50,
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服