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

上传人(卖家):三亚风情 文档编号:2977721 上传时间:2022-06-18 格式:PPT 页数:50 大小:2.38MB
下载 相关 举报
R语言入门和使用技巧课件.ppt_第1页
第1页 / 共50页
R语言入门和使用技巧课件.ppt_第2页
第2页 / 共50页
R语言入门和使用技巧课件.ppt_第3页
第3页 / 共50页
R语言入门和使用技巧课件.ppt_第4页
第4页 / 共50页
R语言入门和使用技巧课件.ppt_第5页
第5页 / 共50页
点击查看更多>>
资源描述

1、 R语言入门和 使用技巧IntroductionParametersGraphic samples ContentsChapter 1. Introduction Introduction u 定义定义:R是用于统计分析、绘图的语言和操作环境。R是属于GNU系统的一个自由、免费、源代码开放的软件,它是一个用于统计计算和统计制图的优秀工具。u 功能功能: R是一套完整的数据处理、计算和制图软件系统。1980年(贝尔实验室) R完善(MathSoft 公司的统计科学部) R系 统( Auckland大学的Robert Gentleman 和 Ross Ihaka )R is free!u 免费,开

2、源,统计模块齐全u 避免了像商业软件在固定的分析过程中存在的问题u 用户可以得知其中的计算会暗含着何种漏洞或错误u 可自由计算任何想计算的统计量(包括图形) Introduction资源资源网站资源:网站资源:R主页:http:/www.r-project.org/统计之都:http:/cos.name/The R Graph Gallery:http:/addictedtor.free.fr/graphiques/Bioconductor:http:/www.bioconductor.org/R Graphical Manua:http:/www.oga- with RR for Begin

3、ners(中文版)Statistics and R Reading Notes(统计学与R读书笔记) R charts 基本运算符号 符号符号 命令或运算提示符命令或运算提示符+ 续行符续行符基本算术运算基本算术运算例子例子+4+5-5-4*5*4/4/545赋值符赋值符例子例子=x=5 xx-5assignassign(“x”,5)求助符求助符例子例子?parhelp()help(par)整除整除 % / % 5%/%3余数余数 % % 5%3 向量数值型&整型&单精度实型&双精度实型逻辑型复值型字符型u向量构建c()没有什么规律seq()seq(from ,to, by,length.ou

4、t)rep()rep(x, .) 数值型向量n例子 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.51 1.0 2.5 4.0 5.5 7.0 8.5 10.0 seq(1, 6, by = 3) # 步长为31 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

5、 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数组 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

6、,4) ,1 ,2 ,3 ,41, 1 4 7 102, 2 5 8 113, 3 6 9 12 t(x) ,1 ,2 ,31, 1 2 32, 4 5 63, 7 8 94, 10 11 12 x = array(1:24,c(3,4,2), , 1 ,1 ,2 ,3 ,41, 1 4 7 102, 2 5 8 113, 3 6 9 12, , 2 ,1 ,2 ,3 ,41, 13 16 19 222, 14 17 20 233, 15 18 21 24矩阵 matrix(1:15, nrow=3,ncol=5,byrow=TRUE) ,1 ,2 ,3 ,4 ,51, 1 2 3 4 52,

7、 6 7 8 9 103, 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 VALUE1 1 422 2 73 3 6444 9 as.data.frame(z.df) INDEX VALUE1 1 422 2 73 3 644 4 9 Xming usage Step 1: install Xming in Window Step 2: clink X

8、ming in Linux Step 3: display pictureXming usageChapter 2. Parameters Draw element par()函数的参数详解 plot()及相关函数的参数说明par plotadj,bg,lendbty,cex,col,font,las,lty, family,lwd, ann,new,mar,mex, mgp,mfool,mfrow, oma,pch,pty,tck,tcl,las,xaxt,yaxt, lab,srt,xpdtype,main,sub,xlab,ylab, xlim, ylim, axes,asp, log,

9、col, pch, cex, lty, lwd, Par charts Par chartsCol: 图中符号(点、线等)的颜色,col.axis 坐标轴刻度标记的颜色col.lab 坐标轴标题的颜色col.main 图主标题的颜色col.sub 图副标题的颜色 Par chartsmfrow,mfcolmar,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

10、,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() 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, ty

11、pe=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,

12、 i-0.5, lnamesi, cex=2) 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 i

13、n 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 图副标题的缩放倍数 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:2

