Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: positioning of the legend and length of the lines in the legend

I can't set the location of the legend properly, I have tried to play around with xjust and ncol but it still appears in the middle of the graphic.

Can you help it align it to the border (left or right I do not care)?

Moreover is it possible to draw shorter lines in the legend? These appear too long.

 par(mar=c(5,4,4,5)+.1)
 plot(1:109,rnorm(109), type="l",col="blue", lwd=2, 
      main="Compare signal and std deviation MC3 ch44",
      ylab="signal", xlab="locations")
 par(new=TRUE)
 plot(1:109, rnorm(109), ylab="",xlab="", type="l", 
      col="red",xaxt="n",yaxt="n", lwd=2)
 axis(4)
 mtext("std. deviation",side=4,line=3)
 legend("topright",col=c("blue", "red"), lwd=2,
        legend=c("signal","std dev"), cex=0.85, bty="n", xjust=1)
like image 995
Irene Avatar asked Oct 14 '13 08:10

Irene


People also ask

How do I change the position of my legend in R?

position. You can place the legend literally anywhere. To put it around the chart, use the legend. position option and specify top , right , bottom , or left .

How do I change the size of a legend in R?

To change the legend size of the plot, the user needs to use the cex argument of the legend function and specify its value with the user requirement, the values of cex greater than 1 will increase the legend size in the plot and the value of cex less than 1 will decrease the size of the legend in the plot.

How do I change the position of a graph in R?

By default, the position of X-axis is at the bottom and we know that it is always there in all software/programming tools. If we want to change the position of X-axis in base R plot then we can use axis function with pos argument. The pos argument will decide the position of the X-axis on Y-axis.

What is a legend position?

LegendPosition. is an option for Legend that specifies the location of the lower-left corner of a legend box.


1 Answers

As pointed out in the comments restarting an R session and using

legend("topright",col=c("blue", "red"), lwd=2,
 legend=c("signal","std dev"), cex=0.85, bty="n", xjust=1, seg.len=0.5)

solved my problems.

like image 177
Irene Avatar answered Sep 21 '22 10:09

Irene