2

I'm trying to force my legend to be in lower right corner AND flush against both axes. Right now, the legend defaults to being further inset into the plot than I want it to be (around the middle of the x-axis). I've poked around here and on R documentation and discovered that "inset=" is an optional argument that can be used in the legend function. I THINK this is how I can accomplish what I want. However, when I try only entering one numerical value (inset= 0), the legend ends up in the same place as the default. When I try to enter two numerical arguments (inset= c(0,-2)), the legend won't show up at all. I thought maybe I was using improper syntax and tried inset= 0:-2, but that didn't work either. I've tried playing around with higher values and that causes the legend to become further inset, so I know it's the right argument, but maybe I'm not giving it the right values? I've also tried changing the values for the "xjust=" and "yjust=" arguments and those don't seem to have any notable affect. Does anyone have advice on what I might be doing wrong? Attached are both my code and my current plot output (and arrows showing where I WANT the legend to be):

layout(matrix(c(2,1), 1, 2, byrow = FALSE))
par(pty="s", mar= c(1,3,1,3), mgp=c(1.5,0.5,0))

plot(x1, y1, 
     xlab="Total X1", 
     ylab="Total X2", 
     type= "n",
     ylim = c(-3,2),
     xlim = c(-2.5,2.5),
     cex.lab= 0.7,
     cex.axis= 0.7)
text(x1, y1, labels = SedCombined2$Site,
     col= c("#D55E00", "#000000", "#009E73", "#0072B2")[as.factor(SedCombined2$Event)],
     cex=0.7,
     font = 2)
legend("bottomright",
       border = "black",
       bty= "n",
       legend= c("November 2014", "April 2015","November 2015","April 2016"),
       text.col = c("#D55E00", "#000000", "#009E73", "#0072B2"),
       cex=0.6,
       x.intersp = 0.5,
       y.intersp = 0.5,
       inset = 0)

x2 <- fitSedCombinedISO$points[,1]
y2 <- fitSedCombinedISO$points[,2]
plot(x2, y2,
     xlab="Toxic X1", 
     ylab="Toxic X2", 
     type= "n",
     ylim = c(-3,2),
     xlim = c(-2.5,2.5),
     cex.lab= 0.7,
     cex.axis= 0.7)
text(x2, y2, labels = SedCombined2$Site,
     col= c("#D55E00", "#000000", "#009E73", "#0072B2")[as.factor(SedCombined2$Event)],
     cex=0.7,
     font = 2)
legend("bottomright",
       border = "black",
       bty= "n",
       legend= c("November 2014", "April 2015","November 2015","April 2016"),
       text.col = c("#D55E00", "#000000", "#009E73", "#0072B2"),
       cex=0.6,
       x.intersp = 0.5,
       y.intersp = 0.5,
       inset = 0)

Current Plot Output

  • 1
    Welcome to SO! Please read [ask] and https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example to make your example reproducible. – jogo Sep 26 '18 at 19:38
  • Inset with an argument of -2 would be placing completely out of the plot area. It's supposed to be the fraction of the plotregion, so values above 1 or below -1 wouldn't leave it in the plot region. – IRTFM Sep 26 '18 at 21:00
  • Yes, as @42 said: use a fractional value for the inset. I can't tell you which one to use, because your code isn't reproducible here, but start with something like `inset = c(-0.1, 0)`. – user2554330 Sep 26 '18 at 22:10
  • @jogo Apologies! I'll shortly rewrite the sample code so that it adheres to the "shortest code necessary" and so that it's more reproducible. – Christine K. Sep 27 '18 at 18:37

1 Answers1

0

This requires a little hack, assign your legend to a variable, and then use that to create a text box:

temp <- legend("bottomright",
       border = "black",
       bty= "n",
       legend=c('','','',''),
       text.width=max(strwidth(c("November 2014", "April 2015","November 2015","April 2016"))),
       text.col = c("#D55E00", "#000000", "#009E73", "#0072B2"),     
       xjust=1, yjust=1)

text(
    temp$rect$left + temp$rect$w, 
    temp$text$y, 
    c("November 2014", "April 2015","November 2015","April 2016"), 
    pos=2, 
    c("#D55E00", "#000000", "#009E73", "#0072B2"))

As a general point, if this level of control over plot appearance is important, I would highly recommend ggplot.

Josh Rumbut
  • 2,640
  • 2
  • 32
  • 43