R语言绘图系列:
低级函数合集:
Functions | Frequency | Functions |
---|---|---|
arrows | *** | 在图上添加箭头 |
axis | ***** | 绘制坐标轴 |
box | *** | 绘制图形边框 |
abline | ***** | 添加直线 |
clip | *** | 剪切图形,将图形分割成几个部分 |
curve | *** | 添加曲线 |
locate | ***** | 识别图中点的坐标 |
layout | ***** | 切分画布 |
legend | ***** | 添加图例 |
lines | ***** | 添加线条(直线和曲线) |
segments | *** | 添加线段 |
rug | * | 添加小地毯 |
points | *** | 添加点 |
polygon | *** | 多边形 |
symbols | **** | 添加标记,也可以画气泡图 |
titles | ***** | 添加标题 |
text | ***** | 添加文字 |
mtext | **** | 在图像四周添加文字 |
grid | *** | 添加背景网格线 |
绘图练习
- axis和mtext
N <- 200
x <- seq(-4,4,length=N)
y1 <- sin(x)
y2 <- cos(x)#生成数据
op <- par(mar=c(5,4,4,4)) #mar参数定义图形上下左右距离边框的距离
xlim <- range(x)
ylim <- c(-1.1,1.1)
plot(x,y1,col='blue',type='l',
xlim=xlim,ylim=ylim, #设置坐标轴的取值范围
axes=F,xlab='',ylab='',main='Title')
#此处axes参数为F,故在这里我们不画坐标轴
axis(1)
#添加坐标轴,第一个参数是位置参数,取值1,2,3,4,分别表示下、左、上、右
axis(2,col = 'blue') #添加第二条坐标轴
par(new=TRUE) #在刚刚做好的图上添加新图
plot(x,y2,col='red',type = 'l',
xlim=xlim,ylim=ylim,
#注意此处为了使两张图完美契合,其坐标轴取值范围必须保持一致
axes=F,xlab='',ylab='',main='')
axis(4,col = 'red')#绘制第三条坐标轴
mtext('First Y axis',2,line = 2,col = 'blue',cex = 1.2)
#添加边缘文字,第一个参数传入一个字符串,表示要在图中添加的文字,第二个参数表示文字出现的位置
mtext('Second Y axis',4,line = 2,col = 'red',cex = 1.2)

- 添加坐标轴的演示 axis abline curve
plot(rnorm(100),axes = FALSE,col='orange',pch=16,ylab = 'scatter points',xlab = 'x axis',ylim = c(-3.5,3.5)) #不显示坐标轴
axis(1,at=seq(0,100,10),labels = LETTERS[1:11],col = 'red',lwd = 1.3,col.ticks = 'pink')
#at参数定义横坐标间隔(刻度),从0-100每10个值取1个。labels是每个刻度的标签。
#x轴的A如果没有出现的是散点的取值范围和x轴的取值范围不一致所致,在第一行代码后面添加xlim=c(0,100)即可解决(与at后的取值保持一致即可)
axis(2,at=seq(-3,3,1),col = 'blue',lwd = 1.3,col.ticks = 'sky blue') #添加纵坐标
abline(h=0,col='red',lwd=2,lty=2) #添加一条横线 h=0表示水平线,y=0
abline(v=4,col='black') #添加一条竖线 v=4表示是垂直线,x=4
curve(sin,0,100,add=TRUE)

- legend添加图例
a=c(1:5);b=c(5,3,4,5,5);c=c(4,5,4,3,1) #随便生成三个变量
plot(b~a,type='b',bty='l',xlab='value of a',ylab = 'value of b',col=rgb(0.2,0.4,0.1,0.7),lwd=3,pch=17,ylim=c(1,5))
lines(c~a,col=rgb(0.8,0.4,0.1,0.7),lwd=3,pch=19,type='b')
#添加图例
legend('bottomleft',
#位置参数,接受一个字符串,比如‘topright'表示右上方,也接受坐标,比如3,5
legend=c('Group 1','Group 2'),
col=c(rgb(0.2,0.4,0.1,0.7),
rgb(0.8,0.4,0.1,0.7)),#定义图例的填充颜色
pch=c(17,19),
bty='n',#定义边框,此处为不要边框
pt.cex=2, #图标的大小
cex=1.2, #文字的大小
text.col='black', #文字的颜色
horiz=F, #是否水平放置
inset=c(0.1,0.1))

⚠️在不确定将图例添加在什么位置时,可使用locator函数。
locator()函数可以定位图上的点。运行locator函数之后在图上随便点一个点,点击finish就会返回刚刚那个点的位置坐标。
- text和clip
x <- rnorm(1000)
hist(x,xlim = c(-4,4)) #此时并未对横轴设置break,图形横轴是系统自行区分并取参数的。
#使用usr参数找到画布四个角的坐标
usr <- par('usr')
usr
#[1] -4.32 4.32 -8.20 213.20
#使用clip函数切割图形。clip函数接受的数值是x1, x2, y1, y2,也就是将要切割的部分
clip(usr[1],-1,usr[3],usr[4]) #切从x轴从-4.32到-1,y轴从-8.20到213.20的矩形(后台切割的,在图形上未显示)
hist(x,col = 'red',add=TRUE) #上色
#切割另外一个矩形并上色
clip(1,usr[2],usr[3],usr[4])
hist(x,col = 'blue',add=TRUE)
#text函数:随意在图形中添加文字,接受的参数是具体的位置参数
text(x=3,y=50,labels='blue area',col='blue',srt=300) #srt参数是旋转的角度

