[r] R: "Unary operator error" from multiline ggplot2 command

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.

This question is related to r ggplot2 multiline reshape2

The answer is


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.


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"


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


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.


Examples related to r

How to get AIC from Conway–Maxwell-Poisson regression via COM-poisson package in R? R : how to simply repeat a command? session not created: This version of ChromeDriver only supports Chrome version 74 error with ChromeDriver Chrome using Selenium How to show code but hide output in RMarkdown? remove kernel on jupyter notebook Function to calculate R2 (R-squared) in R Center Plot title in ggplot2 R ggplot2: stat_count() must not be used with a y aesthetic error in Bar graph R multiple conditions in if statement What does "The following object is masked from 'package:xxx'" mean?

Examples related to ggplot2

Center Plot title in ggplot2 R ggplot2: stat_count() must not be used with a y aesthetic error in Bar graph Saving a high resolution image in R Change bar plot colour in geom_bar with ggplot2 in r Remove legend ggplot 2.2 Remove all of x axis labels in ggplot Changing fonts in ggplot2 Explain ggplot2 warning: "Removed k rows containing missing values" Error: package or namespace load failed for ggplot2 and for data.table In R, dealing with Error: ggplot2 doesn't know how to deal with data of class numeric

Examples related to multiline

Way to create multiline comments in Bash? How to get multiline input from user R: "Unary operator error" from multiline ggplot2 command Can a JSON value contain a multiline string PHP multiline string with PHP Pythonic way to create a long multi-line string How do I create a multiline Python string with inline variables? How do you write multiline strings in Go? Multiline TextView in Android? What is the proper way to format a multi-line dict in Python?

Examples related to reshape2

Grouped bar plot in ggplot R: "Unary operator error" from multiline ggplot2 command Compute mean and standard deviation by group for multiple variables in a data.frame