Hi I'm trying to get the legend to not overlap with my plot but the solutions I've found on stackoverflow don't seem to be working right now, using the following random data
par(mar = c(3,1,1,1))
plot(1:3, rnorm(3), pch=1, lty=1, type="o", ylim=c(-2,2))
legend("bottom", legend = c("a","b"), horiz=T,
bty = "n", pch=15 , col= c("green","red"), text.col = "black",
cex=1, pt.cex=1,xpd=T)
So this graph has the legend overlapping with the graph even though I have the xpd = T
argument and I also tried putting in the inset=c(0,-0.5)
argument but the legend then disappears (can't even see it) so I was wondering what is wrong with the code and how to get the legend to not overlap, thanks a lot!
CodePudding user response:
You may need to add par(xpd=TRUE)
to enable to drawn outside the graph and add "bottom" for x and an inset. You can use the following code:
par(mar = c(3,1,1,1))
plot(1:3, rnorm(3), pch=1, lty=1, type="o", ylim=c(-2,2))
par(xpd = TRUE)
legend(x="bottom", inset=-0.15, legend = c("a","b"), horiz=T,
bty = "n", pch=15 , col= c("green","red"), text.col = "black",
cex=1, pt.cex=1)
Created on 2022-07-23 by the reprex package (v2.0.1)