0

I want to produce a simple plot with a legend on the top right above the plot (see picture below, plot on the left). I can achieve this by using the following simple example:

par(mar = c(4, 4, 1.5, 4))
plot(rnorm(50))
par(oma = c(0, 0, 0, 0), mar = c(0, 0, 0, 4), new = TRUE)
plot(0, 0, type = "n", bty = "n", axes = F)
legend(x = "topright", "data", bty = "n", pch = 1, xpd = T, horiz = T)

When I scale this plot to different sizes the legend sometimes overlaps the plot area though (plot on the right). Plot in different sizes. Note the legend overlapping the plot area on the right side of the figure.

How can I achieve no overlapping legend for varying plot sizes (or width/height ratios)? I would highly appreciate help on this problem!

oepix
  • 151
  • 2
  • 12
  • 2
    Try this approach http://stackoverflow.com/questions/3932038/plot-a-legend-outside-of-the-plotting-area-in-base-graphics. – 67342343 Mar 28 '17 at 13:57
  • 4
    `legend` does not play well with resizing window after plotting. It's better to define the plot size before using `pdf` or `windows` – d.b Mar 28 '17 at 13:57
  • 1
    Oh, so defining the plot size eliminates the problem, since the "line width" for the margins will be fixed? That works for me, since I want to define a specific size anyway. Thanks for the help! – oepix Mar 28 '17 at 14:07

0 Answers0