0

I would like to add an error bar of 1.555 % to each data point. Or have a back ground band of +/- 1.555 behind the points. I've done the calculation elsewhere based on a different set which is not featured in the plot. I can't seem to add this vertical error bar.

library(ggplot2)
carb<-read.table("total_carb", header= TRUE)

p<- ggplot(carb,aes(x=Sample, y=TC, color="Total Carbonate")) + geom_line() + geom_point()

p + scale_x_continuous(name="Core Depth (cm)") + scale_y_continuous(name="Carbonate (%)")
  + geom_errorbar(aes(ymin=TC-1.555, ymax=TC+1.555), width=.2)

my error:

Error in +geom_errorbar(aes(ymin = TC - 1.555, ymax = TC + 1.555), width = 0.2) : invalid argument to unary operator

aosmith
  • 34,856
  • 9
  • 84
  • 118
  • If **p** plots correctly, I suspect that this is a typographical error. Are you sure you copied the code over exactly? Your code as written should work. – Brian Sep 29 '17 at 16:25
  • 2
    You put the `+` sign at the beginning of the `geom_errobar` line rather than the end of the previous line. See [here](https://stackoverflow.com/questions/17240536/r-unary-operator-error-from-multiline-ggplot2-command). I believe this error message is changing in the next release of *ggplot2* in an attempt to make what is happening clearer. – aosmith Sep 29 '17 at 18:16

2 Answers2

1

I think it should read

+ geom_errorbar(mapping=aes(ymin=TC-1.555, ymax=TC+1.555), width=.2)
Guenther
  • 2,035
  • 2
  • 15
  • 20
0

Try adding a columns to your dataset that contain the values for the error. For example:

carb$error <- carb$data*0.015
carb$lower <- carb$data - carb$error
carb$upper <- carb$data + carb$error

p<- ggplot(carb,aes(x=Sample, y=TC, color="Total Carbonate")) + geom_line() + geom_point()

p + scale_x_continuous(name="Core Depth (cm)") + scale_y_continuous(name="Carbonate (%)")
  + geom_errorbar(aes(ymin=lower, ymax=upper), width=.2)
bob1
  • 398
  • 3
  • 12