R画图基本(一)机关颜色等——2
四,标记与线形
绘图中的标记由pch参数来节制。其描边色由col节制,填充色由bg节制。
> pchShow <- + function(extras = c("*",".", "o","O","0","+","-","|","%","#"), + cex = 3, ## good for both .Device=="postscript" and "x11" + col = "red3", bg = "gold", coltext = "brown", cextext = 1.2, + main = paste("plot symbols : points (... pch = *, cex =", + cex,")")) + { + nex <- length(extras) + np <- 26 + nex + ipch <- 0:(np-1) + k <- floor(sqrt(np)) + dd <- c(-1,1)/2 + rx <- dd + range(ix <- ipch %/% k) + ry <- dd + range(iy <- 3 + (k-1)- ipch %% k) + pch <- as.list(ipch) # list with integers & strings + if(nex > 0) pch[26+ 1:nex] <- as.list(extras) + plot(rx, ry, type="n", axes = FALSE, xlab = "", ylab = "", + main = main) + abline(v = ix, h = iy, col = "lightgray", lty = "dotted") + for(i in 1:np) { + pc <- pch[[i]] + ## 'col' symbols with a 'bg'-colored interior (where available) : + points(ix[i], iy[i], pch = pc, col = col, bg = bg, cex = cex) + if(cextext > 0) + text(ix[i] - 0.3, iy[i], pc, col = coltext, cex = cextext) + } + } > > pchShow()
线形主要由lty和lwd来节制。lty: line type. lwd: line width.顾名思意,lty节制线的形状,而lwd节制线的粗细,默认值为1。设计成2暗示两倍线宽。
五,坐标轴,图例,标志与标题
相关参数来节制,它们有
参数 | 描写 |
main | 主标题 |
sub | 副标题 |
xlab | x轴标志 |
ylab | y轴标志 |
xlim | x轴上下限(范畴) |
ylim | y轴上下限 |
mgp | 坐标轴标志,坐标字符,坐标刻度线间隔坐标轴的行数,默认值为c(3,1,0) |
增加一个新的坐标轴利用axis()函数。
参数 | 描写 |
side | 坐标轴地址的位置,1:下,2:左,3:上,4:右 |
at | 坐标轴详细位置,凡是由自动给出。 |
labels | 坐标字符串 |
pos | 坐标轴线地址的行,默认值为重绘地址位置上的原坐标 |
lty | 线型 |
col | 颜色 |
las | 坐标标志与坐标轴方位干系,=0为平等,=2为垂直 |
lwd.ticks | 坐标刻度线宽度 |
col.ticks | 坐标刻度线颜色 |
(…) | 其它par()中可用的参数 |
> # A Silly Axis Example > > # specify the data > x <- c(1:10); y <- x; z <- 10/x > > # create extra margin room on the right for an axis > par(mar=c(5, 4, 4, 8) + 0.1) > > # plot x vs. y > plot(x, y,type="b", pch=21, col="red", + yaxt="n", lty=3, xlab="", ylab="") > > # add x vs. 1/x > lines(x, z, type="b", pch=22, col="blue", lty=2) > > # draw an axis on the left > axis(2, at=x,labels=x, col.axis="red", las=2) > > # draw an axis on the right, with smaller text and ticks > axis(4, at=z,labels=round(z,digits=2), + col.axis="blue", las=2, cex.axis=0.7, tck=-.01) > > # add a title for the right axis > mtext("y=1/x", side=4, line=3, cex.lab=1,las=2, col="blue") > > # add a main title and bottom and left axis labels > title("An Example of Creative Axes", xlab="X values", + ylab="Y=X")
图例利用legend()函数节制
参数 | 描写 |
x,y | 图例地址位置,可以利用”bottom”,”bottomleft”,”left”,”topleft”,”top”,”topright”,”right”,”bottomleft”,”center”来指定。 |
inset | 配置在主画图边距 |
title | 图例的标题 |
legend | 图例的内容 |
… | 其它par()可用的参数 |
> attach(mtcars) > boxplot(mpg~cyl, main="Milage by Car Weight", + yaxt="n", xlab="Milage", horizontal=TRUE, + col=terrain.colors(3)) > legend("topright", inset=.05, title="Number of Cylinders", + c("4","6","8"), fill=terrain.colors(3), horiz=TRUE) |
文本框利用text可能mtext函数。text可以在主画图区内加文本框,mtext在边距可能外边距上加文本框。
#p#分页标题#e#
text(location, “text to place”, pos, …)
mtext(“text to place”, side, line=n, …)
参数 | 描写 |
location | 图例地址位置 |
pos | 地址的相对位置,1:下面,2:左边,3:上面,4:右边 |
side | 地址边距的位置,1:下,2:左,3:上,4:右 |
… | 其它par()可用的参数 |
> attach(mtcars) > plot(wt, mpg, main="Milage vs. Car Weight", + xlab="Weight", ylab="Mileage", pch=18, col="blue") > text(wt, mpg, row.names(mtcars), cex=0.6, pos=4, col="red") |
1234下一页