0

I am using the code as given below

The problem is there is very long space between legend line and legend text as shown in figure. kindly help how i can reduce this spacing. such as spacing between legend numbers and lines like this

________          1

--------          2

legend(x=q-70, y=m, bty = "n", 
  legend = c("1","2","3", "4","5", "6","7", "8","9", "10","11", "12","13", "14","15", "16","17", "18","19", "20"), 
  lty = c(1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,4,4,4,4), 
  col = c("black", "red","green","blue","cyan","magenta","yellow","gray","black", "red","green","blue","cyan","magenta","yellow","gray","black", "red","green","blue"),  
  y.intersp=0.7, cex = 0.95,seg.len = 1.3, lwd = 2)

enter image description here

  • I cannot reproduce this as-is. Can you make this question reproducible? That includes sample data (small) and the code to generate the plot as a whole. – r2evans May 17 '19 at 19:28
  • I dnt know how to reproduce a good question, kindly see the legend code only and see the spacing between lines and numbers in the legend in the picture, please – Sikandar Ali May 17 '19 at 19:32
  • 1
    Ditto. I ran your legend code on a blank plot and did _not_ get any large gap between the line and the label. – G5W May 17 '19 at 19:36
  • 1
    Good references for how to produce good questions include: https://stackoverflow.com/questions/5963269, https://stackoverflow.com/help/mcve, and https://stackoverflow.com/tags/r/info. – r2evans May 17 '19 at 20:40
  • But in general, try it from our perspective: start with a fresh, empty R session. Take all of the code you have given us and try to run it. It's close, but it does not work because we do not have an existing plot to start from. If I lead with `plot(1)` then the call to `legend` works and does not error (which is good) but does not reproduce your issue here. I suggest you try to reproduce this problem with different types of data; best-case for a very reproducible question is using a well-known dataset included in R or one of its more popular packages. – r2evans May 17 '19 at 20:42

1 Answers1

1

I notice that you have explicitly set y.intersp=0.7. I wonder if you have somehow set x.intersp as well. At any rate, if nothing else works for you, you could try various values of x.intersp until you get a position that you want. Here is a simple example on a blank background to show how x.intersp adjusts the position.

par(mfrow=c(1,2))
plot(0:2, pch="", xlab="", ylab="", bty="n", xaxt="n", yaxt="n", 
    main="x.intersp=1")
legend(x=1, y=2, bty = "n", 
  legend = c("1","2","3", "4","5","6","7","8","9","10",
    "11","12","13","14","15","16","17","18","19","20"), 
  lty = c(1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,4,4,4,4), 
  col = c("black", "red","green","blue","cyan","magenta","yellow",
    "gray","black", "red","green","blue","cyan","magenta","yellow",
    "gray","black", "red","green","blue"),  
    x.intersp=1, y.intersp=0.7,  cex = 0.95,seg.len = 1.3, lwd = 2)

plot(0:2, pch="", xlab="", ylab="", bty="n", xaxt="n", yaxt="n", 
    main="x.intersp=4")
legend(x=1, y=2, bty = "n", 
  legend = c("1","2","3", "4","5","6","7","8","9","10",
    "11","12","13","14","15","16","17","18","19","20"), 
  lty = c(1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,4,4,4,4), 
  col = c("black", "red","green","blue","cyan","magenta","yellow",
    "gray","black", "red","green","blue","cyan","magenta","yellow",
    "gray","black", "red","green","blue"),  
    x.intersp=4, y.intersp=0.7,  cex = 0.95,seg.len = 1.3, lwd = 2)

Different Spacing

G5W
  • 36,531
  • 10
  • 47
  • 80