




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、 R語(yǔ)言入門和 使用技巧IntroductionParametersGraphic samples ContentsChapter 1. Introduction Introduction u 定義定義:R是用于統(tǒng)計(jì)分析、繪圖的語(yǔ)言和操作環(huán)境。R是屬于GNU系統(tǒng)的一個(gè)自由、免費(fèi)、源代碼開(kāi)放的軟件,它是一個(gè)用于統(tǒng)計(jì)計(jì)算和統(tǒng)計(jì)制圖的優(yōu)秀工具。u 功能功能: R是一套完整的數(shù)據(jù)處理、計(jì)算和制圖軟件系統(tǒng)。1980年(貝爾實(shí)驗(yàn)室) R完善(MathSoft 公司的統(tǒng)計(jì)科學(xué)部) R系 統(tǒng)( Auckland大學(xué)的Robert Gentleman 和 Ross Ihaka )R is free!u 免費(fèi),開(kāi)
2、源,統(tǒng)計(jì)模塊齊全u 避免了像商業(yè)軟件在固定的分析過(guò)程中存在的問(wèn)題u 用戶可以得知其中的計(jì)算會(huì)暗含著何種漏洞或錯(cuò)誤u 可自由計(jì)算任何想計(jì)算的統(tǒng)計(jì)量(包括圖形) Introduction資源資源網(wǎng)站資源:網(wǎng)站資源:R主頁(yè):/統(tǒng)計(jì)之都:/The R Graph Gallery:http:/addictedtor.free.fr/graphiques/Bioconductor:/R Graphical Manua:http:/www.oga- with RR for Begin
3、ners(中文版)Statistics and R Reading Notes(統(tǒng)計(jì)學(xué)與R讀書筆記) R charts 基本運(yùn)算符號(hào) 符號(hào)符號(hào) 命令或運(yùn)算提示符命令或運(yùn)算提示符+ 續(xù)行符續(xù)行符基本算術(shù)運(yùn)算基本算術(shù)運(yùn)算例子例子+4+5-5-4*5*4/4/545賦值符賦值符例子例子=x=5 xx-5assignassign(“x”,5)求助符求助符例子例子?parhelp()help(par)整除整除 % / % 5%/%3余數(shù)余數(shù) % % 5%3 向量數(shù)值型&整型&單精度實(shí)型&雙精度實(shí)型邏輯型復(fù)值型字符型u向量構(gòu)建c()沒(méi)有什么規(guī)律seq()seq(from ,to,
4、by,length.out)rep()rep(x, .) 數(shù)值型向量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) # 步長(zhǎng)為1.51 1.0 2.5 4.0 5.5 7.0 8.5 10.0 seq(1, 6, by = 3) # 步長(zhǎng)為31 1 4 seq(0, 5, length.out=11) # 生成向量長(zhǎng)度為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
5、 4 5 6 7 8 9 10 rep(1:10,2) # 整個(gè)向量重復(fù)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) # 每個(gè)元素重復(fù)5次 1 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 rep(1:3, 1:3) # 每個(gè)元素分別重復(fù)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數(shù)組 x = array(1:24,3)1 1 2 3 x = array(1:24,3,4)1 1 2 3 x = ar
6、ray(1:24,c(3,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,
7、 1 2 3 4 52, 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 Ste
8、p 2: clink Xming in Linux Step 3: display pictureXming usageChapter 2. Parameters Draw element par()函數(shù)的參數(shù)詳解 plot()及相關(guān)函數(shù)的參數(shù)說(shuō)明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, ax
9、es,asp, log,col, pch, cex, lty, lwd, Par charts Par chartsCol: 圖中符號(hào)(點(diǎn)、線等)的顏色,col.axis 坐標(biāo)軸刻度標(biāo)記的顏色col.lab 坐標(biāo)軸標(biāo)題的顏色col.main 圖主標(biāo)題的顏色col.sub 圖副標(biāo)題的顏色 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,s
10、ide=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() 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
11、: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,lw
12、d=3)text(15, 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,5
13、0,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: 圖上元素(文本和符號(hào)等)的縮放倍數(shù);取值為一個(gè)相對(duì)于1的數(shù)值cex.axis 坐標(biāo)軸刻度標(biāo)記的縮放倍數(shù)cex.lab 坐標(biāo)軸標(biāo)題的縮放倍數(shù)cex.main 圖主標(biāo)題的縮放倍數(shù)cex.sub 圖副標(biāo)題的縮放倍數(shù) plot charts plot(c(0,4.5),c(0,4),col=white,xlab=,ylab=,main=pch=,xaxt=n,yaxt=n,cex.main=2.5) f
14、or (i in 0:24) 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,
15、font = NULL, .) 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, ), l
16、ine = -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(.) exampl
17、esn,+ 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, expre
18、ssion(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) legend and gridlegend(x, y = NULL, legend, fill = NULL, col = par(col), lty, lwd, pch, a
19、ngle = 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, ersp = 1, ersp = 1, adj = c(0, 0.5), text.width = NULL, text.col = par(col), merge = do.lines & has.pch, t
20、race = 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) axisplot(1:7, rnorm(7), main = a
21、xis() 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) Chapter 3. Graphic samples Graphicspiepi
22、e3Dvennrect1linesdensityhistmatplotbarplotboxplotpointslogsegmentsablinearrowspolygon342 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, co
23、rnsilk, 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) Pie3D charts library(plotrix) a-c(58.13,21.64,20.24) c-paste(a,%,sep=) label=paste(c(mCG,mCH
24、G,mCHH),c,sep=n) 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=
25、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) 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)
26、plot(x, type=n, 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=gr
27、een,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) density charts核密度估計(jì)函數(shù)已知樣本,估計(jì)其密度。density(x, bw = nrd0, adjust = 1,kernel = c(gaussian, epanechnikov, rectriangular, biweight, cosinwindow = kernel, width
28、,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 = kern
29、els, col = seq(kernels), lty = 1, cex = .8, ersp = 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, lin
30、e=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) 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 = brewe
31、r.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) barplot chartsmtread.table(overlap.CF_ANT.Methylation.level.region.relation,head=F);pdf(overlap.CF_ANT.Methylation.level.region
32、.relation.pdf,height=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
33、,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); barplot2 chartsci.u = upper boundci.l = lower boundlibrary(gplots) hh - t(VADeaths)1,1mybarcol - gray20ci.l - hh * 0.85ci.u - hh * 1.15barplot2(
34、hh,ci.l = ci.l, 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 =
35、 Death Rates in Virginia, font.main = 4,sub = Faked 95 percent error bars, col.sub = mybarcol, s = 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 U
36、rban Male Urban 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箱線圖直接簡(jiǎn)潔的展示數(shù)據(jù)分布的特征。boxplot(x, ., range = 1.5, width = NULL, varwidth = FALSE,notch = FALSE, outline = TRUE, names, plot = TRUE,border = par(f
37、g), col = NULL, log = ,pars = list(boxwex = 0.8, staplewex = 0.5, outwex = 0.5),horizontal = FALSE, add = FALSE, at = NULL)例子:Notch:凹槽所表示的實(shí)際上是中位數(shù)的一個(gè)區(qū)間估計(jì).計(jì)算式:Q2 +/1.58IQR/區(qū)間置信水平:95%在比較兩組數(shù)據(jù)中位數(shù)差異時(shí),我們只需要觀察箱線圖的凹槽是否有重疊部分,若兩個(gè)凹槽互不交疊,那么說(shuō)明這兩組數(shù)據(jù)的中位數(shù)有顯著差異(P值小于0.05).x x-c(1,2,6,8,11); dim(x)-c(5,1); d hc1-hclust
38、(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個(gè)樣本與第j個(gè)樣本的距離,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) +
39、 3test11:20, seq(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/log10(
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年安徽國(guó)控資產(chǎn)管理有限公司第二季度社會(huì)招聘5人筆試參考題庫(kù)附帶答案詳解
- 寧夏職業(yè)技術(shù)學(xué)院《馬克思主義哲學(xué)原理》2023-2024學(xué)年第二學(xué)期期末試卷
- 西南林業(yè)大學(xué)《建筑消防設(shè)備工程》2023-2024學(xué)年第二學(xué)期期末試卷
- 燕山大學(xué)《機(jī)器人學(xué)導(dǎo)論》2023-2024學(xué)年第二學(xué)期期末試卷
- 沈陽(yáng)化工大學(xué)《語(yǔ)言處理與語(yǔ)音識(shí)別》2023-2024學(xué)年第二學(xué)期期末試卷
- 曲靖醫(yī)學(xué)高等??茖W(xué)?!睹窈铰每瓦\(yùn)輸》2023-2024學(xué)年第二學(xué)期期末試卷
- 安徽警官職業(yè)學(xué)院《應(yīng)用水文氣象學(xué)》2023-2024學(xué)年第二學(xué)期期末試卷
- 重慶建筑工程職業(yè)學(xué)院《計(jì)算機(jī)基礎(chǔ)B實(shí)驗(yàn)》2023-2024學(xué)年第二學(xué)期期末試卷
- 開(kāi)封職業(yè)學(xué)院《A芯片應(yīng)用開(kāi)發(fā)實(shí)踐》2023-2024學(xué)年第二學(xué)期期末試卷
- 漳州理工職業(yè)學(xué)院《鐵路行車織》2023-2024學(xué)年第二學(xué)期期末試卷
- 臺(tái)區(qū)線損綜合分析臺(tái)區(qū)線損分類及計(jì)算方法
- 城市園林綠化養(yǎng)護(hù)方案
- 人民幣收藏培訓(xùn)知識(shí)
- 籍貫對(duì)照表完整版
- 中興基站設(shè)備故障處理指導(dǎo)書
- 公路工程地質(zhì)試卷A
- 渤海大學(xué)在線自助繳費(fèi)平臺(tái)操作流程
- 2023年山東省大學(xué)生朋輩心理輔導(dǎo)技能大賽筆試題庫(kù)
- 聯(lián)合利華POSM展策劃案
- 中國(guó)石油成品油銷售業(yè)務(wù)介紹
- 13-3飛速發(fā)展的通信世界 教案
評(píng)論
0/150
提交評(píng)論