6

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.

Community
  • 1
  • 1
Justyna
  • 737
  • 2
  • 10
  • 25

1 Answers1

6

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.

LyzandeR
  • 37,047
  • 12
  • 77
  • 87
  • I had to answer to your comment as an "answer to a question" so that I could attach images. – Justyna May 22 '15 at 22:42
  • 1
    @justyna Thanks for the reply. So, the problem is when you save it not when you plot it. Use `cex` to make your legend as small as you want (you see that in your output the right legend is smaller, i.e. the `cex` argument works) and then use the following to save your graph: `png(height=1200, width=1500, pointsize=15, file="mygraph.png")`. Play with the height and width according to how big you want the image and `pointsize` will take care of the text size inside the legend. Increase it for bigger size. A bit of playing around and you will make it as you want. Let me know how it goes. – LyzandeR May 23 '15 at 00:34
  • Also, I would suggest that you edit your question and add the info you provide in the answer there. Moderators will delete such an answer and you are likely to get downvotes (not from me) as well for an answer that is not really an answer. – LyzandeR May 23 '15 at 00:38
  • I edited my question to add the additional information. I didn't realize I can do that instead of writing the answer. Thank you for letting me know! I am still relatively new to this forum and I am trying to follow all the rules :) Thank you for your advice about saving with `png()`. I will definitely try to play with it tomorrow! – Justyna May 23 '15 at 00:51
  • Great! Let me know if it helps after you try it. Glad I could be of help and don't worry about it. We were all new to the site at some point :) – LyzandeR May 23 '15 at 00:58