当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

中国科学技术大学:《实用统计软件》课程课件讲义(统计计算与软件)第二讲 R语言基础(二)

资源类别:文库,文档格式:PDF,文档页数:53,文件大小:543.99KB,团购合买
1 Graphics with R 1.1 Managing graphics 1.1.1 Graphical Functions 1.1.2 Low-level plotting commands 1.1.3 Graphical Parameters 2 Statistical Analysis with R 2.1 Formulae 2.2 Generic Functions 2.3 Packages 3 Programming with R 3.1 Flow Control 3.2 Functions 3.3 Miscellaneous programming tips 3.4 Debugging 3.5 Efficient programming 3.6 R script editors
点击下载完整版文档(PDF)

Lecture2:R基础(二) 张伟平 Monday 7th September,2009 Previous Next First Last Back Forward

Lecture 2: R ƒ:() ‹ï² Monday 7th September, 2009 Previous Next First Last Back Forward a

Contents 1 Graphics with R 1.1 Managing graphics。.....··。·.·。·.·。····· 2 1.1.1 Graphical Functions.................. 5 1.1.2Low-level plotting commands......·.····· 11 l.l.3 Graphical Parameters··....·.···.·.···14 2 Statistical Analysis with R 23 2.1 Formulae...······· 26 2.2 Generic Functions.... 444 28 2.3.Packages..。。····· 4 31 3 Programming with R 36 3.1 Flow Control·.······· .·..。。..36 3.2 Functions········· 4.444 40 3.3 Miscellaneous programming tips··.······ 42 3.4 Debugging.······················· 42 3.5 Efficient programming。。。···.·..·········· 46 3.6 R script editors..........·..。.。。。··· 51 Previous Next First Last Back Forward

Contents 1 Graphics with R 1 1.1 Managing graphics . . . . . . . . . . . . . . . . . . . . . . . 1 1.1.1 Graphical Functions . . . . . . . . . . . . . . . . . . 5 1.1.2 Low-level plotting commands . . . . . . . . . . . . . 11 1.1.3 Graphical Parameters . . . . . . . . . . . . . . . . . 14 2 Statistical Analysis with R 23 2.1 Formulae . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 2.2 Generic Functions . . . . . . . . . . . . . . . . . . . . . . . 28 2.3 Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 3 Programming with R 36 3.1 Flow Control . . . . . . . . . . . . . . . . . . . . . . . . . . 36 3.2 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 3.3 Miscellaneous programming tips . . . . . . . . . . . . . . . 42 3.4 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 3.5 Efficient programming . . . . . . . . . . . . . . . . . . . . . 46 3.6 R script editors . . . . . . . . . . . . . . . . . . . . . . . . 51 Previous Next First Last Back Forward 1

Chapter 1 Graphics with R 绘图函数的工作方式与前面描述的工作方式大为不同,不能把绘图函数的结 果赋给一个对象,其结果直接输出到一个“绘图设备”上.绘图设备是一个绘 图的窗口或是一个文件.有两种绘图函数:高级绘图函数(high-level plotting functions)创建一个新的图形,低级绘图函数(low-level plotting functions)在 现存的图形上添加元素.绘图参数(graphical parameters)控制绘图选项,可以 使用缺省值或者用函数par修改, 1.1 Managing graphics 当绘图函数开始执行,如果没有打开绘图设备,那么R将打开一个绘图窗口来 展示这个图形.可用的绘图设备种类取决于操作系统.在Unix/Linux下,绘图 窗口称为x11,而在Windows下称为windows.但是绘图窗口都可以用xl1(0) 命令打开,windows下还可以用windows()命令打开. Previous Next First Last Back Forward 1

