在RStudio中打印太大的图例框 [英] Printing too big legend box in RStudio

查看:499
本文介绍了在RStudio中打印太大的图例框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在图中添加图例,但我不明白为什么我无法控制其大小和/或位置.我知道有很多关于它的文章,但是我已经尝试重现解决方案,无论出于什么原因,它似乎都无法在我的RStudio中使用.这是我尝试过的:

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. 如何在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

我还尝试运行R中提供的一些示例代码,而且看起来也很怪异.例如,我的情节:

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))

看起来像这样:我的剧情2 ,我不知道为什么.我尝试更改cexmar,但没有任何区别.

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.

我是否需要任何其他程序包来控制图例? (我加载了library(graphics),但没有任何区别.)

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.

您好Lyzander,谢谢您的回复.实际上,我确实放大了绘图,它看起来与链接的图完全一样.该图是将绘图保存到png文件时得到的.我转载了您的代码,当我尝试保存它时,会得到以下结果:

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:

这是缩放后的样子:

如您所见,看上去都不像您得到的东西,我也不明白为什么.我拥有R的最新版本,并且更新了所有软件包.

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.

推荐答案

只需使用keyword而不是指定确切的坐标,它将更好地工作:

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))

在这种情况下,如您所见,我使用了topleft关键字,它看起来很棒:

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

如果您指定cex,则会使图例变小,如下所示:

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)

此外,当您在Rstudio中查看图形时,请确保单击缩放"按钮.它更能代表输出是什么.

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

这篇关于在RStudio中打印太大的图例框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