r - 使用“Regend outside the plot in R"中的答案,但仍然存在一些问题 [英] r - Using an answer from "Legend outside the plot in R", but it still has some problems

查看:39
本文介绍了r - 使用“Regend outside the plot in R"中的答案,但仍然存在一些问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现此

我无法共享数据.只需将其视为 data.frames 中的列:df1、df2、df12.

生成图像的代码如下:

 add_legend <- function(...) {opar <- par(fig=c(0, 1, 0, 1), oma=c(0, 0, 0, 0),mar=c(0, 0, 0, 0), 新=真)on.exit(par(opar))情节(0, 0, 类型='n', bty='n', xaxt='n', yaxt='n')传奇(...)}op <- par(cex = 1)#公元前情节(df1[,2],df2[,1],xlab="save",ylab="log85",ylim=c(6, 10))#bc2点(df1[,2],df2[,3],xlab="save",ylab="log85",col=2)#od点(df1[,2],df1[,1],pch=3,col=3)#od2点(df12[,2],df12[,1],pch=3)add_legend("top", legend=c("文字太大","描述","很多","无用的自由空间"),col=c(1, 2,3,1),pch=c(1,1,3,3),horiz=TRUE, bty='n', x.intersp = 0.3)

我想将图例放在描述之间的可用空间较小且字体较大的位置.我试过使用 x.intersp 但它只是减少了符号与其描述之间的距离,而不是前一个描述和下一个符号之间的距离.如果我在绘图之前使用 par(cex=1) ,那么字体大小很好,但更多的文本被剪掉了.

任何帮助将不胜感激.

解决方案

在当前的 R 版本 (3.5.0) 中不能这样做.

如果您指定 horiz=TRUE 或 nc>1,则每个图例条目之间的水平距离将始终相等,并且每个条目之间的间距将由最长的条目确定.如果项目的数量跨越多列和多行,这在美学上很重要,但在您的示例中看到多列且只有一行时,这一点就不那么重要了.正如您巧妙地建议的那样,如果项目的长度不相等,结果可能会变得相当丑陋并浪费空间.不幸的是,似乎没有一个参数来手动控制每个项目之间的距离.因此,您遇到了 R 的限制之一.一种解决方法是自己破解图例函数并找到代码的哪一部分放置图例条目.我曾经修改过它,让您可以相对于图例的其他部分调整填充框的大小.我注意到图例函数的参数数量随着每个新的主要 R 升级而增加,例如,seg.len 参数是最近添加的,允许行长度为调整.也许在未来的版本中,会有一个新参数允许您手动指定图例条目之间的水平间距(并调整填充框的大小).:)

也许这里最好的选择是指定 nc=2 而不是 horiz=TRUE.

编辑:如果每个图例条目之间的空间太大,text.width 参数也可能有所帮助,但同样,每个项目之间的距离必须相等.

I'm trying to implement one of the answers in this question

I'm using the third answer, the one with the function new_legend. Here's my output.

I cannot share the data. Just think of it as columns in data.frames: df1, df2, df12.

The code that produces the image is the following:

    add_legend <- function(...) {
      opar <- par(fig=c(0, 1, 0, 1), oma=c(0, 0, 0, 0), 
                  mar=c(0, 0, 0, 0), new=TRUE)
      on.exit(par(opar))
      plot(0, 0, type='n', bty='n', xaxt='n', yaxt='n')
      legend(...)
    }

op <- par(cex = 1)
#bc
plot(df1[,2],df2[,1],xlab="save",ylab="log85",ylim=c(6, 10))
#bc2
points(df1[,2],df2[,3],xlab="save",ylab="log85",col=2)
#od
points(df1[,2],df1[,1],pch=3,col=3)
#od2
points(df12[,2],df12[,1],pch=3)
add_legend("top", legend=c("too big text", "description with","a lot of" ,"useless freespace"),
           col=c(1, 2,3,1),pch=c(1,1,3,3),horiz=TRUE, bty='n', x.intersp = 0.3)

I would like to put the legend with less free space between descriptions and with a bigger font size. I've tried using x.intersp but it just diminishes the distance between the symbol and its description, and not the distance between a previous description and the next symbol. If I use par(cex=1) before doing the plot, then the font size is good, but more text is clipped.

Any help would be appreciated.

解决方案

You can't do this in the current R version (3.5.0).

If you specify horiz=TRUE or nc>1, then the horizontal distance between each legend entry will always be equal and the space between each item will be determined by the longest item. This is important aesthetically if the number of items spans across multiple columns and rows, but not so important when there are multiple columns and only one row as seen in your example. The result can, as you subtly suggest, become rather ugly and appear to waste space if the lengths of the items are not equal. Unfortunately, there doesn't appear to be an argument to manually control the distance between each item. So you've hit one of the limitations of R. One workaround is to hack the legend function yourself and find what part of the code places the legend entries. I once hacked it to allow you to adjust the size of the filled boxes relative to the other parts of the legend. I've noticed that the number of arguments to the legend function has been growing with each new major R upgrade, for example, the seg.len argument is a fairly recent addition to allow the line lengths to be adjusted. Perhaps in a future version, there will be a new argument that allows you to manually specify the horizontal spacing between legend entries (and adjust the size of the filled boxes). :)

Maybe your best option here is to specify nc=2 instead of horiz=TRUE.

Edit: The text.width argument may also help if there is too much space between each legend entry, but again, the distance between each item must be equal.

这篇关于r - 使用“Regend outside the plot in R"中的答案,但仍然存在一些问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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