我在一个情节中有一个传说,一条线(来自一个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)
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).