14、4) points(i % 5, i %/% 5, pch=i,cex=2)text(0.3+ i % 5, i %/% 5, i, cex=2) Texttitletitle(main = NULL, sub = NULL, xlab = NULL, ylab = NULL, line = NA, outer = FALSE, .) texttext (x, y = NULL, labels = seq_along(x), adj = NULL, pos = NULL, offset = 0.5, vfont = NULL, cex = 1, col = NULL, font = NULL,

15、 .) mtextmtext(text, side = 3, line = 0, outer = FALSE, at = NA, adj = NA, padj = NA, cex = NA, col = NA, font = NA, .) 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,+ s

16、ide=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 =

17、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(be

18、ta) = (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) legend and gridlegend(x, y = NULL, legend, fill = NULL, col = par(col), lty, lwd, pch, angle = 45, d

19、ensity = 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, pl

20、ot = 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) axisplot(1:7, rnorm(7), main = axis() examples,

21、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) Chapter 3. Graphic samples Graphicspiepie3Dvennrect1line

22、sdensityhistmatplotbarplotboxplotpointslogsegmentsablinearrowspolygon342 Common functions 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, wh

23、ite) 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) 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)

24、 pie3D(a,labels=label,explode=0.1,radius=0.9,border=black)explodeborderlabelsradiusclockwisecolangledensityinit.angle. 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

25、,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) 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,

26、axes=FALSE, ann=FALSE) usr hist(sqrt(islands), breaks = 12, +col=lightblue, border=pink) 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 =

27、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) density charts核密度估计函数已知样本,估计其密度。density(x, bw = nrd0, adjust = 1,kernel = c(gaussian, epanechnikov, rectriangular, biweight, cosinwindow = kernel, width,give.Rkern = FA

28、LSE,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(k

29、ernels), lty = 1, cex = .8, y.intersp = 1,lwd=2) density chartspar(ann=F)m-read.table(Inbred.0.8,head=T)n-read.table(Wild.0.8,head=T)x-m,10m,10 0y-n,10n,10 0plot(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)mtex

30、t(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) barplot chartslibrary(RColorBrewer)par(mfrow = c(2, 2), mar = c(3, 2.5, 0.5, 0.1)death = t(VADeaths), 5:1barplot(death, col = brewer.pal(4, Set1)barplot(death, col = brewer.pal(4, Set1),

31、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) barplot chartsmtread.table(overlap.CF_ANT.Methylation.level.region.relation,head=F);pdf(overlap.CF_ANT.Methylation.level.region.relation.pdf,he

32、ight=8,width=12);x=0:10y=4+8*xpar(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(

33、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); barplot2 chartsci.u = upper boundci.l = lower boundlibrary(gplots) hh - t(VADeaths)1,1mybarcol - gray20ci.l - hh * 0.85ci.u - hh * 1.15barplot2(hh,ci.l = ci.l,

34、ci.u = ci.u,plot.ci = TRUE,col=3,axes=F) 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

35、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

36、Female50-54 11.7 8.7 15.4 8.455-59 18.1 11.7 24.3 13.660-64 26.9 20.3 37.0 19.365-69 41.0 30.9 54.6 35.170-74 66.0 54.3 71.1 50.0 boxplot charts箱线图直接简洁的展示数据分布的特征。boxplot(x, ., range = 1.5, width = NULL, varwidth = FALSE,notch = FALSE, outline = TRUE, names, plot = TRUE,border = par(fg), col = NULL,

37、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

38、 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的距离 heatmap chartlibrary(pheatmap)test = matrix(rnorm(200), 20, 10)test1:10, seq(1, 10, 2) = test1:10, seq(1, 10, 2) + 3test11:20, seq

39、(2, 10, 2) = test11:20, seq(2, 10, 2) + 2colnames(test) = paste(Test, 1:10, sep = )rownames(test) = paste(Gene, 1:20, sep = )pheatmap(test) heatmap chartx-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/

40、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)heatma

41、p.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

42、,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=,lab

43、Col=c(FEM,NFEM,SRM,NSRM)练习题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的位置上。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 办公、行业 > 各类PPT课件(模板)
版权提示 | 免责声明

1,本文(R语言入门和使用技巧课件.ppt)为本站会员(三亚风情)主动上传,163文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。
2,用户下载本文档,所消耗的文币(积分)将全额增加到上传者的账号。
3, 若此文所含内容侵犯了您的版权或隐私,请立即通知163文库(发送邮件至3464097650@qq.com或直接QQ联系客服),我们立即给予删除!


侵权处理QQ:3464097650--上传资料QQ:3464097650

【声明】本站为“文档C2C交易模式”,即用户上传的文档直接卖给(下载)用户,本站只是网络空间服务平台,本站所有原创文档下载所得归上传人所有,如您发现上传作品侵犯了您的版权,请立刻联系我们并提供证据,我们将在3个工作日内予以改正。


163文库-Www.163Wenku.Com |网站地图|