使用text函数在每个条图下面添加标签
data <- sample(20:80,20)
par(mar=c(4,3,3,1))
barcenter <- barplot(data,col = rgb(0.7,0.5,0.4,0.2),axes=FALSE,ylim = c(-5,100))
#如果这里ylim设置为ylim = c(0,100),即使运行下面的text,也不能绘制标签。因为text的纵轴是-2。
axis(2,at=seq(0,100,10))
text(barcenter,-2,labels = LETTERS[1:20],col = 'red',cex = 0.8)

- symbols绘制气泡图
#构建数据集
mycars <- data.frame(car=c('Honda','Benz','BMW','Audi','Chery','Toyota','Ford'),
mtg=c(4.0,5.2,5.5,2.8,1.8,2.8,3.0))
plot(mycars$mtg,cex=mycars$mtg,col='green',pch=21,bg='pink',ylab = "cars\'mtg",xlab='cars')
#此时,cex参数被设置为车辆mtg的大小,因此也就生成了一个气泡图
text(1:7,mycars$mtg,labels = mycars$car) #在图上添加每辆车的名字 横轴坐标是1:7,纵轴坐标是mycar$mtg的大小

N <- nrow(trees) #trees是R的一个内置数据集,包含3个变量。
with(trees,{
symbols(Height,Volume,circles = Girth/24,inches = FALSE, #symbols第一个参数是横轴,第二个参数是纵轴,第三个参数是圈的大小。/24是标准化操作,因为原始值太大了(推荐的操作是用Girth/Girth最大值)。inches = TURE时是圈的大小以inch为单位
main = 'Trees Girth') #这里画了一张没有经过调色的图
op <- palette(rainbow(N,end = 0.9))#设置调色板,这里我们用的是彩虹色
symbols(Height,Volume,circles = Girth/16,inches = FALSE,bg=1:N,
fg='grey30',main = "symbols(*,circles=Girth/16,bg=1:N)")
palette(op) #又画了一张调过色的图
})


设置气泡透明度
par(bg='white')#定义白色背景
par(mar=c(0.8,0.8,0.8,0.8))#定义画布大小
a=seq(1:100)+0.1*seq(1:100)*sample(c(1:10),100,replace = T)
b=seq(1:100)+0.2*seq(1:100)*sample(c(1:10),100,replace = T)
#产生两个数据
plot(a,b,xlim=c(10,200),ylim = c(10,200),pch=20,bg='white',cex=3+(a/30),
col=rgb(a/300,b/300,0.9,0.9))
#画散点图,重点是col参数。这里调用了rgb()函数,这也是一个调色板函数。这个函数中有个参数叫alpha,可以设置透明度
abline(h=c(0,50,100,150),v=c(0,50,100,150),col='whitesmoke')

⚠️调色板函数中有个参数叫alpha,也可以设置气泡的透明度。(在ggplot2中很容易实现)
- layout {graphics} 切分画布
⚠️par(mfrow)只能规规矩矩的切分成几行几列,而layout的切分更灵活
nf <- layout(matrix(c(1,1,2,3),2,2,byrow = TRUE),widths = c(3,1),heights = c(2,2),TRUE)
layout.show(nf)
a=seq(129,1)+4*runif(129,0.4)
b=seq(1,129)^2+runif(129,0.98)
hist(a,breaks = 30,border = F,col = rgb(0.1,0.8,0.3,0.5),xlab = 'distribution of a',main = '')
boxplot(a,xlab='a',col=rgb(0.8,0.8,0.3,0.5),las=2)
boxplot(b,xlab='b',col=rgb(0.4,0.2,0.3,0.5),las=2)

关于layout函数:
nf <- layout(matrix(c(1,1,2,3),2,2,byrow = TRUE),widths = c(3,1),heights = c(2,2),TRUE)
layout.show(nf)

nf <- layout(matrix(c(1,1,2,3,4,4),3,2,),widths = c(3,2),heights = c(2,2),TRUE)
layout.show(nf)

- grid添加网格
op <- par(mfcol=1:2)
with(iris,
{plot(Sepal.Length,Sepal.Width,col=as.integer(Species),
xlim=c(4,8),ylim=c(2,4.5),panel.first=grid(),#使用默认的参数
main='with(iris,plot(....,panel.first=grid(),..))')
plot(Sepal.Length,Sepal.Width,col=as.integer(Species),
panel.first=grid(3,lty = 1,lwd=2),
#设置为3行3列的网格,采用实线,宽度为2
main='...panel.first=grid(3,lty=1,lwd=2),..')
})
par(op)

网友评论