R语言绘图基础

上传人:飞****9 文档编号:142948356 上传时间:2020-08-24 格式:PPT 页数:36 大小:2.48MB
返回 下载 相关 举报
R语言绘图基础_第1页
第1页 / 共36页
R语言绘图基础_第2页
第2页 / 共36页
R语言绘图基础_第3页
第3页 / 共36页
R语言绘图基础_第4页
第4页 / 共36页
R语言绘图基础_第5页
第5页 / 共36页
点击查看更多>>
资源描述

《R语言绘图基础》由会员分享,可在线阅读,更多相关《R语言绘图基础(36页珍藏版)》请在金锄头文库上搜索。

1、R 基础绘图 一、基础绘图工具 1. 绘图函数 2. 绘图参数介绍 3. 画图面板分割 4. 图形保存 二、图形案例 三、其它图形包,2,R绘制的图形,3,绘制地图,左图 maps包 map()右图 PBSmapping包 addBubbles(),fields 包实例 spdep 包实例,前言 学习 R 绘图没有什么技巧,只有记住常用参数的含义及用法,知道怎么去查找未知参数的含义及用法,再就是多多绘图,什么东西用得多了,自然就熟了。 如果需要更深入的了解,使用R 帮助。 R 使用帮助:?加上函数或参数名。例: ?plot,?legend demo(graphics) demo(persp),

