R语言画图小结

2023-04-23,,

本文以1950年到2010年期间我国的火灾统计数据为例,数据如下所示:

(0)加载数据

data<-read.csv("E:\\MyDocument\\p\\Data\\1950~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",col.main="green",font.main=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",col.main="green",font.main=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", col.main="red", font.main=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:font.main用于控制字体,分别为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", col.main="red", font.main=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', 'dot dash', '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", col.main="red", font.main=4) #添加总标题

title(xlab="Days", col.lab=rgb(0,0.5,0)) # 添加x轴标题

title(ylab="Total", col.lab=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文件中,命名为data.txt,放至C盘目录下

cars trucks suvs

1 2 4

3 5 4

6 4 6

4 5 6

9 12 16

autos_data <- read.table("C:/data.txt", header=TRUE, sep=" ") #sep=” “绰号中间为一空格,如果不输入空格,则会出现数据读取错误

max_y <- max(autos_data)

plot_colors <- c("blue","red","forestgreen")

png(filename="C:/figure.png", height=346, width=460, bg="white") # 将图片输出到C目录,名字为figure.png,大小为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", col.main="red", font.main=4)

title(xlab= "Days", col.lab=rgb(0,0.5,0))

title(ylab= "Total", col.lab=rgb(0,0.5,0))

legend(1, max_y, names(autos_data), cex=0.8, col=plot_colors, pch=21:23, lty=1:3);

dev.off()

  

案例 5-从文本读取数据绘图

案例06-输出图片到PDF以及坐标轴的控制

autos_data <- read.table("C:/data.txt", header=T, sep=" ")

plot_colors <- c(rgb(r=0.0,g=0.0,b=0.9), "red", "forestgreen")

pdf(file="C:/figure.pdf", 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", cex.lab=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, cex.axis=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"则不绘制边框

dev.off()

par(mar=c(5, 4, 4, 2) + 0.1) #恢复边界默认值

  

案例 6-输出图片到PDF以及坐标轴的控制

案例07-简单条形图

Bar Charts

cars <- c(1, 3, 6, 4, 9)

barplot(cars)

  

案例 7-简单条形图

案例08-条形图加标签

autos_data <- read.table("C:/data.txt", header=T, sep=" ")

barplot(autos_data$cars, main="Cars", xlab="Days",ylab="Total", names.arg=c("Mon","Tue","Wed","Thu","Fri"),border="blue", density=c(10,20,30,40,50))

# density控制条形图中斜线的密度

  

案例 8-条形图加标签

案例09-分组条形图

autos_data <- read.table("C:/data.txt", header=T, sep=" ")

data <- as.matrix(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 <- read.table("C:/data.txt", header=T, sep=" ")

par(xpd=T, mar=par()$mar+c(0,0,0,4))

# xpd对超出边界的图形的处理方式;取值FALSE把图形限制在作图区域内,出界的图形截去;取值TRUE出界的图形不截去

barplot(t(autos_data), main="Autos", ylab="Total", col=heat.colors(3), space=0.2, cex.axis=0.8, las=1,names.arg=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=heat.colors(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 <- read.table("C:/data.txt", header=T, sep=" ")

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 <- read.table("C:/data.txt", header=T, sep=" " )

autos <- c(autos_data$cars, autos_data$trucks, autos_data$suvs)

max_num <- max(autos)

hist(autos, col=heat.colors(max_num), breaks=max_num, xlim=c(0,max_num), right=T, main="Autos Histogram", las=1)

# right = TRUE (默认)直方图的范围是(a, b]左开右闭,right = FALSE区间为[a, b)

  

案例 13-直方图3

案例14-不均匀间隔直方图

autos_data <- read.table("C:/data.txt", header=T, sep=" ")

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=heat.colors(length(brk)), breaks=brk,

xlim=c(0,max_num), right=F, main="Probability Density",

las=1, cex.axis=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, "%", sep="")

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 <- read.table("C:/data.txt", header=T, sep=" ")

dotchart(t(autos_data))

  

案例 19-点图

案例20-点图上色

autos_data <- read.table("C:/data.txt", header=T, sep=" ")

dotchart(t(autos_data), color=c("red","blue","darkgreen"),

main="Dotchart for Autos", 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. R语言画曲线图

2. R语言绘图渐进

3. 如何用R画折线图,散点图,平滑曲线图

R语言画图小结的相关教程结束。

《R语言画图小结.doc》

下载本文的Word格式文档,以方便收藏与打印。