4

I want to plot the mean abundance data and standard error data which I calculated in Excel as a bar plot in ggplot2. I am getting the error Error: Discrete value supplied to continuous scale when I try to plot my data in gglot2.

I have tried using an import of the data directly from Excel in Comma Delimited Format (CSV), this did not work so I tried creating my data frame from scratch and the same error appears.

Here is the minimum code required to produce the error. First, I create the column data.

Parasite <- c("Heligmosomoides", "Heligmosoma", "Trichuris", "Mastophorus", "Auncotheca", "Syphacia", "Tapeworms") Mean <- c(0.166, 0.053, 0.012, 0.012, 0.0072, 0.287, 0.067) SE <- c(0.060, 0.036, 0.012, 0.012, 0.042, 0.125, 0.026) 

Then I created the data frame.

DF6 <- data.frame(Parasite, Mean, SE) 

Then I load up ggplot2.

library(ggplot2) 

Then I used ggplot2 to create my bar graph with error bars also.

BGPA <- ggplot(DF6, aes(x = DF6$Parasite, y = DF6$Mean)) + geom_bar(color="black") + geom_errorbar(aes(ymin = DF6$Parasite, ymax = DF6$Mean+DF6$SE)) 

And then print it.

print(BGPA) 

This is where I get the error.

Error: Discrete value supplied to continuous scale 
3
  • 1
    In ggplot, get rid of the DF6$. And you have ymin = Parasite, it should be ymin = Mean - SE. Commented Aug 11, 2019 at 17:26
  • 2
    Try this code: ggplot(DF6,aes(x=Parasite,y=Mean))+geom_col(color="black")+geom_errorbar(aes(ymin=Mean-SE,ymax=Mean+SE)). Generally, don't use $ in ggplot aesthetics, as they make use of non-standard evaluation. Commented Aug 11, 2019 at 17:26
  • Thank you I have tried what you both recommended and it worked! Error was also me using geom_bar instead of geom_col. Excellent! Commented Aug 11, 2019 at 17:33

1 Answer 1

1

The issue is that you are setting the ymin to Parasite instead of Mean-SE. Also either use geom_bar with stat = "identity" or geom_col.

BGPA <- ggplot(DF6, aes(x = Parasite, y = Mean)) + geom_bar(color = "black", stat = "identity") + geom_errorbar(aes(ymin = Mean-SE, ymax = Mean+SE)) BGPA 
Sign up to request clarification or add additional context in comments.

2 Comments

This is all correct except I removed the negative SE for aesthetic purposes. Thank you for letting me know about how to use geom_bar correctly!
No worries. And if you want to remove the lower SE, then just use ymin = Mean.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.