Help using ggplot2: I can perfectly plot one variable but the other keeps returning an error

Hi all,

I'm creating a series of figures for some vegetation survey data and I'm coming up against a frustrating barrier in ggplot2. I managed to figure out how to make a figure with all the elements I want using:

ISI_summary<-anovadata %>%
  group_by(Site) %>%
  summarise(mean_ISI= mean(ISI),
            sd_ISI= sd(ISI),
            n_ISI= n(),
            se_ISI= sd(ISI)/sqrt(n()))

ISIPlot<-ggplot(ISI_summary, aes(Site, mean_ISI))+
  geom_col(color= "gray20", fill="seagreen3")+
  geom_errorbar(aes(ymin=mean_ISI-se_ISI, ymax=mean_ISI+se_ISI), width=0.2)+
  geom_text(label=c("b","a","a","a","a","b","a"), aes(y=mean_ISI+se_ISI, x=Site), 
            vjust=-0.5, size=5)+
  ylim(0,2.5)

ISIfigure<-ISIPlot + labs(y="Mean ISI ± SE", x="Site")

However when I try to replicate this figure using a different variable:

Rich_summary<-anovadata %>%
  group_by(Site) %>%
  summarise(mean_rich= mean(rich),
            sd_rich= sd(rich),
            n_rich= n(),
            se_rich= sd(rich/sqrt(n()))

RichPlot<-ggplot(Rich_summary, aes(Site, mean_rich)) +
  geom_col(color= "gray20", fill="steelblue3") +
  geom_errorbar(aes(ymin=mean_rich-se_rich, ymax=mean_rich+se_rich), width=0.2) +
  geom_text(label=c("a","ab","abc","c","bc","bc","ab"), aes(y=mean_rich+se_rich, x=Site), 
            vjust=-0.5, size=5)

Richfigure<-RichPlot + labs(y="Mean Total Species Richness ± SE", x="Site")

I get the error "Error: Cannot add ggproto objects together. Did you forget to add this object to a ggplot object?"
I'm not sure what's happening here. I copied the text from the first figure exactly, just replacing the "ISI" variable with the "rich" variable. Can anyone offer some advice? Thank you

You're right. They look identical, except for ylim(0,2.5), but ggplot is unconvinced. The message is a complaint that labs(y="Mean ISI ± SE", x="Site") isn't being tacked on to a ggplot object.

These are hard to eye-check and a little sample from anovadata would make a nice reproducible example, called a reprex.

Try

RichPlot

and see if it produces output, which is pretty good evidence that you do have a ggplot object, and v.v.

If that works

RichPlot + labs(y="Mean Total Species Richness ± SE", x="Site")

to see if you still get the error message.

If you don't, put spaces around <-

Thank you for the advice! I went back over my code and found the issue: I was missing a closing bracket at the end of

summarise(mean_rich= mean(rich),
            sd_rich= sd(rich),
            n_rich= n(),
            se_rich= sd(rich/sqrt(n()))

Which for some reason caused no errors in my first plot but was holding up the execution of the second plot...

RStudio does some highlighting of symmetry if you hover over an opening brace it will show you the corresponding closing brace. Also, the styler package will uniformly lay out your code, which can make it easier to spot those darned missing piece. Glad you're on your way!

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.