issue with lining up lines and box in legend R

Viewed 65

I have a legend with lines and one box. I'm trying to get the box to line up with the lines. I have tried changing the first value in x.intersp=c(1, etc) to 0.5 and -0.5, but this seems to move the text and not the box. Any help would be appreciated.

Example code

x<-c(1,2,3,4)
y<-c(1,2,3,4)

plot(x,y)

mygray<-"gray"

#add legend
legend("bottomleft", legend = c("Survey Domain", "CA II Access Area","2020 SAMS Areas","EEZ"),
       bg="white",cex=1,
       bty="n",col=c(NA,"black","red","black"),
       lty = c(NA,1,1,1),
       fill=c(mygray,NA,NA,NA),
       lwd=c(NA,2,2,1),border = c("black",NA,NA,NA), 
       seg.len=0.8,y.intersp=1,x.intersp=c(1,1,1,1)) 
1 Answers

I think you have to do this in 2 steps to combine boxes and lines as they will appear beside one another rather than instead if both are specified. E.g.:

x <- c(1,2,3,4)
y <- c(1,2,3,4)

mygray <- "#cccccc"

plot(x,y)

#add legend
legend(
  "topleft",
  legend = c("a","b","c","d"),
  fill   = c(mygray,NA,NA,NA),
  border = c("black",NA,NA,NA),
  bty    = "n"
) 

legend(
  "topleft",
  legend = c("","","",""),
  col    = c(NA,"black","red","black"),
  lty    = c(NA,1,1,1),
  border = c("black",NA,NA,NA),
  bty    = "n",
  seg.len = 0.8
) 

Result:

enter image description here

Related