简体   繁体   English

R图中的图例显示所有线条

[英]legend in R plot display all lines

I have a following problem.我有以下问题。 I want to put a legend into my graph.我想在我的图表中加入一个图例。 My code:我的代码:


plot(Lc(`BEL_2016_final.csv`$value),col="red",lwd=2, 
     xaxt="n", yaxt="n", cex.lab = 1.5)
axis(side=1, at=axTicks(1), cex.axis = 1.5)
axis(side=2, at=axTicks(2), cex.axis = 1.5)

par(new=TRUE)

plot(Lc(`CRO_2016_final.csv`$value),col="blue",lwd=2,
     xaxt="n", yaxt="n", cex.lab = 1.5)
axis(side=1, at=axTicks(1), cex.axis = 1.5)
axis(side=2, at=axTicks(2), cex.axis = 1.5)

legend(x = "topleft", legend=paste0(c("Belgium, Gini "),
                            round(Gini(`BEL_2016_final.csv`$value), digits = 2),
                            c("Croatia, Gini "), 
                            round(Gini(`CRO_2016_final.csv`$value), digits = 2)),
       col=c("red", "blue"), lty=1:2, cex=1, lwd=1.5)

However, the legend looks like this:然而,传说看起来是这样的:

在此处输入图片说明

When I try:当我尝试:

legend=paste0(c("Belgium, Gini ", "Croatia, Gini "),
                                    round(c(Gini(`BEL_2016_final.csv`$value)),
                                    Gini(`CRO_2016_final.csv`$value)),
                                   digits = 2)

I got this result:我得到了这个结果: 在此处输入图片说明

which is wrong, because Gini index for Croatia is 0.73.这是错误的,因为克罗地亚的基尼指数是 0.73。

How can I modify my code to display both lines (red and blue) in the legend, both on a new line?如何修改我的代码以在图例中显示两行(红色和蓝色),都在新行上? Thanks a lot.非常感谢。

Your parentheses are mismatched.您的括号不匹配。 Whatever IDE/editor you are using I encourage the use of matching (sometimes "rainbow") parentheses.无论您使用什么 IDE/编辑器,我都鼓励使用匹配(有时是“彩虹”)括号。 For example, in RStudio, if the cursor is the _ symbol (and accepting RStudio's insistence on its indentation preference):例如,在 RStudio 中,如果光标是_符号(并接受 RStudio 对其缩进首选项的坚持):

在此处输入图片说明

notice that the ( next to paste0 is highlighted, suggesting you that digits=2 is the last argument in paste0 . This is incorrect. Another hint is using RStudio's indentation preference (highlight the block and press Ctrl-I , the default keypress for "Reindent Lines"): the second Gini lines up with c( , not with the first Gini , meaning that c( and second- Gini are at the same level ... where I would expect the second- Gini to be nested within the c( .请注意(旁边的paste0被突出显示,表明digits=2paste0的最后一个参数。这是不正确的。另一个提示是使用 RStudio 的缩进首选项(突出显示块并按Ctrl-I ,“重新缩进的默认按键” Lines"):第二个Ginic( ,而不是第一个Gini ,这意味着c(和第二个Gini在同一级别......我希望第二个Gini嵌套在c( .

To validate what is going on, I'll replace the Gini(.) calls with your 0.52 and 0.73 values, verbatim (but please keep them as Gini(.) in your code:为了验证发生了什么,我将用您的0.520.73值逐字替换Gini(.)调用(但请在代码中将它们保留为Gini(.)

paste0(c("Belgium, Gini ", "Croatia, Gini "),
                                    round(c(0.52),
                                    0.73),
                                   digits = 2)
# [1] "Belgium, Gini 0.52" "Croatia, Gini 0.52"

Looking at it this way, it appears as if the first right-paren after 0.52 might have been intended to be after the 0.73 , since grouping 0.52 and 0.73 makes sense.从这个角度来看,似乎0.52之后的第一个右括号可能是在0.73 ,因为将 0.52 和 0.73 分组是有意义的。

Here is corrected code, where all I do is remove one right-paren from after the first- Gini , and add one right-paren to the very end of this expression:这是更正的代码,我所做的就是从第一个Gini之后删除一个右括号,并在此表达式的最后添加一个右括号:

legend=paste0(c("Belgium, Gini ", "Croatia, Gini "),
              round(c(Gini(`BEL_2016_final.csv`$value),
                      Gini(`CRO_2016_final.csv`$value)),
              digits = 2) )

and the associated matching-paren highlighting (again, _ is the current cursor):以及相关的匹配括号突出显示(同样, _是当前光标):

带有相应父级匹配的 rstudio

<soapbox>

PS: I am not saying that one must use the RStudio IDE for R work. PS:我并不是说必须使用 RStudio IDE 来进行 R 工作。 In fact, I don't, I use emacs/ess.其实我不会,我用的是emacs/ess。 There are other editors to use as well.还有其他编辑器可以使用。 However, as much as indentation and similar can be viewed as style and therefore not important for programming, I argue that indentation and some editor functionality like matching-parens can help in readability as well as troubleshooting code before you even get to a mistake;然而,尽管缩进和类似可以被视为风格,因此对编程并不重要,但我认为缩进和一些编辑器功能(如匹配括号)可以帮助提高可读性以及在您出现错误之前对代码进行故障排除; for instance, a consistent indentation style alone here hints to improper paren-closure, and the matching-paren-highlighter confirms it.例如,这里一致的缩进样式暗示了不正确的括号闭合,而匹配括号突出显示则证实了这一点。 Use what you prefer, but some programming styles are actually beneficial functionally (and therefore pragmatic).使用您喜欢的方式,但某些编程风格实际上在功能上是有益的(因此也是实用的)。

</soapbox>

Correct solution is:正确的解决办法是:

legend(x = "topleft", legend=paste0(c("Belgium, Gini ", "Croatia, Gini "),
                                    c(round(Gini(`BEL_2016_final.csv`$value), digits = 2),
                                    round(Gini(`CRO_2016_final.csv`$value), digits = 2)
                                    )),
       col=c("red", "blue"), lty=1:2, cex=1, lwd=1.5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM