情节传说没有边框和白色背景

bio*_*tom 51 plot r legend

我在一个情节中有一个传说,一条线(来自一个abline-statement)经过它.如何在传奇附近看到abline隐形?这应该可以通过将图例背景设置为白色而没有边框来实现,但是我该如何实现呢?假设图形应如下所示:

windows.options(width=30, height=12)
plot(1:10)
abline(v=seq(1,10,1), col='grey', lty='dotted')
legend(4.8, 3, "This legend text should not be disturbed by the dotted grey lines")
Run Code Online (Sandbox Code Playgroud)

并且让它变得更复杂:如果图例干扰了点图的点:我怎样才能实现在图例附近看到的ablines不可见(如上所示),但点仍然可见?

windows.options(width=30, height=12)
plot(1:10)
abline(v=seq(1,10,1), col='grey', lty='dotted')
legend(1, 5, "This legend text should not be disturbed by the dotted grey lines, but the plotted dots should still be visible")
Run Code Online (Sandbox Code Playgroud)

最后:有没有办法在图例语句中引入换行符?

小智 97

使用选项bty = "n"legend删除图例周围的框.例如:

legend(1, 5,
       "This legend text should not be disturbed by the dotted grey lines,\nbut the plotted dots should still be visible",
       bty = "n")
Run Code Online (Sandbox Code Playgroud)

  • 这是正确的答案.如果R文档实际上字面上解释了bty ='n'将导致没有绘制图例框,那将是很好的.相反,它只是说"n"是其中一个选项而不解释该选项的效果.我想知道R维护者是否会允许像我这样的人改进标准文档,例如这样. (3认同)

jor*_*ran 23

正如?legend您所记录的那样,这样做:

plot(1:10,type = "n")
abline(v=seq(1,10,1), col='grey', lty='dotted')
legend(1, 5, "This legend text should not be disturbed by the dotted grey lines,\nbut the plotted dots should still be visible",box.lwd = 0,box.col = "white",bg = "white")
points(1:10,1:10)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

使用新行字符实现换行符\n.只需更改绘图顺序即可使点仍然可见.请记住,在R中绘图就像在一张纸上绘图:你绘制的每一件事都会放在当前的任何东西之上.

请注意,图例文本被截断,因为我使绘图尺寸更小(所有R平台上都不存在windows.options).

  • 这是比bty ='n'更好的答案,因为它保留了白色bg,这是原始问题所要求的.对于第二点,图例覆盖了线而不是点,我能看到的唯一解决方案是在图例线后面添加一个points()命令. (3认同)
  • box.lwd = 0会正常工作.bty ='n'可能更好,因为它意味着图例框类型完全没有.两个好答案! (2认同)