2
votes

I've tried searching for a solution to this seemingly easy problem, but to no avail. All I'm trying to do is plot a line in ggplot and its standard deviation around the line. However, I keep recovering this error:

Error: Discrete value supplied to continuous scale

My data frame plotdata is as follows:

 sites    Spoly     Spolylower Spolyupper
 526.790 0.03018671 0.1196077 0.1196077
1538.512 0.04106053 0.1429613 0.1429613
2540.500 0.02896953 0.1127456 0.1127456
3541.000 0.03560484 0.1200609 0.1200609
4560.143 0.06038193 0.1564464 0.1564464
5569.831 0.03608714 0.1296704 0.1296704

I can plot just the line perfectly fine:

ggplot(data = plotdata, aes(x = "Sites", y = "Mean Values")) +
  geom_line(aes(x = sites, y = Spoly), color = "steelblue")

But when I try to add the ribbon, I get the error:

ggplot(data = plotdata, aes(x = "Sites", y = "Mean Values")) +
  geom_line(aes(x = sites, y = Spoly), color = "steelblue") +
  geom_ribbon(aes(x = sites, ymin = Spolylower, ymax = Spolyupper), alpha = 0.3)

Error: Discrete value supplied to continuous scale

What is going on? What am I doing wrong here?

2

2 Answers

1
votes

one option is:

library(ggplot2)
library(cowplot)
data <- "
sites    Spoly     Spolylower Spolyupper
526.790 0.03018671 0.1196077 0.1196077
1538.512 0.04106053 0.1429613 0.1429613
2540.500 0.02896953 0.1127456 0.1127456
3541.000 0.03560484 0.1200609 0.1200609
4560.143 0.06038193 0.1564464 0.1564464
5569.831 0.03608714 0.1296704 0.1296704
"
dat <- read.table(text = data, header = TRUE)
#change Spolylower value (currently Spolylower= Spolyupper)
dat$Spolylower <- dat$Spolylower - .2

ggplot(data = dat, aes(x = sites, y = Spoly)) +
      geom_line(color = "steelblue") +
      geom_ribbon(aes(ymin = Spolylower, ymax = Spolyupper), alpha = 0.3) +
      theme_cowplot()
0
votes

I think you should try this:

ggplot(data = plotdata, aes(x = "Sites", y = "Mean Values")) +
  geom_line(aes(x = sites, y = Spoly), color = "steelblue") +
  geom_ribbon(aes(ymin = plotdata$Spolylower, ymax = plotdata$Spolyupper),fill="dimgray", alpha = 0.1)

let me know if it works