Chapter 1 Graphics with R ±„ºÍÛäê™Üc°£„Ûäê™åèÿ”, ÿUr±„ºÍ( JDâòáÈñ, Ÿ(JÜ——òá/±„0˛. ±„¥òá± „Iù½¥òá©á. k¸´±„ºÍ: p?±„ºÍ(high-level plotting functions)MÔòá#„/, $?±„ºÍ(low-level plotting functions)3 y„/˛V\É. ±„ÎÍ(graphical parameters)õõ±„¿ë, å± ¶^"é佈^ºÍpar?U. 1.1 Managing graphics ±„ºÍm©â1, XJvkãm±„, @oR Úãmò᱄Iù5 –´˘á„/. å^±„´a˚uˆäX⁄. 3Unix/Linux e, ±„ Iù°èx11, 3Windows e°èwindows. ¥±„Iù—å±^x11() ·-ãm, windows eÑå±^windows()·-ãm. Previous Next First Last Back Forward 1

1.GRAPHICS WITH R 可以用函数打开一个文件作为绘图设备,这包括:postscript(O,pdf), png(),jpeg(O,,可用的绘图设备列表可以用?device来察看.最后打开的设备 将成为当前的绘图设备,随后的所有图形都在这上面显示.函数dev.list(O显示 打开的列表。 Example >x11();x11(0;pdf(0 >dev.off(2)#关闭设备2 dev.list() X11 X11 X11 pdf 0 234 >dav.off()#关闭当前设备 >dav.cur()#查看当前设备 pdf pdf 4 4 >graphics.off()#关闭所有绘 图设备 >dev.set(3)#设置设备3为当前设备 X11 3 Previous Next First Last Back Forward 2

å±^ºÍãmòá©áä豄, ˘ù)µpostscript(), pdf(), png(), jpeg(),..., å^±„Lå±^?device5 w. Å￾ãm Ú§èc±„, ë￾§k„/—3˘˛°w´. ºÍdev.list() w´ ãmL. ↑Example > x11(); x11(); pdf() > dev.off(2) #'42 > dev.list() X11 X11 X11 pdf 3 2 3 4 > dev.off() #'4c > dev.cur()#wc pdf pdf 4 4 > graphics.off() # '4§k± „ > dev.set(3)#ò3èc X11 3 1. GRAPHICS WITH R Previous Next First Last Back Forward 2

1.GRAPHICS WITH R Example 当要把多个绘图绘制在同一个绘图设备上时,可以使用如下方法 7使用函数split.screen方法,可以单独控制每个分割出的screen. split.screen(figs,screen,erase TRUE) Code screen(n =new TRUE) erase.screen(n = close.screen(n,all.screens FALSE) Code 例如 Example spl1t.screen(c(2,1)#分割显示屏幕为2个,格式为2行1列 [1]12 #分割出的屏幕编号 >split.screen(c(1,3),screen=2)#分割第2个显示屏为3个,1行3列 [1]345 screen(1) #使用屏幕1 Previous Next First Last Back Forward 3

↓Example árı᱄±õ3”ò᱄˛û, 屶^Xeê{: ∇ ¶^ºÍsplit.screen ê{, 屸’õõzá©—screen. ↑Code split.screen(figs, screen, erase = TRUE) screen(n = , new = TRUE) erase.screen(n = ) close.screen(n, all.screens = FALSE) ↓Code ~X ↑Example split.screen(c(2,1)) #©w´¶4è2á,Ç™è211 [1] 1 2 #©—¶4?“ > split.screen(c(1,3), screen = 2) # ©12áw´¶è3á, 113 [1] 3 4 5 > screen(1) #¶^¶41 1. GRAPHICS WITH R Previous Next First Last Back Forward 3

1.GRAPHICS WITH R >p1ot(10:1) #绘制图形 >screen(4) #使用屏幕4 >p1ot(10:1) #绘制图形 >close.screen(a11=TRUE)#关闭如上屏幕分割定义 Example 7使用函数1yout方法,把当前的图形窗口分割为多个部份,图形将一次 显示在各部分中。 layout(mat,widths rep(1,ncol(mat)), FCode heights rep(1,nrow(mat)),respect FALSE) layout.show(n 1) Code 例如 TExample def.par<-par(no.readonly=TRUE)#存储当前图形设备设置 layout(matrix(c(1,1,0,2),2,2,byrow TRUE)) Previous Next First Last Back Forward 4

> plot(10:1) #±õ„/ > screen(4) #¶^¶44 > plot(10:1) #±õ„/ > close.screen(all = TRUE) #'4X˛¶4©½¬ ↓Example ∇ ¶^ºÍlayout ê{, rc„/Iù©èıá‹°, „/Úòg w´3à‹©•. ↑Code layout(mat, widths = rep(1, ncol(mat)), heights = rep(1, nrow(mat)), respect = FALSE) layout.show(n = 1) ↓Code ~X ↑Example def.par <- par(no.readonly = TRUE) # ;c„/ò layout(matrix(c(1,1,0,2), 2, 2, byrow = TRUE)) 1. GRAPHICS WITH R Previous Next First Last Back Forward 4

1.GRAPHICS WITH R #11g1占第一行,f1g2占第二行的第二列 1 ayout.show(2)#显示每个f1g的区域 p1ot(1:10) p1ot(10:1) par(def.par)#恢复默认设置 上xample 7使用函数par中的参数nfrow/mfcol方法函数parO中的参数mfrow 或者mfcol可以用来指定将当前图形窗口分割为几行几列. par(mfrow=c(nr,nc)#将图形窗口分为nr行nc列 ¥cod0 Code 这种方法都是平均分割,不能像]ayout那样可以控制每个区域的大小 1.1.1 Graphical Functions 下面是R中高级绘图函数的概括: Previous Next First Last Back Forward 5

#fig1”1ò1,fig2”111 layout.show(2) # w´záfig´ç plot(1:10) plot(10:1) par(def.par) #°E%@ò ↓Example ∇ ¶^ºÍpar •ÎÍmfrow/mfcol ê{ºÍpar() •ÎÍmfrow ½ˆmfcol å±^5ç½Úc„/Iù©èA1A. ↑Code par(mfrow=c(nr,nc)) # Ú„/Iù©ènr1nc ↓Code ˘´ê{—¥²˛©,ÿUîlayout@å±õõzá´çå. 1.1.1 Graphical Functions e°¥R•p?±„ºÍV): 1. GRAPHICS WITH R Previous Next First Last Back Forward 5

1.GRAPHICS WITH R TCode plot(x) 以x的元素值为纵坐标、以序号为横坐标绘图 plot(x,y) x(在x-轴上)与y(在y-轴上)的二元作图 pie(x) 饼图 boxplot(x) 盒形图(box-and-whiskers) coplot(x"ylz) 关于z的每个数值(或数值区间)绘制x与y的 二元图 matplot(x,y) 二元图,其中x的第一列对应y的第一列,x的 第二列对应y的第二列,依次类推, dotchart(x) 如果x是数据框,作c1 leveland点图(逐行逐列 累加图) Previous Next First Last Back Forward 6

↑Code plot(x) ±xÉäèpãI!±S“èÓãI±„ plot(x, y) x(3x-¶˛)Üy(3y-¶˛)ä„ pie(x) \„ boxplot(x) ›/„(box-and-whiskers) coplot(x˜y|z) 'uzzáÍ䣽Íä´m§±õxÜy „ matplot(x,y) „, Ÿ•x1òÈAy1ò, x 1ÈAy1, ùgaÌ. dotchart(x) XJx¥Í‚µßäCleveland:„£Å1Å \\„§ 1. GRAPHICS WITH R Previous Next First Last Back Forward 6

1.GRAPHICS WITH R pairs(x) 如果x是矩阵或是数据框,作x的各列之间的 二元图 hist(x) x的频率直方图 barplot(x) x的值的条形图 qqnorm(x) 正态分位数一分位数图 qqplot(x,y) y对x的分位数一分位数图 contour(x,y,z) 等高线图(画曲线时用内插补充空白的值), x和y必须为向量,z必须为矩阵,使得 dim(z)=c(length(x),length(y)) (x和y可以省略) fil1ed.contour(x,y,z)同上,等高线之间的区域是彩色的,并且绘制 彩色对应的值的图例 Previous Next First Last Back Forward 7

pairs(x) XJx¥› ½¥Í‚µßäxàÉm „ hist(x) x™«Üê„ barplot(x) xä^/„ qqnorm(x) ©†Í®©†Í„ qqplot(x, y) yÈx©†Í®©†Í„ contour(x, y, z) pÇ„(x­Çû^S÷øòxä), x⁄y7Lèï˛, z7Lè› , ¶ dim(z)=c(length(x),length(y)) (x⁄yå±é—) filled.contour(x,y, z)”˛, pÇÉm´ç¥Á⁄ßøÖ±õ Á⁄ÈAä„~ 1. GRAPHICS WITH R Previous Next First Last Back Forward 7

1.GRAPHICS WITH R image(x,y,z) 同上,但是实际数据大小用不同色彩表示 persp(x,y,z) 同上,但为透视图 stars(x) 如果x是矩阵或者数据框,用星形和线段画出 symbols(x,y,...) 在由x和y给定坐标画符号(圆,正方形,长方形, 星,温度计式或者盒形图),符号的类型、 大小、颜色等由另外的变量指定 termplot(mod.obj) 回归模型(mod.obj)的(偏)影响图 sunflowerplot(x,y) 同上但是以相似坐标的点作为花朵,其花瓣 数目为点的个数 stripchart(x) 把x的值画在一条线段上,样本量较小时可作为 盒形图的替代 Previous Next First Last Back Forward 8

image(x, y, z) ”˛ß¥¢SÍ‚å^ÿ”⁄ÁL´ persp(x, y, z) ”˛ßèfl¿„ stars(x) XJx¥› ½ˆÍ‚µß^(/⁄Ç„x— symbols(x, y, ...) 3dx⁄yâ½ãIxŒ“( , ê/, ê/, (ßß›O™½ˆ›/„), Œ“a.! å!Ù⁄d, C˛ç½ termplot(mod.obj) £8.£mod.obj§£†§Kè„ sunflowerplot(x,y) ”˛¥±ÉqãI:äès˘ßŸsã Í8è:áÍ stripchart(x) rxäx3ò^Ç„˛ß˛ûåäè ›/„Oì 1. GRAPHICS WITH R Previous Next First Last Back Forward 8

点击下载完整版文档(PDF)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
共53页,可试读18页,点击继续阅读 ↓↓
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有