# 02图形初阶

axis()

`x <- c(1:10)y <- xz <- 10/xopar <- par(no.readonly = TRUE)par(mar = c(5, 4, 4, 8) + 0.1)plot(x, y, type = "b", pch = 21, col = "red", yaxt = "n",lty = 3, ann = FALSE)lines(x, z, type = "b", pch = 22, col = "blue", lty = 2)axis(2, at = x, labels = x, col.axis = "red", las = 2)axis(4, at = z, labels = round(z, digits = 2), col.axis = "blue",las = 2, cex.axis = 0.7, tck = -0.01)mtext("y=1/x", side = 4, line = 3, cex.lab = 1, las = 2,col = "blue")title("An Example of Creative Axes", xlab = "X values",ylab = "Y=X")par(opar)`

Hmisc包的minor.tick()函数

`library(Hmisc)minor.tick(nx=2,ny=3,tick.ratio=0.5)`

`abline(h=c(1,5,7),v=seq(1,10,2),lty=2,col="blue")`

h为水平参考线，v为垂直参考线（在1、3、5、7、9的位置）

`legend("topleft", inset = 0.05, title = "Drug Type",c("A", "B"), lty = c(1, 2), pch = c(15, 17), col = c("red","blue"))`

inset distance(s) from the margins as a fraction of the plot region when legend is placed by keyword.

text(location,"text to place",pos,...)向绘图区内部添加文本。location可为一对x,y坐标，或location(1)用鼠标确定。

mtext("text to place",side,line=n,...)向图形四边之一添加文本。

par()

`attach(mtcars)opar <- par(no.readonly = TRUE)par(mfrow = c(2, 2))plot(wt, mpg, main = "Scatterplot of wt vs. mpg")plot(wt, disp, main = "Scatterplot of wt vs disp")hist(wt, main = "Histogram of wt")boxplot(wt, main = "Boxplot of wt")par(opar)detach(mtcars)`

mfrow=c(2,2)表示2行2列按行填充，mfcol=c(2,2)表示2行2列按列填充。

layout()

`attach(mtcars)layout(matrix(c(1, 1, 2, 3), 2, 2, byrow = TRUE),widths = c(3, 1), heights = c(1, 2))hist(wt)hist(mpg)hist(disp)detach(mtcars)`

widths和heights表示各列的相对宽度和相对高度，第一行宽占比3、第二行宽占比1，同理第一列高占比1、第二列高占比2。绝对宽度用lcm()来指定，如widths=lcm(5)。

fig()

`opar <- par(no.readonly = TRUE)par(fig = c(0, 0.8, 0, 0.8))plot(mtcars\$wt, mtcars\$mpg, xlab = "Miles Per Gallon",ylab = "Car Weight")par(fig = c(0, 0.8, 0.55, 1), new = TRUE)boxplot(mtcars\$wt, horizontal = TRUE, axes = FALSE)par(fig = c(0.65, 1, 0, 0.8), new = TRUE)boxplot(mtcars\$mpg, axes = FALSE)mtext("Enhanced Scatterplot", side = 3, outer = TRUE,line = -3)par(opar)`

Top