R: “Unary operator error” from multiline ggplot2 command

前端 未结 4 1102
半阙折子戏
半阙折子戏 2020-12-29 02:31

I\'m using ggplot2 to do a boxplot comparison of two different species, as indicated by the third column shown below:

> library(reshape2)
> library(ggp         


        
4条回答
  •  梦谈多话
    2020-12-29 02:51

    This is a well-known nuisance when posting multiline commands in R. (You can get different behavior when you source() a script to when you copy-and-paste the lines, both with multiline and comments)

    Rule: always put the dangling '+' at the end of a line so R knows the command is unfinished:

    ggplot(...) + geom_whatever1(...) +
      geom_whatever2(...) +
      stat_whatever3(...) +
      geom_title(...) + scale_y_log10(...)
    

    Don't put the dangling '+' at the start of the line, since that tickles the error:

    Error in "+ geom_whatever2(...) invalid argument to unary operator"

    And obviously don't put dangling '+' at both end and start since that's a syntax error.

    So, learn a habit of being consistent: always put '+' at end-of-line.

    cf. answer to "Split code over multiple lines in an R script"

提交回复
热议问题