Anthony Hauser
Anthony Hauser

Reputation: 719

Legend in R: remove left space go to the line

To explain my plots, I'm using the legend() function in R. The problem is that my explanations are long and thus take more than one line. How to automatically go to the next line at the end of the line? How to remove the left space between the left border and the beginning of the text? Here is my script:

m <- matrix(c(1,2,3,3),nrow = 2,ncol = 2,byrow = TRUE) layout(mat = 
m,heights = c(0.5,0.5),widths=c(0.5,0.5))
par(mai=c(0.2,0.8,0.5,0.4)) plot(1:10,1:10)
par(mai=c(0.2,0.8,0.5,0.4)) plot(1:10,1:10)
par(mai=c(0,0,0,0)) plot.new()
legend(x="left","legend legend legend legend legend legend legend legend legendlegend legend legend legend legend legend legend legend legend legend legend legend legendlegend legend legend legend",cex=2)

Here is the plot:

enter image description here

Upvotes: 1

Views: 356

Answers (1)

P. Paccioretti
P. Paccioretti

Reputation: 414

In R with "\n" you go to the next line, you can split your long text with strwrap function and use paste like in https://stackoverflow.com/a/7367534/10263697. With legend(..., x.intersp = 0) you remove the left space in the legend.

m <- matrix(c(1,2,3,3),nrow = 2,ncol = 2,byrow = TRUE) 
layout(mat = 
           m,heights = c(0.5,0.5),widths=c(0.5,0.5))
par(mai=c(0.2,0.8,0.5,0.4)) 
plot(1:10,1:10)
par(mai=c(0.2,0.8,0.5,0.4)) 
plot(1:10,1:10)
par(mai=c(0,0,0,0)) 
plot.new()
legend(x="left",paste(strwrap("legend legend legend legend legend legend legend legend legendlegend legend legend legend legend legend legend legend legend legend legend legend legendlegend legend legend legend", width = 0.3 * getOption("width")), sep = "\n")
,cex=2, x.intersp = 1)

Upvotes: 1

Related Questions