R语言画图大全(实战3,6,11)

R语言画图大全(实战3,6,11)
#图形初阶
#例1(画图)
attach(mtcars)#绑定数据框mtcars
plot(wt,mpg)
abline(lm(mpg~wt))#添加最优拟合曲线
title("Regression of MPG on weight")
detach(mtcars)#对数据框删除绑定
#例2(保存图)
setwd("D:/桌面/R语言学习")
png("mygraph.png")
attach(mtcars)#绑定数据框mtcars
plot(wt,mpg)
abline(lm(mpg~wt))#添加最优拟合曲线
微电解填料title("Regression of MPG on weight")
detach(mtcars)#对数据框删除绑定
无石棉刹车片
dev.off()
#例3(打开新的图形窗口)
w()
attach(mtcars)#绑定数据框mtcars
plot(wt,mpg,col="red")
detach(mtcars)#对数据框删除绑定
开关柜除湿
w()
attach(mtcars)#绑定数据框mtcars
plot(wt,mpg,col="blue")
abline(lm(mpg~wt))#添加最优拟合曲线
detach(mtcars)#对数据框删除绑定
w()
attach(mtcars)#绑定数据框mtcars
plot(wt,mpg,col="green")
abline(lm(mpg~wt))#添加最优拟合曲线
title("Regression of MPG on weight")
detach(mtcars)#对数据框删除绑定
#例4
dose<-c(20,30,40,45,60)
drugA<-c(16,20,27,40,60)
drugB<-c(15,18,25,31,40)
plot(dose,drugA,type="b")#b表示同时画点和线
par(optinname=value,...)#设置当前图形参数,除非再次修改,否则将在会话结束前一直有效
pch#指定绘制点使用的符号
lty#指定线条类型
lwd#指定线条宽度
fg#图形的前景
bg#图形的背景
col#绘图颜
col.axis#坐标轴刻度文字的颜
col.lab#坐标轴标签的颜
col.main#标题颜
col.sub#副标题颜
#文本大小
cex#指定绘制点符号的大小
cex.axis#坐标轴刻度文字的缩放倍数
cex.lab#
彩铅芯cex.main#
cex.sub#
#字体
font#指定绘图使用的字体样式
font.axis#
font.lab#
font.main#
font.sub#
#例5
opar<-adonly=T)#复制当前的图形参数设置
par(lty=2,pch=17)
plot(dose,drugB,type="b")
par(opar)
#例6
n<-10
mycolors<-rainbow(n)
pie(rep(1,n),labels=mycolors,col=mycolors)
mygrays<-gray(0:n/n)
pie(rep(1,n),lables=mygrays,col=mygrays)
title(main="",sub="",xlab="",ylab="")#为图形添加标题和坐标轴标签
axis(side,at=,labels=,pos=,...)#创建自定义的坐标轴
side #在图形的哪边绘制坐标轴(1下,2左,3上,4右)
at #需要绘制刻度线的位置
labels #置于刻度线旁边的文字标签 
pos #坐标轴线绘制位置的坐标
las #标签是否平行于(0)或垂直于(2)坐标轴
tck #刻度线的长度,以相对于绘图区域大小的分数表示
#例7
x<-c(1:10)
y<-x
z<-10/x
opar<-adonly=T)
par(mar=c(5,4,4,8)+0.1)#增加边界大小
plot(x,y,type="b",pch=21,yaxt="n",ann=FALSE)#ann=FALSE是移除默认的标题和标签
lines(x,z,type="b",pch=22,col="red")
axis(2,at=x,labels=x,col.axis="blue",las=2)#绘制坐标轴
axis(4,at=z,labels=round(z,digits=2),col.axis="green",cex.axis=0.7,las=2,tck=-0.01)
mtext("y=1/x",side=4,line=3,,cex.lab=1,las=2)#添加标题和文本
title("An Example of Creative Axes",xlab="X",ylab="Y=X")
par(opar)
abline(h=yvalues,v=xvalues) #为图形添加参考线
legend(location,title,legend,...) #添加图例
location #图例的位置,可以执行locator(1),单击鼠标给出位置
text() #向绘图区域内部添加文本
mtext() #向图形的四个边界之一添加文本
#例8(图形组合)
attach(mtcars)
opar<-par(mfrow=c(3,1))#图1
hist(wt)
hist(mpg)
hist(disp)
悬空板
电厂巡检机器人par(opar)
layout(matrix(c(1,1,2,3),2,2,byrow=T))#图2
hist(wt)
hist(mpg)
hist(disp)
detach(mtcars)
通过令fig=c(x1,x2,y1,y2)来完成图形布局的精细控制
#例9(图形布局)
opar<-adonly=T)
par(fig=c(0,0.8,0,0.8))
plot(mtcars$wt,mtcars$mpg)
par(fig=c(0,0.8,0.38,1),new=T)
boxplot(mtcars$wt,horizontal=T,axes=F)
par(fig=c(0.5,1,0,0.8),new=T)

本文发布于:2024-09-22 03:33:09,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/4/104676.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:图形   添加   坐标轴   绘制   刻度   指定   位置   标签
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议