lines(x,z " />
R语⾔画图⼩结
本⽂以1950年到2010年期间我国的⽕灾统计数据为例,数据如下所⽰:
(0)加载数据
data<-("E:MyDocumentpData1950~2010⽕灾情况.csv")
x=t(data[1])
y=t(data[2])
z=t(data[3])
w=t(data[4])
maxy=max(y)
maxz=max(z)
maxw=max(w)
(1)将⽕灾数、直接损失、死伤⼈数,分别按年份作图
plot(x,y,type="o",xlab="年份",ylab="⽕灾(起)",ylim=c(0,maxy),col="red",main="1950年到2010年⽕灾年度统计",pch=c(15))
plot(x,z,type="o",xlab="年份",ylab="经济损失(万元)",ylim=c(0,maxz),col="blue",main="1950年到2010年⽕灾经济损失年度统计",pch=c(16))
plot(x,w,type="o",xlab="年份",ylab="死伤⼈数",ylim=c(0,maxw),col="green",main="1950年到2010年⽕灾死伤⼈数年度统计",pch=c(17))
注:x,y为图中的x轴和y轴,type设置图形中数据的显⽰类型,常见的有:
type="p"在图形中数据显⽰为点;
type="l"在图形中数据显⽰为线;
type="b"在图形中数据显⽰为点和连接线;
type="o"在图形中数据点覆盖在线上;
type="h"在图形中数据显⽰为从点到x轴的垂直线;
type="s"在图形中数据显⽰为阶梯图;
type="n"在图形中数据不显⽰;
xlab和ylab分别表⽰x轴和y轴的说明,ylim为控制y轴的范围,col为线条的颜⾊,main为图的主标题,pch为数据点的显⽰符号,可取1到25的整数以及“”中的符号:
(2)将⽕灾数、直接损失、死伤⼈数画在⼀个图⾥
>plot(x,y,type="o",xlab="年份",ylab="",ylim=c(0,maxy),col="red",main="1950年到2010年⽕灾年度统计",pch=c(15))#画⽕灾数曲线
>lines(x,z,type="o",col="blue",pch=c(16))#追加经济损失曲线
>lines(x,w,type="o",col="green",pch=c(17))#追加死伤⼈数曲线
(3)增加加图列
legend("topleft",c("⽕灾(起)","经济损失(万元)","死伤⼈数"),col=c("red","blue","green"),pch=15:17)
##对图添加栅格使⽤grid()函数
##grid(nx=6,ny=6,lwd=2)
如果只需横轴上有栅格,则ny=NA
##如何在图中添加⼀条直线⽤abline()函数
##>abline(h=18,col="black",lty=1,lwd=2)
##如何添加图例⽤legend()函数
##>legend("topright",legend=c("x","y"),pch=c(22,16),col=c("red","green"),lwd=2,lty=c(1,2))
当然topright还可以⽤坐标位置代替(x,y)
(4)平滑曲线图
##使⽤函数spline(x,y,n=?)
##>sp=spline(week,x,n=1000)
>sp1=spline(week,y,n=1000)
#n值表⽰平滑程度
>plot(sp,col="red",type="l",xlim=c(0,6),ylim=c(0,30),lwd=2,xlab="WEEK",ylab="STUDENT",main="lesson",sub="class",="green",=2)
>lines(sp1,col="green",type="l",xlim=c(0,6),ylim=c(0,30),lwd=2,xlab="WEEK",ylab="STUDENT",main="lesson",sub="class",="green",=2)
>legend("topright",legend=c("x","y"),col=c("red","green"),lwd=2,lty=c(1,2))
案例01-基本绘图
cars<-c(1,3,6,4,9)
plot(cars)
案例1-基本绘图
案例02-增添标题
cars<-c(1,3,6,4,9)
plot(cars,type="o",col="blue")#绘制蓝⾊折线图,
title(main="Autos",="red",=4)#增添标题,红⾊,粗斜体
案例2-增添标题
注1:其中type设置画图的类型,有九种可能的取值,分别代表不同的样式:
(1)'p')画点;
(2)'l'画线5;
(3)'b'同时画点和线,但点线不相交;
(4)'c'将type='b'中的点去掉,只剩下相应的线条部分;
(5)'o'同时画点和线,且相互重叠,这是它与type='b'的区别;
(6)'h'画铅垂线;
(7)'s'画阶梯线,从⼀点到下⼀点时,先画⽔平线,再画垂直线;
(8)'S'也是画阶梯线,但从⼀点到下⼀点是先画垂直线,再画⽔平线;
(9)'n'作⼀幅空图,没有任何内容,但坐标轴、标题等其它元素都照样显⽰。
注2:⽤于控制字体,分别为1:正常,2:斜体,3:粗体,4:粗斜体
案例03-增添新曲线
cars<-c(1,3,6,4,9)
trucks<-c(2,5,4,5,12)
plot(cars,type="o",col="blue",ylim=c(0,12))#ylim控制y轴的范围
lines(trucks,type="o",pch=22,lty=2,col="red")#添加折线,lty=2表⽰虚线
title(main="Autos",="red",=4)
案例3-增添新曲线
注1:pch控制点的符号;pch=19)实圆点、pch=20)⼩实圆点、pch=21)圆圈、pch=22)正⽅形、pch=23)菱形、pch=24)正三⾓尖、pch=25)倒三⾓尖,其
中,21-25可以填充颜⾊(⽤bg参数)
注2:lty控制连线的线型,可以是整数(1:实线,2:虚线,3:点线,4:点虚线,5:长虚线,6:双虚线),或者是不超过8个字符的字符串(字符为从"0"到"9"之间的数字)交替地指
定线和空⽩的长度,单位为磅(points)或象素,例如lty="44"和lty=2效果相同。线条虚实样式:0)不画线,1)实线,2)虚线,3)点线,4)点划线,5)长划线,6)点长
划线;或者相应设置如下字符串(分别对应前⾯的数字):'blank','solid','dashed','dotted','dotdash','longdash','twodash';还可以⽤由⼗六进制的数字组成的
字符串表⽰线上实线和空⽩的相应长度,如'F624'
案例04-添加图例
cars<-c(1,3,6,4,9)
trucks<-c(2,5,4,5,12)
g_range<-range(0,cars,trucks)
plot(cars,type="o",col="blue",ylim=g_range,axes=FALSE,ann=FALSE)#axes=FALSE,ann=FALSE表⽰不绘制坐标轴与标题
axis(1,at=1:5,lab=c("Mon","Tue","Wed","Thu","Fri"))
axis(2,las=1,at=4*0:g_range[2])
#axis(),2表⽰坐标轴在左侧,at=4*0:g_range[2]表⽰刻度范围为(0:g_range[2]),#每4个单位显⽰⼀个刻度
box()
lines(trucks,type="o",pch=22,lty=2,col="red")
title(main="Autos",="red",=4)#添加总标题
title(xlab="Days",=rgb(0,0.5,0))#添加x轴标题
title(ylab="Total",=rgb(0,0.5,0))#添加y轴标题
legend(1,g_range[2],c("cars","trucks"),cex=0.8,col=c("blue","red"),pch=21:22,lty=1:2);
#legend(),1,g_range[2]表⽰图例左上⾓的坐标;c("cars","trucks")标签,cex=0.8字体的放⼤倍数
案例4-添加图例
案例5-从⽂本读取数据绘图
将下列数据复制到⼀个txt⽂件中,命名为,放⾄C盘⽬录下
carstruckssuvs
124
354
646
456
91216
autos_data<-("C:/",header=TRUE,p="")#p=”“绰号中间为⼀空格,如果不输⼊空格,则会出现数据读取错误
max_y<-max(autos_data)
plot_colors<-c("blue","red","forestgreen")
png(filename="C:/",height=346,width=460,bg="white")#将图⽚输出到C⽬录,名字为,⼤⼩为346*460,背景⾊为⽩⾊
plot(autos_data$cars,type="o",col=plot_colors[1],ylim=c(0,max_y),axes=FALSE,ann=FALSE)
axis(1,at=1:5,lab=c("Mon","Tue","Wed","Thu","Fri"))
axis(2,las=1,at=4*0:max_y)
box()
lines(autos_data$trucks,type="o",pch=22,lty=2,col=plot_colors[2])
lines(autos_data$suvs,type="o",pch=23,lty=3,col=plot_colors[3])
title(main="Autos",="red",=4)
title(xlab="Days",=rgb(0,0.5,0))
title(ylab="Total",=rgb(0,0.5,0))
legend(1,max_y,names(autos_data),cex=0.8,col=plot_colors,pch=21:23,lty=1:3);
()
案例5-从⽂本读取数据绘图
案例06-输出图⽚到PDF以及坐标轴的控制
autos_data<-("C:/",header=T,p="")
plot_colors<-c(rgb(r=0.0,g=0.0,b=0.9),"red","forestgreen")
pdf(file="C:/",height=3.5,width=5)
par(mar=c(4.2,3.8,0.2,0.2))#mar控制绘图区别的⼤⼩,4个数字代表绘图区域距离下,左,上,右边界的⾏数,类似的mai则是英⼨数
plot(autos_data$cars,type="l",col=plot_colors[1],ylim=range(autos_data),axes=F,ann=T,xlab="Days",ylab="Total",=0.8,lwd=4)
axis(1,lab=F)
text(axTicks(3),-0.8,srt=45,adj=0.5,labels=c("Mon","Tue","Wed","Thu","Fri"),xpd=T,cex=0.8)
#axTicks()函数的功能是计算坐标轴,取值分别为1,2,3,4。1是计算下⽅坐标轴,2是左,3是上,4是右。
#text()函数的前两个数字是说明坐标轴标签的位置,str=标签倾斜的⾓度,adj取值为0到1,精细调整坐标轴的左右位置,0是最左边,1是最右边,0.5居中
axis(2,las=1,=0.8)
box()
lines(autos_data$trucks,type="l",lty=2,lwd=2,col=plot_colors[2])
lines(autos_data$suvs,type="l",lty=3,lwd=2,col=plot_colors[3])
legend("topleft",names(autos_data),cex=0.8,col=plot_colors,lty=1:3,lwd=2,bty="n");
#bty控制图形边框形状,可⽤的值为:"o","l","7","c","u"和"]"(边框和字符的外表相像);这些字符本⾝的形状对应着边框样式,⽐如(默认值)o表⽰四条边都显⽰,⽽c表⽰不显⽰右侧边如果bty="n"则不绘制边框
()
par(mar=c(5,4,4,2)+0.1)#恢复边界默认值
案例6-输出图⽚到PDF以及坐标轴的控制
案例07-简单条形图
BarCharts
cars<-c(1,3,6,4,9)
barplot(cars)
案例7-简单条形图
案例08-条形图加标签
autos_data<-("C:/",header=T,p="")
barplot(autos_data$cars,main="Cars",xlab="Days",ylab="Total",=c("Mon","Tue","Wed","Thu","Fri"),border="blue",density=c(10,20,30,40,50))
#density控制条形图中斜线的密度
案例8-条形图加标签
案例09-分组条形图
autos_data<-("C:/",header=T,p="")
data<-(autos_data)#转化为矩阵
barplot(data,main="Autos",ylab="Total",beside=TRUE,col=rainbow(5))
legend("topleft",c("Mon","Tue","Wed","Thu","Fri"),cex=0.6,bty="n",fill=rainbow(5));
案例9-分组条形图
案例10-条形图的堆叠
autos_data<-("C:/",header=T,p="")
par(xpd=T,mar=par()$mar+c(0,0,0,4))
#xpd对超出边界的图形的处理⽅式;取值FALSE把图形限制在作图区域内,出界的图形截去;取值TRUE出界的图形不截去
barplot(t(autos_data),main="Autos",ylab="Total",col=(3),space=0.2,=0.8,las=1,=c("Mon","Tue","Wed","Thu","Fri"),cex=0.8)
#space为条形间隔,las坐标轴标签样式;取0、1、2、3四个整数之⼀,分别表⽰“总是平⾏于坐标轴”、“总是⽔平”、“总是垂直于坐标轴”和“总是竖直”。
legend(6.2,30,names(autos_data),cex=0.8,fill=(3));
par(mar=c(5,4,4,2)+0.1)
案例10-条形图的堆叠
案例11-直⽅图
suvs<-c(4,4,6,6,16)
hist(suvs)
案例11-直⽅图
案例12-直⽅图2
autos_data<-("C:/",header=T,p="")
autos<-c(autos_data$cars,autos_data$trucks,autos_data$suvs)
hist(autos,col="lightblue",ylim=c(0,10))
案例12-直⽅图2
案例13-直⽅图3
autos_data<-("C:/",header=T,p="")
autos<-c(autos_data$cars,autos_data$trucks,autos_data$suvs)
max_num<-max(autos)
hist(autos,col=(max_num),breaks=max_num,xlim=c(0,max_num),right=T,main="AutosHistogram",las=1)
#right=TRUE(默认)直⽅图的范围是(a,b]左开右闭,right=FALSE区间为[a,b)
案例13-直⽅图3
案例14-不均匀间隔直⽅图
autos_data<-("C:/",header=T,p="")
autos<-c(autos_data$cars,autos_data$trucks,autos_data$suvs)
max_num<-max(autos)
brk<-c(0,3,4,5,6,10,16)#建⽴不均匀的区间
hist(autos,col=(length(brk)),breaks=brk,
xlim=c(0,max_num),right=F,main="ProbabilityDensity",
las=1,=0.8,freq=F)
案例14-不均匀间隔直⽅图
案例15-对数正态分布
r<-rlnorm(1000)
hist(r)
案例15-对数正态分布
案例16-饼图
cars<-c(1,3,6,4,9)
pie(cars)
案例16-饼图
案例17-饼图2
cars<-c(1,3,6,4,9)
pie(cars,main="Cars",col=rainbow(length(cars)),labels=c("Mon","Tue","Wed","Thu","Fri"))
案例17-饼图2
案例18-饼图3
cars<-c(1,3,6,4,9)
colors<-c("white","grey70","grey90","grey50","black")
car_labels<-round(cars/sum(cars)*100,1)
car_labels<-paste(car_labels,"%",p="")
pie(cars,main="Cars",col=colors,labels=car_labels,cex=0.8)
legend(1.5,0.5,c("Mon","Tue","Wed","Thu","Fri"),cex=0.8,
fill=colors)
案例18-饼图3
案例19-点图
autos_data<-("C:/",header=T,p="")
dotchart(t(autos_data))
案例19-点图
案例20-点图上⾊
autos_data<-("C:/",header=T,p="")
dotchart(t(autos_data),color=c("red","blue","darkgreen"),
main="DotchartforAutos",cex=0.8)
案例20-点图上⾊
案例21-Misc
plot(1,1,xlim=c(1,5.5),ylim=c(0,7),type="n",ann=FALSE)
text(1:5,rep(6,5),labels=c(0:4),cex=1:5,col=1:5)
#text(1:5,rep(6,5))表⽰其坐标,分别为(1,6),(2,6),(3,6),(4,6),(5,6)
points(1:5,rep(5,5),cex=1:5,col=1:5,pch=0:4)#添加点,倍数逐步放⼤
text((1:5)+0.4,rep(5,5),cex=0.6,(0:4))#添加⽂字0到4
points(1:5,rep(4,5),cex=2,pch=(5:9))#添加点,类型为pch=5到9
text((1:5)+0.4,rep(4,5),cex=0.6,(5:9))#添加⽂字5到9,(5:9)表⽰添加的⽂字
points(1:5,rep(3,5),cex=2,pch=(10:14))
text((1:5)+0.4,rep(3,5),cex=0.6,(10:14))
points(1:5,rep(2,5),cex=2,pch=(15:19))
text((1:5)+0.4,rep(2,5),cex=0.6,(15:19))
points((1:6)*0.8+0.2,rep(1,6),cex=2,pch=(20:25))
text((1:6)*0.8+0.5,rep(1,6),cex=0.6,(20:25))
案例21-Misc
参考链接:
1.
2.
3.
本文发布于:2022-12-04 09:10:09,感谢您对本站的认可!
本文链接:http://www.wtabcd.cn/fanwen/fan/88/50143.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |