R语言入门和使用技巧.ppt_第1页
R语言入门和使用技巧.ppt_第2页
R语言入门和使用技巧.ppt_第3页
R语言入门和使用技巧.ppt_第4页
R语言入门和使用技巧.ppt_第5页
已阅读5页,还剩45页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

R语言入门和使用技巧 LijunJin Contents Chapter1 Introduction Introduction R语言的概述定义 R是用于统计分析 绘图的语言和操作环境 R是属于GNU系统的一个自由 免费 源代码开放的软件 它是一个用于统计计算和统计制图的优秀工具 功能 R是一套完整的数据处理 计算和制图软件系统 R语言的发展1980年 贝尔实验室 R完善 MathSoft公司的统计科学部 R系统 Auckland大学的RobertGentleman和RossIhaka Risfree R语言的运用免费 开源 统计模块齐全避免了像商业软件在固定的分析过程中存在的问题用户可以得知其中的计算会暗含着何种漏洞或错误可自由计算任何想计算的统计量 包括图形 Introduction 资源网站资源 R主页 http www r project org 统计之都 http cos name TheRGraphGallery http addictedtor free fr graphiques Bioconductor http www bioconductor org RGraphicalManua http www oga Rcharts 基本运算符号 符号 命令或运算提示符 续行符基本算术运算例子 4 5 5 4 5 4 4 5 4 5赋值符例子 x 5x x 5assign assign x 5 求助符例子 parhelp help par 整除 5 3余数 5 3 向量 数值型 整型 单精度实型 双精度实型逻辑型复值型字符型 向量构建 c 没有什么规律 seq seq from to by length out rep rep x 数值型向量 例子 1 10 1 12345678910 xseq 10 sameas1 10 1 12345678910 seq 1 10 by 1 5 步长为1 5 1 1 02 54 05 57 08 510 0 seq 1 6 by 3 步长为3 1 14 seq 0 5 length out 11 生成向量长度为11 1 0 00 51 01 52 02 53 03 54 04 55 0 rep 1 10 1 12345678910 rep 1 10 2 整个向量重复2次 1 1234567891012345678910 rep 1 3 each 5 每个元素重复5次 1 111112222233333 rep 1 3 1 3 每个元素分别重复1 2 3次 1 122333 rep 1 4 each 2 len 10 1 1122334411 数组 x array 1 24 3 1 123 x array 1 24 3 4 1 123 x array 1 24 c 3 4 1 2 3 4 1 14710 2 25811 3 36912 t x 1 2 3 1 123 2 456 3 789 4 101112 x array 1 24 c 3 4 2 1 1 2 3 4 1 14710 2 25811 3 36912 2 1 2 3 4 1 13161922 2 14172023 3 15182124 矩阵 matrix 1 15 nrow 3 ncol 5 byrow TRUE 1 2 3 4 5 1 12345 2 678910 3 1112131415 xis matrix x 1 TRUE x array 1 24 c 3 4 2 is matrix x 1 FALSE y matrix x nrow 3 1 2 3 4 5 6 7 8 1 1471013161922 2 2581114172023 3 3691215182124 数据框与列表 数据框是一种矩阵形式的数据结构 数据框内含的数据可以是不同类型的数据 每一列的数据必须是同一类型 且每一列的长度必须相同 数据框可以由data frame 构建 数据框的构建 方法1 读文件read table 表格式文件read csv 逗号分隔的文件read delim tab键分隔的文件方法2 读变量 x c 42 7 64 9 y 1 4 z df data frame INDEX y VALUE x INDEXVALUE1142227336449 as data frame z df INDEXVALUE11422273364449 Xmingusage Step1 installXminginWindowStep2 clinkXminginLinuxStep3 displaypicture Xmingusage Chapter2 Parameters Drawelement par 函数的参数详解plot 及相关函数的参数说明 Parcharts Parcharts Col 图中符号 点 线等 的颜色 col axis坐标轴刻度标记的颜色col lab坐标轴标题的颜色col main图主标题的颜色col sub图副标题的颜色 Parcharts mfrow 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 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 plotcharts 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 iinseq along ltypes lines lpos i 1 20 1 20 type ltypes i 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 iin1 10 abline h i 1 lty lnames i lwd 3 text 15 i 0 5 lnames i cex 2 plotcharts plot c 0 5 1 5 c 0 3 col white xlab ylab main cex xaxt n yaxt n cex main 2 5 for iinseq 0 3 0 2 points 0 i pch 16 cex i text 1 i paste i cex i plot c 10 20 c 2 3 2 5 5 col white xlab ylab main lwd xaxt n log y cex main 2 5 yaxp c 0 1 50 2 for iin1 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图副标题的缩放倍数 plotcharts plot c 0 4 5 c 0 4 col white xlab ylab main pch xaxt n yaxt n cex main 2 5 for iin0 24 points i 5 i 5 pch i cex 2 text 0 3 i 5 i 5 i cex 2 Text titletitle 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 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 ParabolaPoints xlab xlab mtext 10ofthem for sin1 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 examples n sub RisGNU butnot mtext Latin 1accentedchars side 3 points c 6 2 c 2 1 pch 3 cex 5 col green text 6 2 thetextisCENTEREDaround x y 6 2 bydefault col 4 cex 8 text 2 1 orLeft Bottom JUSTIFIEDat 2 1 by adj c 0 0 adj c 0 0 text 4 9 expression hat beta X t X 1 X t y col 8 text 4 8 4 expression hat beta X t X 1 X t y col 11 cex 2 text 4 7 expression bar x sum frac x i n i 1 n col 12 cex 2 title text examples n legendandgrid 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 axis plot 1 7 rnorm 7 main axis examples type s xaxt n frame FALSE col red axis 1 1 7 LETTERS 1 7 col axis blue unusualoptions axis 4 col violet col axis darkviolet lwd 2 axis 3 col gold lty 2 lwd 0 5 plot 1 10 xaxt n axis 1 xaxp c 2 9 7 Chapter3 Graphicsamples Graphics Piecharts pie sales c 0 12 0 3 0 26 0 16 0 04 0 12 names pie sales c Blueberry Cherry Apple BostonCream Other VanillaCream 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 Pie3Dcharts 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 explodeborderlabelsradiusclockwisecolangledensityinit angle Venncharts 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 Vennpicture font 2 cex 1 5 axischarts 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 par usr rect usr 1 usr 3 usr 2 usr 4 col cornsilk border black lines x col blue points x pch 21 bg lightcyan cex 1 25 axis 2 col axis blue las 1 axis 1 at 1 12 lab month abb col axis blue box title main TheLevelofInterestinR font main 4 col main red title xlab 1996 col lab red axis side at NULL labels TRUE tick TRUE line NA pos NA outer FALSE font NA lty solid lwd 1 lwd ticks lwd col NULL col ticks NULL hadj NA padj NA plot 1 7 rnorm 7 main axis examples type s xaxt n frame FALSE col red axis 1 1 7 LETTERS 1 7 col axis blue axis 4 col violet col axis darkviolet lwd 2 axis 3 col gold lty 2 lwd 0 5 Rectcharts plot c 100 200 c 300 450 type n xlab ylab rect 100 300 125 350 transparentrect 100 400 125 450 col green border blue rect 115 375 150 425 col par bg border transparent rect 150 300 175 350 density 10 border red rect 150 400 175 450 density 30 col blue angle 30 border transparent legend 180 450 legend 1 4 fill c NA green par fg blue density c NA NA 10 30 angle c NA NA 30 30 rect xleft ybottom xright ytopdensityanglecolborderltylwd pointcharts plot 1 col white xlab ylab main curve abline points lines xaxt n yaxt n type n xlim c pi pi ylim c pi pi cex main 2 5 arrows c 3 0 c 0 3 c 3 0 c 0 3 lwd 3 curve sin pi pi add T lwd 3 col red curve cos pi pi add T lwd 3 col green curve tan pi 2 pi 2 add T lwd 3 col blue abline 0 1 lwd 3 lty 2 col grey80 abline 0 1 lwd 3 lty 2 col grey80 abline 1 0 lwd 3 lty 2 col grey80 abline 1 0 lwd 3 lty 2 col grey80 points 3 2 cos pi 1 50 25 3 2 sin pi 1 50 25 pch 19 line x ytype points pchcexcollwdbg ablinecharts plot c 2 3 c 1 5 type n xlab x ylab y asp 1 abline h 0 v 0 col gray60 text 1 0 abline h 0 col gray60 adj c 0 1 abline h 1 5 v 2 3 col lightgray lty 3 abline a 1 b 2 col 2 text 1 3 abline 1 2 col 2 adj c 1 1 abline a b untf FALSE abline h untf FALSE abline v untf FALSE abline coef untf FALSE abline reg untf FALSE histcharts 直方图宽度 组距高度 频数 频率 频率 组距注 高度 频率 组距 矩形的面积是数据落入区间的频率 可估计总体的概率密度 hist x breaks Sturges freq NULL probability freq include lowest TRUE right TRUE density NULL angle 45 col NULL border NULL main paste Histogramof xname xlim range breaks ylim NULL xlab xname ylab axes TRUE plot TRUE labels FALSE nclass NULL breaks 组距freq TRUE 频率直方图FALSE 密度直方图例子 hist sqrt islands breaks 12 col lightblue border pink histcharts 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 densitycharts 核密度估计函数已知样本 估计其密度 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 R sdensity kernelswithbw 1 for iin2 length kernels lines density 0 bw 1 kernel kernels i col i lwd 2 legend topright legend kernels col seq kernels lty 1 cex 8 y intersp 1 lwd 2 densitycharts par ann F m read table Inbred 0 8 head T n read table Wild 0 8 head T x m 10 m 10 0 y n 10 n 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 Methylationlevelofgene 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 barplotcharts 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 barplotcharts mt read 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 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 Relativemethylaionlevel 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 barplot2charts ci u upperbound ci l lowerbound library gplots hh t VADeaths 1 1 mybarcol gray20 ci l hh 0 85ci u hh 1 15barplot2 hh ci l ci l ci u ci u plot ci TRUE col 3 axes F barplot2charts library gplots hh t VADeaths 5 1 mybarcol gray20 ci l hh 0 85ci u hh 1 15mp barplot2 hh beside TRUE col c lightblue mistyrose lightcyan lavender legend colnames VADeaths ylim c 0 100 main DeathRatesinVirginia font main 4 sub Faked95percenterrorbars 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 RuralMaleRuralFemaleUrbanMaleUrbanFemale50 5411 78 715 48 455 5918 111 724 313 660 6426 920 337 019 365 6941 030 954 635 170 7466 054 371 150 0 boxplotcharts 箱线图直接简洁的展示数据分布的特征 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 c 1 2 3 4 9 10 1 2 0 8 boxplot x notch T col blue outline T boxplotcharts t read table CF Egg sum len txt header T par font 2 cex axis 1 2 font lab 2 font axis 2 boxplot list t 1 t 2 t 3 t 4 t 5 t 6 t 7 t 8 t 9 t 10 notch T outline F names c 1 2 3 4 5 6 7 8 9 10 col 2 11 mtext Methylationlevel side 1 line 3 cex 1 3 mtext Genelength side 2 line 3 cex 1 3 title CF EggDistribution box Clusterchart 系统聚类分析法 最短距离法 single 最长距离法 complete 中间距离法 median 类平均法 average 重心法 centroid 离差平方和 ward 相似分析法 Mcquitty xhc1hc3oparplot 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的距离 heatmapchart library pheatmap test matrix rnorm 200 20 10 test 1 10 seq 1 10

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论