0

I have a problem with the external legend of a plot: here's the code:

x1<-read.csv("C:\\Users\\...\\x1.csv",sep=";")

plot(b~obs,data=x1,xlab="Observations",ylab="values", xlim=c(1,13), ylim=c(1,13),pch=1,cex=1)

points(x1$a,pch=20,cex=1)

For the legend I've tried this:

legend( x=15,y=10, xpd=TRUE, xjust=0, yjust=.5,
    legend=c("w/o FU", "w FU"),
    pch=c(1,20),cex=1)

and this:

legend( x="topleft",inset=c(1,0), xpd=TRUE,bty="n", 
    legend=c("w/o FU", "w FU"),
    pch=c(1,20),cex=1)

and this is what I obtain: enter image description here

The legend is hidded and I can't view it nor zooming neither expanding the window.

How can I fix it

Thank you!

ArTu
  • 431
  • 4
  • 20
  • 3
    Have you read the answers to this question? I'm pretty sure you can find your solution there: https://stackoverflow.com/questions/3932038/plot-a-legend-outside-of-the-plotting-area-in-base-graphics?rq=1 – divibisan Aug 03 '18 at 16:28
  • Yup, ArTu it will likely be a `par(mar=...)` thing. – r2evans Aug 03 '18 at 16:50
  • @divibisan of course I did but I can find how to put The legend outside the plot but not why I have the legend cut – ArTu Aug 03 '18 at 20:34
  • @r2evans what do you mean? I should use par to have more space even if I have only 1 plot? – ArTu Aug 03 '18 at 20:35
  • 1
    `par` sets many parameters for base R graphics, not just multiple plots. The `mar` and `mai` parameters set margin size. `?par` has a full listing of the graphical parameters you can change – divibisan Aug 03 '18 at 20:39
  • From [`?par`](https://stat.ethz.ch/R-manual/R-patched/library/graphics/html/par.html): *A numerical vector of the form 'c(bottom, left, top, right)' which gives the number of lines of margin to be specified on the four sides of the plot. The default is 'c(5, 4, 4, 2) + 0.1'.* – r2evans Aug 03 '18 at 20:53
  • Thank you! I'll study the mar and mai parameters!!! – ArTu Aug 13 '18 at 21:12

1 Answers1

0
plot(mpg~disp, data=mtcars)
legend(par("usr")[2], par("usr")[4], legend=c("Abc","Xyz"),pch=c(1,20), inset=c(0,0), xpd=TRUE)

bad margins

par(mar=c(5,4,4,4)+0.1) # default is c(5,4,4,2)+0.1
plot(mpg~disp, data=mtcars)
legend(par("usr")[2], par("usr")[4], legend=c("Abc","Xyz"),pch=c(1,20), inset=c(0,0), xpd=TRUE)

shifted margin

r2evans
  • 141,215
  • 6
  • 77
  • 149