Printing too big legend box in RStudio

Justyna picture Justyna · May 22, 2015 · Viewed 23.7k times · Source

I am trying to add a legend to my plot and I don't understand why I can't control for its size and/or location. I know there is a lot of posts about it but I already tried to reproduce the solutions and for whatever reason it does not seem to work in my RStudio. Here is what I tried:

  1. How to scale legend box or enlarge font size in the legend box in R

And here is how my plot looks like when I run the exact same code (you can see the legend is in the middle of the plot): my plot-1

I also tried to run some of the sample codes provided in R I also get weired looking plots. For instance, my plot for:

x <- seq(-pi, pi, len = 65)
plot(x, sin(x), type = "l", col = 2, xlab = expression(phi),
     ylab = expression(f(phi)))
abline(h = -1:1, v = pi/2*(-6:6), col = "gray90")
lines(x, cos(x), col = 3, lty = 2)
ex.cs1 <- expression(plain(sin) * phi,  paste("cos", phi))  # 2 ways
utils::str(legend(-3, .9, ex.cs1, lty = 1:2, plot = FALSE,
           adj = c(0, 0.6)))  # adj y !
legend(-3, 0.9, ex.cs1, lty = 1:2, col = 2:3,  adj = c(0, 0.6))

looks like this: my plot-2 and I don't know why. I try to change the cex and mar but it does not make any difference.

Do I need any extra packages to control the legend? (I loaded the library(graphics) but it does not make any difference.)

EDIT: I copy here my follow up question.

Hi Lyzander, thank you for your response. I actually did zoom in my plot and it looks exactly like on the linked figure. That figure is what I get when I save my plot to a png file. I reproduced your code and here is what get when I try to save it:

enter image description here

and this is how it looks like after zooming:

enter image description here

As you can see neither looks like what you get and I don't understand why. I have the latest version of R and I updated all my packages.

Answer

LyzandeR picture LyzandeR · May 22, 2015

Just use a keyword instead of specifying the exact coordinates and it will work better:

x <- seq(-pi, pi, len = 65)
plot(x, sin(x), type = "l", col = 2, xlab = expression(phi),
     ylab = expression(f(phi)))
abline(h = -1:1, v = pi/2*(-6:6), col = "gray90")
lines(x, cos(x), col = 3, lty = 2)
ex.cs1 <- expression(plain(sin) * phi,  paste("cos", phi))  # 2 ways
utils::str(legend(-3, .9, ex.cs1, lty = 1:2, plot = FALSE,
                  adj = c(0, 0.6)))  # adj y !
legend('topleft', ex.cs1, lty = 1:2, col = 2:3,  adj = c(0, 0.6))

In this case I used the topleft keyword as you can see and it looks great:

enter image description here

And if you specify cex it does make the legend smaller as you can see below:

x <- seq(-pi, pi, len = 65)
plot(x, sin(x), type = "l", col = 2, xlab = expression(phi),
     ylab = expression(f(phi)))
abline(h = -1:1, v = pi/2*(-6:6), col = "gray90")
lines(x, cos(x), col = 3, lty = 2)
ex.cs1 <- expression(plain(sin) * phi,  paste("cos", phi))  # 2 ways
utils::str(legend(-3, .9, ex.cs1, lty = 1:2, plot = FALSE,
                  adj = c(0, 0.6)))  # adj y !
legend('topleft', ex.cs1, lty = 1:2, col = 2:3,  adj = c(0, 0.6))
legend('topright', ex.cs1, lty = 1:2, col = 2:3,  adj = c(0, 0.6), cex=0.75)

enter image description here

Also, when you look at graphs in Rstudio make sure you hit the zoom button. It is more representative of what the output is.