Skip to navigation

ggplot2 error “invalid argument to unary operator”

I had a longish ggplot command and kept getting the error:

invalid argument to unary operator

It happened when I was specifying custom labels with scale_y_continuous but it turns out this was irrelevant and it hinged on a line break. It turnsout (at least when stepping through the code with ESS in Emacs using C-c C-c, bound to ess-eval-function-or-paragraph-and-step, you need to have the + of tying different ggplot bits together on the end of the preceding line.

This won’t work:

ggplot(longdata, aes(x=x, y = y))
+ geom_jitter(alpha = 0.6)

This will:

ggplot(longdata, aes(x=x, y = y)) +
geom_jitter(alpha = 0.6)

(Or, put it all on one line).

Leave a Reply