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).

13 Responses to “ggplot2 error “invalid argument to unary operator””

  1. Anonymous Says:

    I’d been struggling with this for an hour. thanks!

  2. Sam Says:

    Thank you for this. Easy fix!

  3. Anonymous Says:

    Thank you!!!

  4. Anonymous Says:

    thank you!!! brilliant

  5. M.Shapiera Says:

    Thanks for this. I’d have been hard pressed to figure that out!

  6. Dolev Rahat Says:

    This is extraordinarily useful. You just save my day. Thanks!

  7. Par Jason Engle Says:

    Thank You. That’s it!

  8. timat35 Says:


  9. Anonymous Says:

    Oh my gosh, thank you so much!!!

  10. Anonymous Says:

    Lifesaver! God, the idiosyncrasies or r.

  11. Mark Says:

    Brilliant, thanks man! Great post!

  12. Me Says:

    Thanks so much! Saved me alot of trouble

  13. Ruser9999999 Says:

    Thank you!!!