38

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.

3
  • 13
    you seem to have too many +s in your lines... Specifically, drop all the + at the beginning of the lines; they're presumably remnants of the printed output in a R console. Commented Jun 21, 2013 at 16:57
  • 2
    baptiste is correct. If you paste output back from the console you'll get this issue. You can use Rstudio's history or work from a file and it will let you get around this. Commented Jun 21, 2013 at 17:01
  • 1
    Right you are! Haha, it's always something stupid like that. Thanks! Works fine now. Commented Jun 21, 2013 at 17:05

4 Answers 4

77

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.

Sign up to request clarification or add additional context in comments.

Comments

23

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"

Comments

4

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.

Comments

0

Try to consolidate the syntax in a single line. this will clear the error

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.