base plot
系统类似于一步一步往图片上添加东西。通过这样的操作来得到想要的图片。这里说明也一下和 ggplot2
系统的区别。 ggplot2
系统可以通过一个代码出图。不需要连续运行很多的代码。同时该绘图系统可以通过 +
来对不同的函数添加到一起。
在进行base plot绘图之间,我们可以通过par函数对所做图的进行全局调整。基本上绘图参加能用到的参数在par中都能进行调节。不同的就是。通过par
我们调节的整个绘图区域的改变。而在各个绘图参数中的调节只是调整单独的参数。 单独的参数,我们可以在后面单独的讲。这里重点说一几个只能单独在par
中调节的参数。
mfcol
和mfrow
用来指定一个画布放多少个图形。其中两者的区别在于mfcol
在指定完之后。绘图的顺序是先把同一列绘制满,然后在绘制第二列。相反 mfrow
则是先把同一行绘制满,再绘制第二行 两者函数相同的参数都是c(nrow, ncol)
par(mfrow = c(2,2))
for(i in c(6:9)){
plot(1:i)
}
PS:在ggplot2系统中可以通过cowplot
包中的plot_grid
来对图进行拼接
mar
和oma
可以用来调整绘图的边界。两个函数接受到参数都是相同的。 oma = c(bottom, left, top, right)
default: c(0,0,0,0)lines mar = c(bottm, left, top, right)
default: c(5.1,4.1,4.1,2.1)lines 其中两者的区别可以通过下面的代码观察到(代码来自于r-graph-gallery):
# Margins area
par(oma=c(3,3,3,3)) # all sides have 3 lines of space
par(mar=c(5,4,4,2) + 0.1)
# Plot
plot(0:10, 0:10, type="n", xlab="X", ylab="Y") # type="n" hides the points
# Place text in the plot and color everything plot-related red
text(5,5, "Plot", col="red", cex=2)
# Place text in the margins and label the margins, all in forestgreen
mtext("Margins", side=3, line=2, cex=2, col="forestgreen")
mtext("par(mar=c(b,l,t,r))", side=3, line=1, cex=1, col="forestgreen")
mtext("Line 0", side=3, line=0, adj=1.0, cex=1, col="forestgreen")
mtext("Line 1", side=3, line=1, adj=1.0, cex=1, col="forestgreen")
mtext("Line 2", side=3, line=2, adj=1.0, cex=1, col="forestgreen")
mtext("Line 3", side=3, line=3, adj=1.0, cex=1, col="forestgreen")
box("figure", col="forestgreen")
# Label the outer margin area and color it blue
# Note the 'outer=TRUE' command moves us from the figure margins to the outer margins.
mtext("Outer Margin Area", side=1, line=1, cex=2, col="blue", outer=TRUE)
mtext("par(oma=c(b,l,t,r))", side=1, line=2, cex=1, col="blue", outer=TRUE)
mtext("Line 0", side=1, line=0, adj=0.0, cex=1, col="blue", outer=TRUE)
mtext("Line 1", side=1, line=1, adj=0.0, cex=1, col="blue", outer=TRUE)
mtext("Line 2", side=1, line=2, adj=0.0, cex=1, col="blue", outer=TRUE)
box("outer", col="blue")
PS:ggplot2中可以在theme
参数中通过plot.margin
来进行调整。
PS:对于其他参数可以用于具体函数中调整,也同样可以使用par
进行调整
base plot中可以通过bty
来调整边框,其中是有六种边框类型可以选的。分别为“o” (the default), “l”, “7”, “c”, “u”, or “]”
id <- c("o", "l", "7", "c", "u", "]")
par(mfrow = c(2,3))
for(i in id){
plot(0:6, bty = i, main = paste0("bty = ", i))
}
cex
代表字体缩放多少倍数。默认的为1.数字越大代表字体越大
plot(c(0:6),col='white',xlim = c(1,8))
text(2,5,labels = 'cex=0.5',cex=0.5)
text(3,4,labels = 'cex=0.8',cex=0.8)
text(4,3,labels = 'cex=1(default)',cex=1)
text(5,2,labels = 'cex=1.2',cex=1.2)
text(6,1,labels = 'cex=1.5',cex=1.5)
其中cex
的调整是图片内部的元素的调整。如果要调整其他元素则需要用到cex.axis
(坐标轴调整)。cex.lab
标签的调整。cex.main
标题的调整。 cex.sub
副标题的调整
font
可以调整字体的样式。其接受1:4的数字。其中1
代表正常字体;2
代表加粗字体;3
代表斜体;4
代表粗斜体。
plot(c(0:5), col = 'white')
text(2,4, labels = 'font=1', font = 1)
text(3,3, labels = 'font=2',font = 2)
text(4,2,labels = 'font=3',font = 3)
text(5,1,labels = 'font=4',font=4)
las
可以调整坐标轴的方向。其接受四种参数0:3
par(mfrow = c(2,2))
for(i in 0:3){plot(0:6, las = i, main = paste0("las = ", i))}
在我们使用plot
进行绘图图片的时候。输入的数据。默认是绘制的是点图。但是也可以通过type
参数来进行调整。比如绘制成把点连接起来的线图。type
可以接受的参数有九种。分别是‘p’,‘l’,‘b’,‘c’,‘o’,‘h’,‘s’,‘S’,‘n’
x <- 2:9;y <- 2:9
par(mfrow=c(3,3),mar=c(2,2,2,2))
ida <- c('p','l','b','c','o','h','s','S','n')
for(i in 1:length(ida)){
plot(1,1,ylim=c(1,10),xlim = c(1,10),col='white',
main = paste('type=',ida[i],sep = ''))
points(x,y,type=ida[i])
}
lty
可以用来调整线条的样式。接受的参数是0:7的数字。其主要样式有
par(mar=c(0,0,0,0))
data = matrix(rep(rep(1:7),10),ncol = 10, nrow = 7)
plot(data[1,],type = 'l',lty=0,ylim = c(1,8),xlim = c(-2,10),axes = F,
xlab = '', ylab = '')
text(-1,1,labels = 'lty=0')
for(i in 2:7){
lines(data[i,],lty=i-1)
text(-1,i,labels = paste('lty=',i))
}
lwd
可以调整线条的宽度。lwd
接受数字起含义和cex
一样。只不过一个是调整线条的宽度,一个是调整字体的大小。
par(mfrow = c(1,3))
for(i in c(1,3,5)){
plot(1:9, type = "l", lwd = i)
}
pch
可以选择不同的点的形状。其中点的形状包括0:25个数字和一些特殊的符号
par(mar=c(0,0,0,0))
plot(1,col='white',xlim = c(1,9),ylim = c(1,7))
for(i in 0:25){
x <- (i%/%5)*1+1
y <- 6-(i%%5)
if(length(which(c(21:25)==i)>=1)){
#21--25的点格式可以设置背景色
points(x,y,pch=i,bg='red',cex=2)
}else{
points(x,y,pch=i,cex=2)
}
text(x+0.2,y+0.3,labels = paste('pch=',i),cex=0.8)
}
points(6,5,pch='*',cex=2);text(6.2,5.3,labels = paste('pch=\'*\''),cex=0.8)
points(6,4,pch='.',cex=2);text(6.2,4.3,labels = paste('pch=\'.\''),cex=0.8)
points(6,3,pch='o',cex=2);text(6.2,3.3,labels = paste('pch=\'o\''),cex=0.8)
points(6,2,pch='O',cex=2);text(6.2,2.3,labels = paste('pch=\'O\''),cex=0.8)
points(7,6,pch='0',cex=2);text(7.2,6.3,labels = paste('pch=\'0\''),cex=0.8)
points(7,5,pch='+',cex=2);text(7.2,5.3,labels = paste('pch=\'+\''),cex=0.8)
points(7,4,pch='-',cex=2);text(7.2,4.3,labels = paste('pch=\'-\''),cex=0.8)
points(7,3,pch='|',cex=2);text(7.2,3.3,labels = paste('pch=\'|\''),cex=0.8)