OpenSauce
OpenSauce

Reputation: 354

How to fix "Discrete value supplied to continuous scale" Error in ggplot2

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

Upvotes: 4

Views: 626

Answers (1)

Arienrhod
Arienrhod

Reputation: 2581

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

Upvotes: 1

Related Questions