R: “Unary operator error” from multiline ggplot2 c

2019-02-04 00:40发布

问题:

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

> library(reshape2)
> library(ggplot2)
> melt.data = melt(actb.raw.data)

> head(actb.raw.data)
  region  expression species
1     CG -0.17686667   human
2     CG -0.06506667   human
3     DG  1.04590000   human
4    CA1  1.94093333   human
5    CA2  1.55023333   human
6    CA3  1.75800000   human

> head(melt.data)
  region species   variable       value
1     CG   human expression -0.17686667
2     CG   human expression -0.06506667
3     DG   human expression  1.04590000
4    CA1   human expression  1.94093333
5    CA2   human expression  1.55023333
6    CA3   human expression  1.75800000

However, when I run the following code:

ggplot(combined.data, aes(x = region, y = expression, fill = species)) +
+     geom_boxplot() +
+     scale_fill_manual(values = c("yellow", "orange"))
+     ggtitle("Expression comparisons for ACTB")
+     theme(axis.text.x = element_text(angle=90, face="bold", colour="black"))

I get this error:

> ggplot(actb.raw.data, aes(x = region, y = expression, fill = species)) +
+     + geom_boxplot() +
+     + scale_fill_manual(values = c("yellow", "orange"))
Error in +geom_boxplot() : invalid argument to unary operator
> + ggtitle("ACTB expression in human vs. macaque")
Error in +ggtitle("ACTB expression in human vs. macaque") : 
 invalid argument to unary operator
> + theme(axis.text.x = element_text(angle=90, face="bold", colour="black"))
Error in inherits(x, "theme") : argument "e2" is missing, with no default

This also happens when I run using the variable melt.data, for whatever that's worth. Can someone help me fix this? I've run this code successfully before with a different dataset that was formatted identically, and I can't figure out what's going wrong here.

回答1:

It looks like you might have inserted an extra + at the beginning of each line, which R is interpreting as a unary operator (like - interpreted as negation, rather than subtraction). I think what will work is

ggplot(combined.data, aes(x = region, y = expression, fill = species)) +
    geom_boxplot() +
    scale_fill_manual(values = c("yellow", "orange")) + 
    ggtitle("Expression comparisons for ACTB") + 
    theme(axis.text.x = element_text(angle=90, face="bold", colour="black"))

Perhaps you copy and pasted from the output of an R console? The console uses + at the start of the line when the input is incomplete.



回答2:

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 this 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"



回答3:

It's the '+' operator at the beginning of the line that trips things up (not just that you are using two '+' operators consecutively). The '+' operator can be used at the end of lines, but not at the beginning.

This works:

ggplot(combined.data, aes(x = region, y = expression, fill = species)) +
geom_boxplot() 

The does not:

ggplot(combined.data, aes(x = region, y = expression, fill = species))
+ geom_boxplot() 

*Error in + geom_boxplot():
invalid argument to unary operator*

You also can't use two '+' operators, which in this case you've done. But to fix this, you'll have to selectively remove those at the beginning of lines.