Printing too large a window in RStudio
I am trying to add a legend to my plot and I don't understand why I can't control its size and / or location. I know there are many posts in it, but I've already tried to reproduce the solutions and for some reason doesn't seem to work in my RStudio. Here's what I've tried:
And this is what my plot looks like when I run the same code (you can see the legend is in the middle of the plot): my plot-1
I also tried running some of the code examples provided in RI to get the perspective plots drawn as well. For example 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 is 2 and I don't know why. I'm trying to change cex
and mar
, but it doesn't make any difference.
Do I need additional packages to manage the legend? (I downloaded library(graphics)
, but it doesn't make any difference.)
EDIT : I am copying my next question here.
Hi Liskandre, thanks for your answer. I actually zoomed in my plot and it looks exactly the same as the linked figure. This figure is what I get when I save my plot in a png file. I have reproduced your code, and this is what happens when I try to save it:
and this is how it looks after scaling:
As you can see, neither one nor the other looks like what you are getting, and I do not understand why. I have the latest version of R and have updated all my packages.
source to share
Just use keyword
instead of specifying 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 keyword topleft
, as you can see, and it looks great:
And if you specify cex
it will shrink the legend like 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)
Also, when looking at graphs in Rstudio, make sure you click the zoom button. It is more representative of the output.
source to share