调整水平图例中文本之间的间距 [英] Adjust spacing between text in horizontal legend

查看:109
本文介绍了调整水平图例中文本之间的间距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有水平图例的情节:

I have a plot with a horizontal legend:

 legend("bottomleft", inset = c(0, -0.3), bty = "n",
        x.intersp=0, xjust=0,yjust=0,
        legend=c("AAPL", "Information Technology",
                 "Technology Hardware and Equipment", "S&P 500"),
        col=c("black", "red", "blue3", "olivedrab3"),
        lwd=2, cex = 0.5, xpd = TRUE, ncol = 4)

问题在于,图例的第一项"AAPL"与第二项信息技术"之间有很大的间距.

The problem is that there is a huge spacing between the first item of the legend, "AAPL", and the second item "Information Technology".

我尝试使用txt.width()调整间距,但是根本不起作用.或者,也许我没有按照指示使用此选项.这就是我在legend()中引入txt.width选项的方式:

I tried adjusting the spacing using txt.width(), but it didn't work at all. Or maybe I am not using this option as directed. This is how I have introduced the txt.width option inside legend():

txt.width = c(2,1,1)

我不确定是否需要提及,但我的x轴是日期轴!

I am not sure if it relevant to mention, but my x-axis is an axis of dates!

是否有一种简单的方法可以自定义图例中文本之间的空格?

Is there an easy way of customizing the spaces between the text in the legend?

谢谢!

推荐答案

text.width可以让您控制图例中每一列的宽度,但这并不简单.基本上,text.width是一个向量,它将与图例字符串的向量一样长的另一个向量相乘.该第二向量的元素是从0length(legend)-1的整数.请参见编码为legend() 的代码有关血腥的细节.重要的是,您可以将text.width的乘积和第二个矢量视为图例元素的x坐标.然后,如果知道所需的x坐标,则可以计算需要在text.width参数中传递的内容.

text.width can give you control over the width of each column in your legend, but it's not straightforward. Basically, text.width is a vector that will be multiplied by another vector that is as long as your vector of legend strings. The elements of that second vector are integers from 0 to length(legend)-1. See the code to legend() for the gory details. The important thing is that you can think of this product of text.width and the second vector as, approximately, the x coordinates for your legend elements. Then if you know which x coordinates you want, you can calculate what needs to be passed in the text.width argument.

legtext <- c("AAPL", "Information Technology", 
             "Technology Hardware and Equipment", "S&P 500")
xcoords <- c(0, 10, 30, 60)
secondvector <- (1:length(legtext))-1
textwidths <- xcoords/secondvector # this works for all but the first element
textwidths[1] <- 0 # so replace element 1 with a finite number (any will do)

然后您的最终代码可能看起来像这样(除了我们不知道您的原始绘图数据或参数之外):

And then your final code could look something like this (except that we don't know your original plotting data or parameters):

plot(x=as.Date(c("1/1/13","3/1/13","5/1/13"), "%m/%d/%y"), y=1:3, ylim=c(0,3))

legend(x="bottomleft", bty = "n", x.intersp=0, xjust=0, yjust=0,
   legend=legtext, 
   col=c("black", "red", "blue3", "olivedrab3"), 
   lwd=2, cex = 0.5, xpd = TRUE, ncol = 4,
   text.width=textwidths)

如安德烈·席尔瓦(Andre Silva)所述,xcoordstextwidths中所需的值将取决于绘图的当前大小,为x轴指定的值范围等.

As Andre Silva mentioned, the values you'll want in xcoords and textwidths will depend on the current size of your plot, the range of values specified for your x axis, etc.

此外,如果每列有多个元素,则上面的secondvector看起来会有所不同.例如,对于具有两个图例元素的两个列,分别为secondvector == c(0,0,1,1).

Also, secondvector above would look different if you had more than one element per column. For example, for two columns with two legend elements apiece, secondvector == c(0,0,1,1).

这篇关于调整水平图例中文本之间的间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