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