kyliec
kyliec

Reputation: 21

Bar Graph with error bars in ggplot2 getting error message "non-numeric argument to binary operator"

I am trying to make a barplot with error bars in ggplot2, I keep getting this error message: "Error in value - standard_error : non-numeric argument to binary operator"

Salinitydata <- data.frame(
value=c("0","0", "0","0","0", 
     "5","5", "5","5", 
      "10",'10',"10",'10', 
  "20", '20', "20", '20',"20", '20', "20") ,  
  Salinity=c(21,22,22,22,22, 
            23,22,22,22, 
            23,22,23,22, 
             25,22,24,22.5,23,24,24))
# create standard error
standard_error = 5
# Load ggplot2 package
library("ggplot2")

# Create bar plot using ggplot() function
ggplot(Salinitydata, aes(value, Salinity))+
 geom_bar(stat = "identity")+
 geom_errorbar(aes(ymin=value-standard_error,
              ymax=value+standard_error),
       width=.2)

Upvotes: 0

Views: 150

Answers (1)

Quinten
Quinten

Reputation: 41225

As @stefan said right in the comments, your value column should be numeric. Another thing, I think you should change value with Salinity, since you want to add error bars which makes sense to your y-axis like this:

Salinitydata <- data.frame(
  value=c("0","0", "0","0","0", 
          "5","5", "5","5", 
          "10",'10',"10",'10', 
          "20", '20', "20", '20',"20", '20', "20") ,  
  Salinity=c(21,22,22,22,22, 
             23,22,22,22, 
             23,22,23,22, 
             25,22,24,22.5,23,24,24))
# create standard error
standard_error = 5
# Load ggplot2 package
library("ggplot2")
Salinitydata$value <- as.numeric(Salinitydata$value)
# Create bar plot using ggplot() function
ggplot(Salinitydata, aes(value, Salinity))+
  geom_bar(stat = "identity")+
  geom_errorbar(aes(ymin=Salinity-standard_error,
                    ymax=Salinity+standard_error),
                width=.2)

Created on 2022-07-12 by the reprex package (v2.0.1)

Upvotes: 0

Related Questions