R作图:
1.高级指令与低级指令(交互式还未学就先不写)
2.参数的调整
常用高级指令:
plot(x) 以x的元素值为纵坐标、以序号为横坐标绘图
plot(x,y) x(在x-轴上)与y(在y-轴上)的二元作图
pie(x) 饼图
boxplot(x) 盒形图(“box-and-whiskers”)
hist(x) x的频率直方图
barplot(x) x的值的条形图
qqnorm(x) 正态分位数-分位数图
qqplot(x,y) y对x的分位数-分位数图
常用低级指令:(先有图才能用,这些指令给已有的图甜点架线加文字)
points(x, y) 添加点(可以使用选项type=)
lines(x, y) 同上,但是添加线
text(x, y, labels,…) 在(x,y)处添加用labels指定的文字;典型的用法是: plot(x, y, type=”n”); text(x, y, names)
segments(x0, y0,x1, y1) 从(x0,y0)各点到(x1,y1)各点画线段
arrows(x0, y0,x1, y1, angle= 30,code=2) 同上但加画箭头,如果code=2则在各(x0,y0)处画箭头,如 果code=1则在各(x1,y1)处画 箭头,如果code=3则在两端都画箭头; angle控制箭头轴到箭头边的角度
abline: abline(a,b) 绘制斜率为b和截距为a的直线
abline(h=y) 在纵坐标y处画水平线
abline(v=x) 在横坐标x处画垂直线
abline(lm.obj) 画由lm.obj确定的回归线
rect(x1, y1, x2, y2) 绘制长方形,(x1, y1)为左下角,(x2,y2)为右上角
polygon(x, y) 绘制连接各x,y坐标确定的点的多边形
legend: legend(x, y, legend) 在点(x,y)处添加图例,说明内容由legend给定
legend(location,title,legend,lty,pch,col) 一个图画多个函数时函数图例,location一般可用"bottom","bottomleft","top"之类的
title(main="",sub="") 添加标题,也可添加一个副标题
axis(side=, vect) 画坐标轴,side=1时画在下边,side=2时画在左边,side=3时画在上边,side=4时画在右边。可选参数at指定画刻度线的位置坐标
box() 在当前的图上加上边框
参数:
R作图有丰富的参数设置,用起来也很方便,常见的:
pch 点的符号,常用:1(默认),空心圆 2,三角形 3,+ 4 8,米 16,实心圆
cex 符号的大小,绘图符号与相对默认大小的缩放倍数
lty 线条类型 1,默认 2,虚线 3,点线
lwd 线条宽度,同cex
col 颜色
xlim,ylim 设置x轴y轴取值范围
应用时加在指令括号中,逗号隔开,例plot(x,y,pch=16,lty=2,xlim=c(0,20),ylim=c(0,20)
实例:
x <- c(1:10)
y <- x
z <- 10/x
plot(x, y, type = "b", pch = 21, col = "red", yaxt = "n",
lty = 3, ann = FALSE) %type="b"指描点连线
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")
legend("top",title="names",c("y=x","y=10/x"),lty=c(3,2),pch=c(21,22),col=c("red","blue")); %添加多个函数图例
abline(h=2); %添加参考线