在作图的时候,legend很重要,在R中设置很繁琐,不像MATLAB有图形界面的,可以手动的设置。下面以?legend中例子学习一下legend参数。
#首先导入数据
>x <- seq(-pi, pi, len = 65)
> x
[13] -1.96349541 -1.86532064 -1.76714587 -1.66897110 -1.57079633 -1.47262156
[19] -1.37444679 -1.27627202 -1.17809725 -1.07992247 -0.98174770 -0.88357293
[25] -0.78539816 -0.68722339 -0.58904862 -0.49087385 -0.39269908 -0.29452431
[31] -0.19634954 -0.09817477
[37]
[43]
[49]
[55]
[61]
>plot(x, sin(x), type = "l", ylim = c(-1.2, 1.8), col = 3, lty = 2)
#lty指定线型状
>points(x, cos(x), pch = 3, col = 4) #plot sin
>lines(x, tan(x), type = "b", lty = 1, pch = 4, col = 6) #添加线
#画出的图形如下:
#这幅图是没有legend的,下面设置:
#先看
legend(x, y = NULL, legend, fill = NULL, col = par("col"),
border="black", lty, lwd, pch,
angle = 45, density = NULL, bty = "o", bg = par("bg"),
box.lwd = par("lwd"), box.lty = par("lty"), box.col = par("fg"),
pt.bg = NA, cex = 1, pt.cex = cex, pt.lwd = lwd,
xjust = 0, yjust = 1, x.intersp = 1, y.intersp = 1,
adj = c(0, 0.5), text.width = NULL, text.col = par("col"), text.font = NULL,
merge = do.lines && has.pch, trace = FALSE,
plot = TRUE, ncol = 1, horiz = FALSE, title = NULL,
inset = 0, xpd, title.col = text.col, title.adj = 0.5,
seg.len = 2)
#设置
> legend(-1, 1.9, c("sin", "cos", "tan"), col = c(3,4,6),
+
+
#下面解析legend
1.位置:由x,y控制。即这里为-1,1.9.分别代表左边和上边的界限。可以改动试试
2.文字和lty:即这里的sin,cos,tan.
3.Textcol:指的是sin,cos,tan的字体颜色。若text.col=”red”,会发现他们变成红色了。
4.Pch:
注:对于pch,lty,和type。对于plot等是一样的道理
> m=seq(1,10)
> n=m
> par(mfrow=c(1,2))
> plot(m,n,type="p",pch=2)
> plot(m,n,type="p",pch=3)
> par(mfrow=c(1,2))
> plot(m,n,type="l",pch=2)
> plot(m,n,type="p",lty=2)
> plot(m,n,type="o",lty=3,pch=4)
> plot(m,n,type="o",lty=2,pch=2)
5.col,控制图标中的线的颜色
6设置图标的边框等
bty=”o”默认的,=”n”表示没有边框,
> legend(-1, 1.9, c("sin", "cos", "tan"), col = c(3,4,6),text.col = "green4", lty = c(2, -1, 1), pch = c(-1, 3, 4),merge = TRUE, bg = 'gray90',bty="n")
当byt=”o”时,box.lty=1,2,3..可以设置边框的型状,bg设置边框内部的填充颜色。当bty=”n”时候设置box.lty,bg是,没有用的。
>
7:调整大小cex
默认值是1,cex=0.1表明是1的0.1倍,cex=1.2表明是默认的一点2倍
> x <- seq(-pi, pi, len = 65)
> plot(x, sin(x), type = "l", ylim = c(-1.2, 1.8), col = 3, lty = 2)
> points(x, cos(x), pch = 3, col = 4)
> lines(x, tan(x), type = "b", lty = 1, pch = 4, col = 6)
> legend(-1, 1.9, c("sin", "cos", "tan"), col = c(3,4,6),text.col = "green4", lty = c(2, -1, 1), pch = c(-1, 3, 4),merge = TRUE, bg = 'red',bty="o",box.lty=2)
> legend(-1, 1.0, c("sin", "cos", "tan"), col = c(3,4,6),text.col = "green4", lty = c(2, -1, 1), pch = c(-1, 3, 4),merge = TRUE, bg = 'red',bty="o",box.lty=2,cex=1.2)
> legend(-1, 0.0, c("sin", "cos", "tan"), col = c(3,4,6),text.col = "green4", lty = c(2, -1, 1), pch = c(-1, 3, 4),merge = TRUE, bg = 'red',bty="o",box.lty=2,cex=0.5)
#观察其中的cex
8.设置相对的图标位置,也可以是”topright”等
试想,x,y都是在变的,设置legend的定位坐标时,未免麻烦。可以这样做max(x)-2
,max(y)-2
> x=seq(1,10)
> y=x
> plot(x,y)
> legend(max(x)-2,max(y)-2,"test")
#也可以用方向
> legend("topright","test")
> legend("right","test")
> legend("downleft","test")
> legend("topleft","test")
9.柱状图等怎么标注:bty=”n”, pch=15 (代表正方形,填上相应的颜色就好了)
> plot(x,y)
> legend(5,9,"test",bty="n",pch=15)
> legend(5,8,"test",bty="n",pch=15,col="red")
#一些参考
http://stackoverflow.com/questions/8929663/r-legend-placement-in-a-plot
http://stackoverflow.com/questions/9366229/how-to-combine-a-plot-and-legend