a.设置图形边缘大小
par(mar = c(bottom, left, top, right)), 即为到底部、左边、顶部、右边的距离构成的向量
Default:par(mar = c(5, 4, 4, 2) + 0.1)
b. 在整个device region 画图
par(xpd=T)
Default:par(xpd=F)
######################
2.设置坐标轴刻度
axis(side, xaxp, xaxs, col.axis, ……)
side:1=below, 2=left, 3=above and 4=right.
xaxp:A vector of the form c(x1, x2, n) giving the coordinates of the extreme tick marks and the number of intervals between tick-marks when par("xlog") is false. Otherwise, when log coordinates are active, the three values have a different meaning: For a small range, n is negative, and the ticks are as in the linear case, otherwise, n is in 1:3, specifying a case number, and x1 and x2 are the lowest and highest power of 10 inside the user coordinates, 10^par("usr")[1:2]. (The "usr" coordinates are log10-transformed here!) n=1 will produce tick marks at 10^j for integer j, n=2 gives marks k 10^j with k in {1,5}, n=3 gives marks k 10^j with k in {1,2,5}.
yaxp:同上。将所有的x换成y即可。
xaxs,yaxs:"r"或者"i", 分别设定x 和y 轴的形式。"i" (内在的) 和"r" (默认) 形式的刻度都适合数据的范围,但是"r" 形式的刻度会在刻度范围两边留一些空隙。
axes=F:不仅不画坐标轴,也不画边框。
las:刻度标记的方向。0 表示总是平行于坐标轴,1 表示总是水平,以及2 表示总是垂直于坐标轴。
lab:刻度标记的值向量。
at:刻度标记的位置向量。
Example 1:
plot(1:10, xaxt = "n", main="Example 1")
axis(1, xaxp=c(2, 9, 7))
plot(1:4, rnorm(4), axes = FALSE, main="Example 2")
axis(1, 1:4, LETTERS[1:4])
axis(2)
box()
Example 3:
plot(1:7, rnorm(7), ,main="Example 3", type = "s", xaxt = "n", frame = FALSE, col = "red")
axis(1, 1:7, LETTERS[1:7], col.axis = "blue")
Example 4:
plot(1:15, xaxt = "n",main="Example 4")
axis(1,at=c(1,3,9),lab=c(5, 7, 12))
savePlot(filename="Example 4",type ="png",device=dev.cur());
Example 5:
plot(1:10, yaxt = "n",main="Example 5")
axis(2,at=c(1,3,9),lab=c(5, 7, 12),las=1)
savePlot(filename="Example 5",type ="png",device=dev.cur());
###################################################
3.改变坐标轴标签的位置
mtext(text, side = 3, line = 0, outer = FALSE, at = NA,
adj = NA, padj = NA, cex = NA, col = NA, font = NA, ...)
at: give location of each string in user coordinates. If the component of at corresponding to a particular text item is not a finite value (the default), the location will be determined by adj.
adj: adjustment for each string in reading direction. For strings parallel to the axes, adj = 0 means left or bottom alignment, and adj = 1 means right or top alignment.
###########################################################
4. 添加箭头
a. 画箭头图
arrows(x0,y0,x1,y1,length = 0.25, code=3,angle=30,col = par("fg"), lty = par("lty"), lwd = par("lwd"),
……)
code:如果code=2则在各(x0,y0)处画箭头,如果code=1则在各(x1,y1)处画箭头,如果code=3则在两端都画箭头;缺省值为1。
angle:控制箭头轴到箭头边的角度
length:箭头的长度(英寸为单位)
Example:
x <- stats::runif(12); y <- stats::rnorm(12)
i <- order(x,y); x <- x[i]; y <- y[i]
plot(x,y, main="arrows(.)")
s <- seq(length(x)-1)# one shorter than data
arrows(x[s], y[s], x[s+1], y[s+1], col= rainbow(12))
savePlot(filename="arrows",type ="png",device=dev.cur());
#与segments()比较
plot(x,y, main="segments(.)")
segments(x[s], y[s], x[s+1], y[s+1], col= rainbow(12));

b.给坐标轴添加箭头
Example:
plot(1:3,seq(0.05,0.15,0.05),xaxs="i", yaxs="i",xlim=c(1,3.2),ylim=c(0,0.17),
bty="l", type="b",xaxt = "n", main="Add arrows&labels for axis",
xlab="",ylab="",)
axis(1, xaxp=c(1, 3, 2))
par(xpd=T) #可以在整个device region 画图
arrows(3,0,3.2,0,length=0.2)
arrows(1,0.15,1,0.17,length=0.2)
mtext("x",line=0.5,side=1,adj=1)
mtext("y",at=c(1,0.17),adj=1,side=2,line=0.5,las=2)
#las 控制坐标轴刻度数字标记方向的整数(0: 平行于轴,1: 横排,2: 垂直于轴,3:竖排)
savePlot(filename="axis",type ="png",device=dev.cur());

5.将两个直方图画在同一个图上
hist()
hist(x,breaks=10,border="red",add=T)
legend()
pch 图例标记的种类
pt.bg 图例标记的背景颜色
col 图例标记的颜色
Example:
par(mar = c(3, 4, 2, 2) + 0.1)
hist(hbM,breaks=10,border="blue",xlim=c(50,180),main="",xlab="",ylab="")
hist(hbF,breaks=10,border="red",add=T,)
color=rainbow(5)
rect(50,0,60,1, col="pink",border="grey")
rect(60,0,70,1, col="pink",border="grey")
rect(70,0,80,5, col="pink",border="grey")
rect(80,2,90,15, col="pink",border="grey")
rect(80,0,90,2, col=color[4],border="grey")
rect(90,2,100,40, col="pink",border="grey")
rect(90,0,100,2, col=color[4],border="grey")
rect(100,8,110,120, col="pink",border="grey")
rect(100,0,110,8,col=color[4],border="grey")
rect(110,35,120,525, col="pink",border="grey")
rect(110,0,120,35, col=color[4],border="grey")
rect(120,389,130,1063, col="pink",border="grey")
rect(120,0,130,389, col=color[4],border="grey")
rect(130,491,140,1133, col=color[4],border="grey")
rect(130,0,140,491, col="pink",border="grey")
rect(140,87,150,1829, col=color[4],border="grey")
rect(140,0,150,87, col="pink",border="grey")
rect(150,59,160,889, col=color[4],border="grey")
rect(150,0,160,59, col="pink",border="grey")
rect(160,32,170,163, col=color[4],border="grey")
rect(160,0,170,32, col="pink",border="grey")
rect(170,1,180,8, col="pink",border="grey")
rect(170,0,180,1, col=color[4],border="grey")
legend(55,1600,character(2),bty="n",pch=22,col=c(color[4],"pink"),pt.bg=c(color[4],"pink"))
savePlot(filename = "hist(Hb2006)",type = "png",device = dev.cur())






