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).
How can I achieve no overlapping legend for varying plot sizes (or width/height ratios)? I would highly appreciate help on this problem!