cannot add ggproto objects together

I am trying to run this..

myplot<-ggplot2:(data_sum, aes(x=habitat, y=biomass, fill = habitat))+
  
  geom_bar(position=position_dodge(.9), stat="identity") +
  
  geom_errorbar(aes(ymin=biomass-se, ymax=biomass+se), width=.1) +
  xlab("Habitat") + 
  ylab("Predator biomass (g.m-2)") 

But each time this error is happening..

Error: Cannot add ggproto objects together. Did you forget to add this object to a ggplot object?
Run `rlang::last_error()` to see where the error occurred.
> rlang::last_error()
<error/rlang_error>
Cannot add ggproto objects together. Did you forget to add this object to a ggplot object?
Backtrace:
 1. ggplot2:::`+.gg`(...)

please help meee

ggplot2 is the name of the package but, the function you need to use from that package is ggplot().

library(ggplot2)

ggplot(data_sum, aes(x = habitat, y = biomass, fill = habitat)) +
    geom_bar(position = position_dodge(.9), stat = "identity") +
    geom_errorbar(aes(ymin = biomass - se, ymax = biomass + se), width = .1) +
    xlab("Habitat") +
    ylab("Predator biomass (g.m-2)") 

If this doesn't solve your problem, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

Thank you andresrcs, I tried this but it still came up with an error.
Here is a reprex of my data and the other error.
data.frame(
stringsAsFactors = FALSE,
Habitat = c("Cymodocea serrulata","Cymodocea serrulata",
"Zostera muelleri","Zostera muelleri",
"Zostera muelleri","Zostera muelleri","Zostera muelleri",
"Zostera muelleri"),
Biomass = c(1.8110906572861,1.8110906572861,1.8110906572861,
1.8110906572861,1.8110906572861,1.8110906572861,
1.8110906572861,1.8110906572861)

But this is the new error..
<error/rlang_error>
data must be a data frame, or other object coercible by fortify(), not an S3 object with class table
Backtrace:

  1. ├─ggplot2::ggplot(data_sum, aes(x = habitat, y = biomass, fill = habitat))
  2. └─ggplot2:::ggplot.default(...)
  3. ├─ggplot2::fortify(data, ...)
  4. └─ggplot2:::fortify.default(data, ...)

For this plot ..
myplot<-ggplot(data_sum, aes(x=habitat, y=biomass, fill = habitat))+

geom_bar(position=position_dodge(.9), stat="identity") +

geom_errorbar(aes(ymin=biomass-se, ymax=biomass+se), width=.1) +
xlab("Habitat") +
ylab("Predator biomass (g.m-2)")

The se variable you reference in your code is not present in your sample data, but if I ommit that this is what I get

library(ggplot2)

# Sample data on a copy/paste friendly format
data_sum <- data.frame(
    stringsAsFactors = FALSE,
    Habitat = c("Cymodocea serrulata","Cymodocea serrulata",
                "Zostera muelleri","Zostera muelleri",
                "Zostera muelleri","Zostera muelleri","Zostera muelleri",
                "Zostera muelleri"),
    Biomass = c(1.8110906572861,1.8110906572861,1.8110906572861,
                1.8110906572861,1.8110906572861,1.8110906572861,
                1.8110906572861,1.8110906572861))

ggplot(data_sum, aes(x = Habitat, y = Biomass, fill = Habitat)) +
    geom_bar(position = position_dodge(.9), stat = "identity") +
    geom_errorbar(aes(ymin = Biomass, ymax = Biomass), width = .1) +
    xlab("Habitat") +
    ylab("Predator biomass (g.m-2)")

Created on 2021-03-16 by the reprex package (v1.0.0.9002)

As you can see I can't reproduce your issue with the data you are providing, can you try again to make a reproducible example that actually shows the problem?

data.frame(
stringsAsFactors = FALSE,
Habitat = c("Cymodocea serrulata","Cymodocea serrulata",
"Zostera muelleri","Zostera muelleri",
"Zostera muelleri","Zostera muelleri","Zostera muelleri",
"Zostera muelleri","Zostera muelleri",
"Zostera muelleri","Cymodocea serrulata",
"Unvegetated","Zostera muelleri","Zostera muelleri",
"Zostera muelleri","Zostera muelleri",
"Zostera muelleri","Unvegetated","Unvegetated",
"Zostera muelleri","Zostera muelleri",
"Zostera muelleri","Zostera muelleri",
"Zostera muelleri","Zostera muelleri","Unvegetated",
"Cymodocea serrulata","Cymodocea serrulata",
"Cymodocea serrulata","Zostera muelleri",
"Zostera muelleri","Zostera muelleri",
"Zostera muelleri","Zostera muelleri","Zostera muelleri",
"Zostera muelleri","Zostera muelleri",
"Zostera muelleri","Zostera muelleri","Unvegetated",
"Zostera muelleri","Zostera muelleri",
"Zostera muelleri","Zostera muelleri",
"Unvegetated","Unvegetated","Cymodocea serrulata",
"Cymodocea serrulata","Unvegetated",
"Cymodocea serrulata","Zostera muelleri","Unvegetated",
"Cymodocea serrulata","Cymodocea serrulata",
"Cymodocea serrulata","Cymodocea serrulata",
"Cymodocea serrulata","Cymodocea serrulata",
"Cymodocea serrulata","Cymodocea serrulata",
"Cymodocea serrulata","Cymodocea serrulata",
"Cymodocea serrulata","Cymodocea serrulata",
"Cymodocea serrulata","Cymodocea serrulata",
"Cymodocea serrulata","Cymodocea serrulata",
"Cymodocea serrulata","Cymodocea serrulata",
"Cymodocea serrulata","Cymodocea serrulata",
"Cymodocea serrulata","Unvegetated",
"Cymodocea serrulata","Cymodocea serrulata",
"Zostera muelleri","Zostera muelleri","Unvegetated",
"Unvegetated","Zostera muelleri",
"Zostera muelleri","Zostera muelleri","Unvegetated",
"Unvegetated","Unvegetated","Unvegetated",
"Unvegetated","Unvegetated",
"Zostera muelleri","Zostera muelleri","Zostera muelleri",
"Zostera muelleri","Zostera muelleri",
"Cymodocea serrulata","Zostera muelleri",
"Unvegetated","Zostera muelleri","Cymodocea serrulata",
"Cymodocea serrulata"),
Biomass = c(1.8110906572861,1.8110906572861,1.8110906572861,
1.8110906572861,1.8110906572861,1.8110906572861,
1.8110906572861,1.8110906572861,
1.8110906572861,1.8110906572861,3.5136858432153,
3.5136858432153,3.5136858432153,3.5136858432153,
3.5136858432153,3.5136858432153,
3.5136858432153,6.03853001889327,9.54472004811211,
14.1905516041284,14.1905516041284,14.1905516041284,
1.32397212016828,2.35125517813125,
0.657958442111442,1.34073024054044,1,1,1,1,1,1,
1,1,1,1,1,1,1,1.03850561425917,
1.03850561425917,1.03850561425917,2.05108844689479,
3.57673849471692,8.60102748523887,
12.3187184392026,0.912068005115458,0.912068005115458,
0.912068005115458,1.77741221332525,
1.77741221332525,3.06577363980793,7.24614312966712,
10.3051175546404,10.3051175546404,
10.3051175546404,1.8110906572861,1.8110906572861,
1.8110906572861,1.8110906572861,1.8110906572861,
1.8110906572861,3.5136858432153,
6.03853001889327,0.608519266879654,0.608519266879654,
0.608519266879654,0.608519266879654,
0.608519266879654,0.608519266879654,
0.608519266879654,0.608519266879654,0.608519266879654,
0.608519266879654,1.20992144244298,
1.20992144244298,1.20992144244298,2.12146275350218,
3.5136858432153,1,19.0398139780978,
19.0398139780978,0.472860390879922,0.453598006421229,
0.453598006421229,0.453598006421229,
0.905924859856634,0.905924859856634,0.444505251225668,
0.444505251225668,0.444505251225668,
0.852813632194663,1.45232352560218,1.45232352560218,
2.27796998852613,2.61254938723566,
1.02313525929414,2.10192512763994,1.8110906572861,
1.8110906572861)

this is 100 points of my data

Each alteration I do it ends in this error

Error: data must be a data frame, or other object coercible by fortify(), not an S3 object with class table

This is what data sum looks like

data_sum
date habitat Haul.no. Length.of.haul
Length:265 Length:265 Min. :1.000 Min. :10
Class :character Class :character 1st Qu.:1.000 1st Qu.:10
Mode :character Mode :character Median :2.000 Median :10
Mean :1.977 Mean :10
3rd Qu.:3.000 3rd Qu.:10
Max. :3.000 Max. :10
Area.of.haul..m2. Species Scientific.name
Min. :100 Length:265 Length:265
1st Qu.:100 Class :character Class :character
Median :100 Mode :character Mode :character
Mean :100
3rd Qu.:100
Max. :100
Size..cm. Quantity A.Value B.Value
Min. : 4.000 Min. :1.000 Min. :0.00045 Min. :0.00
1st Qu.: 4.000 1st Qu.:1.000 1st Qu.:0.00790 1st Qu.:2.97
Median : 5.000 Median :1.000 Median :0.00955 Median :2.99
Mean : 5.479 Mean :1.019 Mean :0.07045 Mean :2.86
3rd Qu.: 6.000 3rd Qu.:1.000 3rd Qu.:0.02950 3rd Qu.:3.08
Max. :15.000 Max. :5.000 Max. :1.00000 Max. :3.19
Biomass Predator.biomass..g. pbiomass
Min. : 0.03401 Min. : 0.03401 Min. :0.0003401
1st Qu.: 0.90592 1st Qu.: 0.90592 1st Qu.:0.0090592
Median : 1.45232 Median : 1.45232 Median :0.0145232
Mean : 2.79309 Mean : 2.83829 Mean :0.0283829
3rd Qu.: 3.06577 3rd Qu.: 3.06577 3rd Qu.:0.0306577
Max. :19.03981 Max. :19.08944 Max. :0.1908944

It seems to me that the sample data you are providing doesn't come from the data_sum object, how do you get data_sum? Please try to provide a complete self contained reproducible example as explained in the link I gave you before. Otherwise I'm afraid I don't have enough information to been able to help you.

Here is @andresrcs code with these data

suppressPackageStartupMessages({
  library(ggplot2)
})
dta <- data.frame(
  Habitat = c("Cymodocea serrulata","Cymodocea serrulata",
              "Zostera muelleri","Zostera muelleri",
              "Zostera muelleri","Zostera muelleri","Zostera muelleri",
              "Zostera muelleri","Zostera muelleri",
              "Zostera muelleri","Cymodocea serrulata",
              "Unvegetated","Zostera muelleri","Zostera muelleri",
              "Zostera muelleri","Zostera muelleri",
              "Zostera muelleri","Unvegetated","Unvegetated",
              "Zostera muelleri","Zostera muelleri",
              "Zostera muelleri","Zostera muelleri",
              "Zostera muelleri","Zostera muelleri","Unvegetated",
              "Cymodocea serrulata","Cymodocea serrulata",
              "Cymodocea serrulata","Zostera muelleri",
              "Zostera muelleri","Zostera muelleri",
              "Zostera muelleri","Zostera muelleri","Zostera muelleri",
              "Zostera muelleri","Zostera muelleri",
              "Zostera muelleri","Zostera muelleri","Unvegetated",
              "Zostera muelleri","Zostera muelleri",
              "Zostera muelleri","Zostera muelleri",
              "Unvegetated","Unvegetated","Cymodocea serrulata",
              "Cymodocea serrulata","Unvegetated",
              "Cymodocea serrulata","Zostera muelleri","Unvegetated",
              "Cymodocea serrulata","Cymodocea serrulata",
              "Cymodocea serrulata","Cymodocea serrulata",
              "Cymodocea serrulata","Cymodocea serrulata",
              "Cymodocea serrulata","Cymodocea serrulata",
              "Cymodocea serrulata","Cymodocea serrulata",
              "Cymodocea serrulata","Cymodocea serrulata",
              "Cymodocea serrulata","Cymodocea serrulata",
              "Cymodocea serrulata","Cymodocea serrulata",
              "Cymodocea serrulata","Cymodocea serrulata",
              "Cymodocea serrulata","Cymodocea serrulata",
              "Cymodocea serrulata","Unvegetated",
              "Cymodocea serrulata","Cymodocea serrulata",
              "Zostera muelleri","Zostera muelleri","Unvegetated",
              "Unvegetated","Zostera muelleri",
              "Zostera muelleri","Zostera muelleri","Unvegetated",
              "Unvegetated","Unvegetated","Unvegetated",
              "Unvegetated","Unvegetated",
              "Zostera muelleri","Zostera muelleri","Zostera muelleri",
              "Zostera muelleri","Zostera muelleri",
              "Cymodocea serrulata","Zostera muelleri",
              "Unvegetated","Zostera muelleri","Cymodocea serrulata",
              "Cymodocea serrulata"),
  Biomass = c(1.8110906572861,1.8110906572861,1.8110906572861,
              1.8110906572861,1.8110906572861,1.8110906572861,
              1.8110906572861,1.8110906572861,
              1.8110906572861,1.8110906572861,3.5136858432153,
              3.5136858432153,3.5136858432153,3.5136858432153,
              3.5136858432153,3.5136858432153,
              3.5136858432153,6.03853001889327,9.54472004811211,
              14.1905516041284,14.1905516041284,14.1905516041284,
              1.32397212016828,2.35125517813125,
              0.657958442111442,1.34073024054044,1,1,1,1,1,1,
              1,1,1,1,1,1,1,1.03850561425917,
              1.03850561425917,1.03850561425917,2.05108844689479,
              3.57673849471692,8.60102748523887,
              12.3187184392026,0.912068005115458,0.912068005115458,
              0.912068005115458,1.77741221332525,
              1.77741221332525,3.06577363980793,7.24614312966712,
              10.3051175546404,10.3051175546404,
              10.3051175546404,1.8110906572861,1.8110906572861,
              1.8110906572861,1.8110906572861,1.8110906572861,
              1.8110906572861,3.5136858432153,
              6.03853001889327,0.608519266879654,0.608519266879654,
              0.608519266879654,0.608519266879654,
              0.608519266879654,0.608519266879654,
              0.608519266879654,0.608519266879654,0.608519266879654,
              0.608519266879654,1.20992144244298,
              1.20992144244298,1.20992144244298,2.12146275350218,
              3.5136858432153,1,19.0398139780978,
              19.0398139780978,0.472860390879922,0.453598006421229,
              0.453598006421229,0.453598006421229,
              0.905924859856634,0.905924859856634,0.444505251225668,
              0.444505251225668,0.444505251225668,
              0.852813632194663,1.45232352560218,1.45232352560218,
              2.27796998852613,2.61254938723566,
              1.02313525929414,2.10192512763994,1.8110906572861,
              1.8110906572861)
)
  

p <- ggplot(dta,aes(Habitat,Biomass, fill = Habitat))
p + geom_bar(aes(Habitat,Biomass),position=position_dodge(.9), stat="identity") +
    geom_errorbar(aes(ymin = min(Biomass), ymax = max(Biomass)), width = 0.1) +
  xlab("Habitat") +
  ylab("Biomass") + theme_minimal()

Thank you,
Im just wondering how you get the graphs to produce each time. I have the exact same code and it always just says the error as stated above?

The advantage of reprex is that it can be cut-and-pasted into a source or console window and is all-but-guaranteed to produce identical results. I find that whenever I believe that I did exactly the same thing as last time, I didn't. Try cutting and pasting mine.

1 Like

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.