• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Data Viz with Python and R

Learn to Make Plots in Python and R

  • Home
  • Python Viz
  • Seaborn
  • Altair
  • R Viz
  • ggplot2
  • About
    • Privacy Policy
  • Show Search
Hide Search

ggplot2 error: Cannot use `+.gg()` with a single argument.

datavizpyr · July 26, 2022 ·

In this post, we will learn how to fix one of the common ggplot2 error messages. While trying to make a ggplot2, we might get the following error.

Error in `+.gg`:
! Cannot use `+.gg()` with a single argument. 
Did you accidentally put + on a new line?

The error message here ” Did you accidentally put + on a new line?” is bit helpful as it hints what is the cause of the error. We might see “Error in `+.gg` at least in a couple scenarios while making a plot using ggplot2 in R.
Let us load packages we will be using

library(tidyverse)
library(palmerpenguins)
theme_set(theme_bw(16))

We will see two scenarios where we will see this error message and learn how to fix them.

ggplot2 error: Cannot use `+.gg()` : First example

Let us say we are trying to make a boxplot with jittered data points. Our goal is to make a plot like this.

How to fix the ggplot2_error: Did you accidentally put + on new line
How to fix the ggplot2_error: Did you accidentally put + on new line

And we write the following R code to make the plot using ggplot2.

# R code with a mistake that gives "Error in `+.gg`:"
penguins %>%
  ggplot(aes(x=species, y=body_mass_g, color=species))
  + geom_boxplot(outlier.shape = NA) + 
  geom_jitter(width=0.2)+ 
  theme(legend.position = "none")

Using the code above we will get the following error instead of the plot that we aimed to make

Error in `+.gg`:
! Cannot use `+.gg()` with a single argument. Did you accidentally put + on a new line?
Backtrace:
 1. ggplot2:::`+.gg`(geom_boxplot(outlier.shape = NA))
 Error in `+.gg`(geom_boxplot(outlier.shape = NA)) :

The error message here is but helpful. First it asks “Did you accidentally put + on a new line?” . We can immediately check for “+” on a new line and we find that there is “+” sign before geom_boxplot() in our code. If we see the error message again we can see that the Backtrace actually started at out geom_boxplot() statement.

Now that we have identified the problem we can remove th “+” sign at the beginning of line and put that at the end of previous line where we have ggplot() statement.

ggplot2 error: Cannot use `+.gg()` : Second example

Here is an example of another reason to see the same error. Let us say we are trying to be the boxplot with jittered datapoints as before and this time we write the following R code. We can quickly see that we don’t any “+” sign at the new line.

penguins %>
  ggplot(aes(x=species, y=body_mass_g, color=species))+
  geom_boxplot(outlier.shape = NA)+
  geom_jitter(width=0.2)+ +
  theme(legend.position = "none")

Still we will get the following error message (the same as before).


Error in `+.gg`:
! Cannot use `+.gg()` with a single argument. Did you accidentally put + on a new line?
Backtrace:
 1. ggplot2:::`+.gg`(theme(legend.position = "none"))
 Error in `+.gg`(theme(legend.position = "none")) :

We can see that the error message is possibly before our theme(legend.position=”none”) statement. If we check the code in the previous line we find that at the end of the previous line we have two “+” signs. And that thas triggered the same error. By emoving that two “+” signs we get the boxplot we wanted.

penguins %>
  ggplot(aes(x=species, y=body_mass_g, color=species))+
  geom_boxplot(outlier.shape = NA)+
  geom_jitter(width=0.2)+ 
  theme(legend.position = "none")

Related posts:

How to fix ggplot2 error: Mapping should be created with aesError in ggplot(): Mapping should be created with `aes()` How to fix Error in dataframeggplot2 error: how to fix error in dataframe How to fix Error in ggplot(., aes... could not find function ggplotggplot2 errot: how to fix could not find function “ggplot Customizing Mean mark to boxplot with ggplot2How To Show Mean Value in Boxplots with ggplot2?

Filed Under: ggplot2, ggplot2 error, R Tagged With: ggplot2 error, ggplot2 error Cannot use `+.gg()`

Primary Sidebar

Tags

Altair barplot Boxplot boxplot python boxplot with jiitered text labels Bubble Plot Color Palette Countplot Density Plot Facet Plot gganimate ggplot2 ggplot2 Boxplot ggplot2 error ggplot boxplot ggridges ggtext element_markdown() Grouped Barplot R heatmap heatmaps Histogram Histograms Horizontal boxplot Python lollipop plot Maps Matplotlib Pandas patchwork pheatmap Pyhon Python R RColorBrewer reorder boxplot ggplot Ridgeline plot Scatter Plot Scatter Plot Altair Seaborn Seaborn Boxplot Stock Price Over Time Stripplot UpSetR Violinplot Violin Plot World Map ggplot2

Buy Me a Coffee

Copyright © 2025 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version