




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、ReviewnGetting start and getting helpnCalculator and Operators + * / - = = !=nObjects: vector, factor, matrix, data frame and listnControl structures Conditional statements and LoopnData import and save read.table(), save.image(), saveFrom Data to GraphicsPlot()The following both plot y against x:pl
2、ot(y x) # Use a formula to specify the graphplot(x, y)Tryplot(0:20)*pi/10, sin(0:20)*pi/10), xlab=x, ylab=y)x-( 0:20)*pi/10y- sin(0:20)*pi/10)plot(yx)Datasets that relate to lecture note Download the R datafile: usingR.RData from ISpace Place this file in the working directoryWithin the R session, t
3、ype: load(usingR.RData) ls() or objects() Dataset that relates to lecture notePlot()Example 1str(elasticband)plot(distancestretch,data=elasticband)orattach(elasticband) # R now knows where to find distance & stretchplot(distance stretch)Example 2plot(ACT Year, data=austpop, type=l)plot(ACT Year,
4、 data=austpop, type=b)Try to find possible type by ?plotPlot(): adding lines, points and text on the plotExample 3plot(ACTYear,data=austpop,type=n)points(austpop$Year,austpop$ACT,pch=22,col=red)lines(austpop$Year,austpop$ACT,type = l, col=blue)text(austpop$Year5,austpop$ACT5,fifth)title(main=austpop
5、,xlab=Year,ylab=ACT)Note: The points() function adds points to a plot. The lines() function adds lines to a plot. The text() function adds text at specified locations. Add a linelines(austpop$Year,austpop$NT,type = b, col=red)Plot(): Controlling axisExample 4windows()plot(NTYear,data=austpop,type=n,
6、xaxt=n,xlab=Year,ylab=NT,main=austpop)axis(1,at=seq(1910,2000,by=10),cex.axis=0.6)points(austpop$Year,austpop$NT,pch=22,col=red)lines(austpop$Year,austpop$NT,type = l, col=blue)title(main=austpop)windows() open a graphics windowThe axis() function gives fine control over axis ticks and labels.xlim=,
7、 ylim= specifies the lower and upper limits of the axes, for example with xlim=c(1, 10) or xlim=range(x)Plot(): Size, color and choice of plotting symbol The parameter cex (“character expansion”) controls the size The col (“colour”) controls the colour of the plotting symbol. The parameter pch contr
8、ols the choice of plotting symbol.The parameter lwd controls the thick of line. Example 5plot(1, 1, xlim=c(1, 7.5), ylim=c(0,5), type=n) # Do not plot pointspoints(1:7, rep(4.5, 7), cex=1:7, col=1:7, pch=0:6)text(1:7,rep(3.5, 7), labels=paste(0:6), cex=1:7, col=1:7)lines(1:7,rep(2,7), lwd=2)lines(1:
9、7,rep(3,7), lwd=4,lty=3)Try to find other controlling papameter by type ?parPlotting Mathematical SymbolsnBy expression() x-1:100y-pi*x2plot(x, y, xlab=Radius, ylab=expression(Area = pi*r2)text(80,pi*802,expression(pi*r2),c(0,0.4)Separating different classes in a graph x-rnorm(50)yf-2*x1:25+1+rnorm(
10、25)ym-(x26:50)-1+rnorm(25)y=c(yf,ym)z-gl(2,25,labels=c(female,male)data1-data.frame(x,y,sex=z)plot(y,x,col=c(2,3)z,pch=c(2,16)z)legend(topleft,levels(z),col=2:3,pch=c(2,16)Multiple plots on the one pagenmfrow(): subsequent plots appear row by row library(MASS)data(Animals)par(mfrow=c(2,2), pch=16)at
11、tach(Animals)plot(body, brain)plot(sqrt(body), sqrt(brain)plot(body)0.1, (brain)0.1)plot(log(body),log(brain)detach(Animals)par(mfrow=c(1,1), pch=1) # Restore to 1 figure per pagen mfcol(): subsequent plots appear column by column Adding straight lines to plots plot(c(-2,3), c(-1,5), type = n, xlab=
12、x, ylab=y)abline(h=0, v=0, col = blue)abline(h = -1:5, v = -2:3, col =gray) abline(a=1, b=2, col =red) abline()Usageabline(a , b , h, v) a,b: the intercept and slope, single values h: the y-value(s) for horizontal line(s). v: the x-value(s) for vertical line(s). Plots that show the distribution of d
13、ata valuesExample: library(MASS)str(crabs)x - crabs$FLy - crabs$CLop - par(mfrow=c(2,1)hist(x, col=light blue, xlim=c(0,50)hist(y, col=light blue, xlim=c(0,50)par(op) Breakpoints: x - crabs$FLhist(x, col=light blue,breaks=(1:20)*1.5, xlim=c(0,30)hist(x, col=light blue,breaks=20, xlim=c(0,30) Histogr
14、ams: Plots that show the distribution of data values Histograms: Example: After normalization (x-mean(x)/sd(x)x - crabs$FLy - crabs$CLop - par(mfrow=c(2,1)hist( (x - mean(x) / sd(x),col = light blue,xlim = c(-3, 3) )hist( (y - mean(y) / sd(y),col = light blue,xlim = c(-3, 3) )par(op)Plots that show
15、the distribution of data values Density Plots: density()str(crabs)x - crabs$FLy - crabs$CLplot(density(x),type=l)hist(x, col=light blue, xlim=c(0,50), probability = TRUE) lines(density(x), col = red, lwd = 3)Box plot Boxplot()N - 2000 x - rnorm(N)boxplot(x, horizontal = TRUE, col = pink“)boxplot(x,
16、col = pink)library(MASS)boxplot(CLsex,data=crabs,col=pink,xlab=sex,ylab=CL,main=crabs)Normal probability plotsnqqnorm() gives a normal QQ plot of the values in yn - 1000 x - rnorm(n)qqnorm(x)qqline(x, col=red)x - runif(n)qqnorm(x)qqline(x, col=red)Example: nUse of hist(), lines(), density(), skewnes
17、s n - 1000 x - rnorm(n)skewness- sum(x-mean(x)3)/(n-1)* (sd(x)3) # #Calculate the skewness“hist(x, col=light blue, probability=TRUE, main=paste(skewness =, round(skewness,digits=2), xlab=, ylab=)lines(density(x), col=red, lwd=3)n - 1000 x - rnorm(n)kurtosis- sum(x-mean(x)4)/(n-1)* (sd(x)4) #For the
18、kurtosis qqnorm(x, main=paste(kurtosis =, round(kurtosis, digits=2),(gaussian)qqline(x, col=red)op - par(fig=c(.02,.5,.5,.98), new=TRUE)hist(x, probability=T, col=light blue, xlab=,ylab=, main=“, axes=F)lines(density(x), col=red, lwd=2)par(op)Example: Use of par(), qqnorm(), qqline(), hist(), lines(
19、), density(), kurtosisPlot of qualitative univariate variablesBarplot()barplot(height, .) statistics-50; accounting-100; finance-200; food-30 x-data.frame(statistics,accounting,finance,food)barplot(as.matrix(x)statisticsaccountingfinancefood050100150200Barplot()data(HairEyeColor)x - apply(HairEyeCol
20、or, 2, sum)barplot(x, col=1:4,legend.text = TRUE)title(Eye)Barplot()barplot(as.matrix(x),beside = TRUE, legend.text=TRUE)barplot(as.matrix(x), legend.text = TRUE)Barplotbarplot(as.matrix(x),horiz = TRUE,col = rainbow(length(x),legend.text = TRUE)title(main = Bar plot)Plot of qualitative univariate v
21、ariablesnPie chartx - apply(HairEyeColor, 2, sum)pie(x)title(main=Pie chart)lx - paste(names(x),=,round(x/sum(x)*100,2), %)pie(x,lx)Plot of Qualitative bivariate data (barplot)data(HairEyeColor)a -apply(HairEyeColor, c(1,2), sum)barplot(a, legend.text=TRUE)barplot(a,beside = TRUE,legend.text=TRUE)3
22、Dimension plotnPersp ():Draw perspective plots of surfaces over the xy planedata(volcano)z - volcanox - 10 * (1:nrow(z)y - 10 * (1:ncol(z) persp(x, y, z,theta = 120, phi = 15,scale = FALSE, axes = FALSE)Data VolcanoMaunga Whau is a volcano in the Auckland volcanic field.Data set volcano gives topogr
23、aphic information for Maunga Whau on a 10m by 10m grid. Data is displayed by a matrix with 87 rows and 61 columns, rows corresponding to grid lines running east to west and columns to grid lines running south to north. data(volcano)z - volcanonx - 21ny - 21x - seq(-1, 1, length = nx)y - seq(-1, 1, l
24、ength = ny) z-matrix(rep(0,nx*ny),nrow=nx)for (i in 1:nx) for(j in 1:ny) zi,j=xi2-yj2persp(x,y,z,theta=35)Example:Create a simple surface f(x,y) = x2 - y2 Example:Create a simple surface f(x,y) = x2 - y2 nx - 21ny - 21x - seq(-1, 1, length = nx)y - seq(-1, 1, length = ny) z - outer(x, y, function(x,
25、y) x2 - y2) persp(x, y, z, theta = 35) data(volcano)x - 10*1:nrow(volcano)y - 10*1:ncol(volcano)rx-range(x)ry-range(y)plot(x = 0, y = 0,type = n,xlim = rx, ylim = ry,xlab = , ylab = )contour(x, y, volcano,add = TRUE,)title(A Topographic Map of Maunga Whau, font = 4)n Contour(): Create a contour plot
26、, or add contour lines to an existing plot 3 Dimension plotdata(volcano)x - 10*(1:nrow(volcano)y - 10*(1:ncol(volcano)image(x, y, volcano,col = terrain.colors(100),axes = FALSE,xlab = , ylab = )contour(x, y, volcano,levels = seq(90, 200, by=5),add = TRUE)axis(1, seq(100, 800, by = 100)axis(2, seq(10
27、0, 600, by = 100)title(main = Maunga Whau Volcano, font.main = 4)Image() and Contour():R Graphics supplementary: PloygonnPolygon(): draws the polygons whose vertices are given in x and y x-seq(-6,6,length=200)fx-x2gx- x2+10*sin(x)plot(0,0,type=n,xlim=c(min(x),max(x),ylim=c(min(fx,gx),max(fx,gx),xlab=,ylab=,main=polygon function)polygon( c(x,rev(x), c(fx,rev(gx),col = red, border = 0 )lines(x, fx, lwd = 3)lines(x, gx, lwd = 3)ExerciseTry to use R to prod
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年度石材矿山安全责任合同规范文本
- 2025新版停车场租赁合同协议
- 2025合同样本:股权质押合同官方范本
- 2025商品房买卖合同模板
- 多项目管理规划标准化工具
- 回填土施工协议
- 叙事作文跑步比赛300字14篇范文
- 工厂设备采购及安装维护协议
- 小区装修管理培训课件
- 产品委托加工协议书样板
- 2025年天津市中考语文试卷深度评析及2026年备考策略
- 2025-2026学年人教鄂教版(2024)小学科学三年级上册(全册)教学设计(附目录P137)
- 2025年质量月知识竞赛题库含答案(初赛)
- 2025年人教版(2024)小学信息科技四年级(全一册)教学设计(附教材目录 P208)
- 全国行政区域身份证代码表(EXCEL版)
- 社区合理用药讲课
- 义务教育科学课程标准(2022年版)测试题及答案含课标解读
- 水运工程统一用表之一《浙江省港口工程统一用表》
- GB/T 13306-2011标牌
- GA 1800.6-2021电力系统治安反恐防范要求第6部分:核能发电企业
- FZ/T 13001-2013色织牛仔布
评论
0/150
提交评论