R的legend参数设置

在作图的时候,legend很重要,在R中设置很繁琐,不像MATLAB有图形界面的,可以手动的设置。下面以?legend中例子学习一下legend参数。

 

#首先导入数据

>x <- seq(-pi, pi, len = 65)

> x

 [1] -3.14159265 -3.04341788 -2.94524311 -2.84706834 -2.74889357 -2.65071880

 [7] -2.55254403 -2.45436926 -2.35619449 -2.25801972 -2.15984495 -2.06167018

[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  0.00000000  0.09817477  0.19634954  0.29452431

[37]  0.39269908  0.49087385  0.58904862  0.68722339  0.78539816  0.88357293

[43]  0.98174770  1.07992247  1.17809725  1.27627202  1.37444679  1.47262156

[49]  1.57079633  1.66897110  1.76714587  1.86532064  1.96349541  2.06167018

[55]  2.15984495  2.25801972  2.35619449  2.45436926  2.55254403  2.65071880

[61]  2.74889357  2.84706834  2.94524311  3.04341788  3.14159265

 

>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  #添加点,pch控制点的符号类型

>lines(x, tan(x), type = "b", lty = 1, pch = 4, col = 6) #添加线

#画出的图形如下:

 

Description: 1111.jpeg

 

#这幅图是没有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),

       text.col = "green4", lty = c(2, -1, 1), pch = c(-1, 3, 4),

       merge = TRUE, bg = 'gray90')  

 

Description: 2222.jpeg

 

#下面解析legend

1.位置:x,y控制。即这里为-11.9.分别代表左边和上边的界限。可以改动试试

 

2.文字和lty即这里的sin,cos,tan. 接受一个向量。注意要和下面的参数lty(控制线型)对应。比如sin图中lty设置的是2cos?(没有理解为何是-1),tan1.legend中的c(“sin”,”cos”,”tan”)对应于lty中的lty=c(2,-1,1).  #是不是这里的cos是点,设置lty是没有用的

 

3.Textcol:指的是sin,cos,tan的字体颜色。若text.col=”red”,会发现他们变成红色了。

 

4.Pch: 控制线型,即sincos,tan的图标上对应的线.pchlty是有所不同的。Lty是控制连线线型的,即是实线,虚线,点划线等(当type=”l”时使用),而pch是控制符号的类型的,我的理解就是点的型状(当type=”p”使用)。假设所化的线是实线,在plot里设置pch是没有用的,因为没有点。一句话就是type=”l”等与线有关的时候设置lty;type=”p”,等与点有关的的时候,设置pch就有用了;当type=”o”等既有点又有线的时候,设置ltypch都是有用的。

 

注:对于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)  #type=”p”,设置pch有用。

 

Description: 5555.jpeg

 

> par(mfrow=c(1,2))

> plot(m,n,type="l",pch=2)  #如图,无点,设置pch没有用

> plot(m,n,type="p",lty=2)   #type是点,设置lty=2没用

Description: 6666.jpeg

 

> plot(m,n,type="o",lty=3,pch=4)

> plot(m,n,type="o",lty=2,pch=2)  #type=”o”,有点有线,所以lty,pch都有用。

Description: 7777.jpeg

 

 

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")  #边框消失了

Description: 8888.jpeg

 

byt=”o”时,box.lty=1,2,3..可以设置边框的型状,bg设置边框内部的填充颜色。当bty=”n”时候设置box.lty,bg是,没有用的。

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)

 

Description: 9999.jpeg

7:调整大小cex

默认值是1cex=0.1表明是10.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

Description: 99999.jpg

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")  #可以看到出现在了88的坐标位置,如下图

#也可以用方向

> legend("topright","test")

> legend("right","test")

> legend("downleft","test")

> legend("topleft","test")

Description: 999999.jpeg

 

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")

Description: 9999999.jpeg

 

 

 

 

#一些参考

http://stackoverflow.com/questions/8929663/r-legend-placement-in-a-plot

http://stackoverflow.com/questions/9366229/how-to-combine-a-plot-and-legend


转载自: http://blog.sina.com.cn/s/blog_6babbcb801019nqa.html#
  • 6
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值