0

I want to plot a legend outside of a plot area and it must be fixed in the same place starting under the x axis. I tried

 par(mar=c(5.1, 4.1, 3.1, 2.1))
 plot(rnorm(100,0,1),xlab="")
 legend("bottom", xjust=0.5,"this is a scatter plot",xpd=NA,bty="n",inset = 
 c(0,-0.2))

However when I enlarged the plot area and played with it the legend is broken sometimes it was inside the plot when the width is too low and sometimes it was too far awy from it. Is it a problem of R studio that I am using or from anything else like margins of the plot or changing height and width of plot pane... I have not tried it with R just only in R studio

I also tried with par("usr") however it was still degenerated when plot pane was enlarged.

How can I do this the legend will stay in the same place in plot pane? I edited the code. thanks a lot.

Cengover
  • 89
  • 13
  • You'll need to post a MRE (see https://stackoverflow.com/a/5963610/2554330) if you want help with this. – user2554330 Jan 28 '19 at 20:09
  • You can't. The `legend()` function calculates position based on the current display. If you change the size or shape of the display, it won't recalculate, it will just look wrong. – user2554330 Jan 28 '19 at 22:28
  • @user2554330 did I understand it correctly? legend is relocated according to the changing sizes of device width and height. Therefore, it is impossible to fix the location of the legend under x axis in the same location when the device size changes. I have to find the perfect plot by just playing with the pane??? – Cengover Jan 29 '19 at 07:57
  • First you should set the size of the plot to the final size, then draw the legend. If you change the size of the plot after that, the legend won't be positioned properly. This happens for all settings of `xpd`, but looks particularly bad when `xpd = NA`. For other settings, the plot region adapts to the changes, and the legend is drawn relative to the plot region. – user2554330 Jan 29 '19 at 11:21
  • when xpd is false you said. Right? then the legend wil be plotted in inside the plot area. – Cengover Jan 29 '19 at 12:11
  • No, I didn't say whether you should use `TRUE` or `FALSE`. – user2554330 Jan 29 '19 at 15:06

0 Answers0