R绘图基础(5)坐标轴,图例,标记与标题

相关参数来控制,它们有

参数描述
main主标题
sub副标题
xlabx轴标记
ylaby轴标记
xlimx轴上下限(范围)
ylimy轴上下限
mgp坐标轴标记,坐标字符,坐标刻度线距离坐标轴的行数,默认值为c(3,1,0)

增加一个新的坐标轴使用axis()函数。

参数描述
side坐标轴所在的位置,1:下,2:左,3:上,4:右
at坐标轴具体位置,通常由自动给出。
labels坐标字符串
pos坐标轴线所在的行,默认值为重绘所在位置上的原坐标
lty线型
col颜色
las坐标标记与坐标轴方位关系,=0为平等,=2为垂直
lwd.ticks坐标刻度线宽度
col.ticks坐标刻度线颜色
(…)其它par()中可用的参数
  1. > # A Silly Axis Example
  2. >
  3. > # specify the data
  4. > x <- c(1:10); y <- x; z <- 10/x
  5. >
  6. > # create extra margin room on the right for an axis
  7. > par(mar=c(5, 4, 4, 8) + 0.1)
  8. >
  9. > # plot x vs. y
  10. > plot(x, y,type="b", pch=21, col="red",
  11. + yaxt="n", lty=3, xlab="", ylab="")
  12. >
  13. > # add x vs. 1/x
  14. > lines(x, z, type="b", pch=22, col="blue", lty=2)
  15. >
  16. > # draw an axis on the left
  17. > axis(2, at=x,labels=x, col.axis="red", las=2)
  18. >
  19. > # draw an axis on the right, with smaller text and ticks
  20. > axis(4, at=z,labels=round(z,digits=2),
  21. + col.axis="blue", las=2, cex.axis=0.7, tck=-.01)
  22. >
  23. > # add a title for the right axis
  24. > mtext("y=1/x", side=4, line=3, cex.lab=1,las=2, col="blue")
  25. >
  26. > # add a main title and bottom and left axis labels
  27. > title("An Example of Creative Axes", xlab="X values",
  28. + ylab="Y=X")

R绘图基础(5)坐标轴,图例,标记与标题-图片1

坐标轴

图例使用legend()函数控制

参数描述
x,y图例所在位置,可以使用"bottom", "bottomleft", "left", "topleft" ,"top", "topright" ,"right" ,"bottomleft", "center"来指定。
inset设置在主绘图边距
title图例的标题
legend图例的内容
其它par()可用的参数
  1. > attach(mtcars)
  2. > boxplot(mpg~cyl, main="Milage by Car Weight",
  3. + yaxt="n", xlab="Milage", horizontal=TRUE,
  4. + col=terrain.colors(3))
  5. > legend("topright", inset=.05, title="Number of Cylinders",
  6. + c("4","6","8"), fill=terrain.colors(3), horiz=TRUE)

R绘图基础(5)坐标轴,图例,标记与标题-图片2

图例

文本框使用text或者mtext函数。text可以在主绘图区内加文本框,mtext在边距或者外边距上加文本框。

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()可用的参数
  1. > attach(mtcars)
  2. > plot(wt, mpg, main="Milage vs. Car Weight",
  3. + xlab="Weight", ylab="Mileage", pch=18, col="blue")
  4. > text(wt, mpg, row.names(mtcars), cex=0.6, pos=4, col="red")

R绘图基础(5)坐标轴,图例,标记与标题-图片3

文本框

发表评论

匿名网友

拖动滑块以完成验证
加载失败