R: "Unary operator error" from multiline ggplot2 command

soosus picture soosus · Jun 21, 2013 · Viewed 127k times · Source

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.

Answer

Dave Kleinschmidt picture Dave Kleinschmidt · Jun 21, 2013

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.