2、R 绘图界面简介 R 的绘图界面中总共分为三部分outer margins, figure region, and plot region。 figure region 包括 plot region。,一、基础绘图工具 1. 绘图函数:在R中有两种绘图函数 1.1 高级绘图函数 创建一个新的图形,1. 绘图函数 1.2 低级绘图函数 在现有的图形上添加元素,2. 绘图参数介绍, plot(cars,font.lab=2,main=主标题,sub=副标题,type=p,xlim=c(7,20),ylim=c(20,100) plot(cars,font.lab=2,main=主标题,sub=副标

3、题,type=p,xlim=c(7,20),ylim=c(20,100),axes=F),plot(cars,font.lab=2,type=p) plot(cars,font.lab=2,type=l) plot(cars,font.lab=2,type=b) plot(cars,font.lab=2,type=o) plot(cars,font.lab=2,type=h) plot(cars,font.lab=2,type=s) plot(cars,font.lab=2,type=S),其它常用绘图参数,3. 画图面板分割,par是设置全局绘图参数的函数。 ask: par(ask=TRU

4、E),产生新的绘图之前提示操作。 new: FALSE:默认,新图在下一个 figure region 生成; TRUE:新图在当前 figure region mfcol: mfcol=c(3,2):3 行 2 列分割图形界面。 mfrow:以行的方式分割图形界面。 mfg: mfg=c(2,2,3,2):图片在 3 行 2 列图形界面中第二 行,第二列的位置,mar=c(a,b,c,d):设置 plot region 与 figure region 底部、 左边、上部、右边的距离,单位英寸。 mai=c(a,b,c,d):设置 plot region 与 figure region 底部、

5、 左边、上部、右边的距离,单位是文本行数。 mgp=c(a,b,c):三个坐标轴成分的位置,轴文本标记、单位记号标记和轴位置与轴线距离;单位是文本行。,par(mgp=c(1.6,0.6,0),mar=c(3,3,2,1) X=1:10;Y=1:10 cex_type=c(0.7,0.8,0.9,1,2,3,4,5,6,7) plot(X,Y,col=blue,pch=16, cex=cex_type, #绘图符号大小,1是正常大小 cex.axis=1.1, #坐标轴刻度数字大小 cex.lab=1.2, #坐标轴标签大小 cex.main=1.4, #标题文字大小 #cex.sub=1.3

6、, #副标题文字大小 font.axis=3, #坐标轴刻度数字字体,斜体 font.lab=2, #坐标轴标签字体,粗体 font.main=4, #标题字体,粗斜体 #font.sub=1, #副标题字体,正常 main=字体, xlim=c(1,12),ylim=c(1,12) text(X,Y,adj=0,labels=paste(cex=,cex_type),col=red,cex=1.2,font=3),R的绘图符号:pch=1:25,R的绘图符号: par(mgp=c(1.6,0.6,0),mar=c(3,3,2,1) pch_type=c(1:5,15:19) X=1:10;Y

7、=rep(6,10) plot(X,Y,col=1,pch=pch_type,cex=2,main=pch,font.lab=2) text(X,Y ,adj=-0.5,labels=paste(pch=,pch_type),srt=90),颜色名称:white, black, red, yellow, colors() 数字代号:1, 2, 3, 4, 5, 6, 7, 8 颜色函数:rainbow, heat.colors, terrain.colors, topo.colors, cm.colors, colors(),par(mgp=c(1.6,0.6,0),mar=c(3,3,2,1

8、) pch_type=c(1:5,15:19) X=1:10;Y=rep(6,10) plot(X,Y,col=1:10,pch=pch_type,cex=2,main=pch,font.lab=2) text(X,Y ,col=1:10,adj=-0.5,labels=paste(pch=,pch_type), srt=90),控制连线的类型 lty : 1: 实线,2: 虚线,3: 点线,4: 点虚线,5: 长虚线,6: 双虚线 控制线的宽度 lwd : 1为正常大小,1为增加宽度,par(mgp=c(1.6,0.6,0),mar=c(3,3,2,1) X=1:10;Y=1:10 line

9、_type=c(1:6) line_width=seq(from=0.6,by=0.6,length=6) plot(X,Y,col=blue,pch=16,type=n,main=line type and line width,font.lab=2,cex.lab=1.2) abline(h=3:8,lty=line_type,col=colors()120:126,lwd=line_width) text(3:8,3:8,adj=0,labels=paste(lty=,line_type,lwd=,line_width),col=red,cex=1.2,font=3),4. 图形保存 输

10、出到文件 pdf , postscript , xfig, bitmap, pictex, cairo_pdf, svg, png, jpeg, bmp, tiff pdf(plot.pdf,width=4,height=4) png(plot.png,width=400,height=600) dev.off() #绘制完图形后关闭图形设备 通过菜单命令保存图形,22,一般绘图步骤,1.打开绘图窗口,不绘制任何对象 plot(x, y, type=n, xlab=, ylab=, axes=F) 2.添加坐标点 points(x,y) 3.添加坐标轴 axis(1); axis(at=seq

11、(0.2,1.8,0.2), side=2) 4.补齐散点图的边框 box() 5.添加标题、副标题、横轴说明、纵轴说明 title(main=Main title, sub=subtitle, xlab=x-label, ylab=y-label),23,一般绘图步骤,二、图形案例,散点图点线混合图 require(stats) head(cars) plot(cars,1,cars,2,pch=16,col=red,xlab=speed,ylab=dist,type=p,font.axis=2,font.lab=2,cex.lab=1.5) x=seq(5,25,by=0.1); y=10

12、*sin(x)+50 lines(x,y,col=green,lwd=2) a=5:25; b=2*a+10*cos(a) points(a,b,pch=15,col=blue) legend(topleft,pch=c(16,-1,15),lty=c(-1,1,-1),col=c(red,green,blue),legend=c(plot with point,lines,points),散点图并列散点图 a=read.table(file=clipboard,header=T) matplot(a,type=o,pch=15:18,lty=1,bg=yellow,lwd=2, xlab=P

13、osition(nt),ylab=Frequency,font=2,font.lab=2,cex.lab=1.5) legend(topright,pch=15:18,lty=1,merge=T,legend=names(a),col=1:4),散点图坐标对数化,a=array(NA, dim=c(1000,3) a,2=sample(1:100000)1:1000 a,3=sample(1:100000)1:1000 plot(a,2,a,3,log=xy,cex.lab=1.4,cex.axis=1.2,font.lab=2,font.axis=2,xlab=R1,ylab=RS,pch=

14、.,col=red,cex=1.1,axes=F) axis(1,at=axTicks(1),labels=as.integer(axTicks(1) axis(2,at=axTicks(2),labels=as.integer(axTicks(2) title(main=R1 VS RS) box(),条形图标准条形图 abar=barplot(a,2,ylim=c(0,1.2*max(a,2),cex.lab=1.5,names.arg=a,1,xlab=Length(nt),font.lab=1.5,ylab=Number of small RNAs,space=0.9,col=blue

15、,main=Length distribution) ratio=paste(sprintf(%.2f,a,2/sum(a,2)*100),%,sep=) text(bar,a,2,labels=ratio,adj=c(0.5,0),cex=0.8,font=2,pos=3) abline(h=axTicks(2),lty=2,col=rgb(0,0,0,0.5); box(),条形图堆积条形图,h =read.table(“acgt.txt”,head=T) barplot(t(as.matrix(h),ylim=c(0,1),xlab=Position, ylab=Frequency,ma

16、in=Homo sapiens , col=rainbow(4),font=2,cex.lab=1.4) box() legend(topright,legend=c(A,G,C,T), pch=15,col=rainbow(4),条形图邻接条形图 barplot() lines(spline(),条形图Error Bar条形图 barplot(),韦恩图 draw.circle(),饼图二维饼图 pie(),饼图三维饼图 pie3D(),盒形图(箱线图) boxplot(),频率直方图 hist(),热图 heatmap(),library(pheatmap) pheatmap(),library(gplots) heatmap.2(),三、其它图形包 gplots ggplot2 http:/www.bioconductor.org/ source(http:/bioconductor.org/biocLite.R) biocLite(),library(gplots) data(mtcars) x - as.matrix(mt

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号