plot legends without border and with white background

biotom picture biotom · Apr 11, 2012 · Viewed 138.3k times · Source

I have a legend in a plot with a line (from an abline-statement) going through it. How can I achieve that the abline gets invisible in proximity of the legend? This should be achievable by setting the legend background white, without borders, but how can I achieve this? Assume the graph should look like this:

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

And to get it a bit more complicated: If the legend interferes with the dots of the dot-plot: How can I achieve that the ablines gets invisible in proximity of the legend (as above), but that the dots are still visible?

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

And finally: Is there a way to introduce line-breaks in legend statements?

Answer

mayank picture mayank · Apr 7, 2013

Use option bty = "n" in legend to remove the box around the legend. For example:

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