0
votes

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

2
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
You put the + sign at the beginning of the geom_errobar line rather than the end of the previous line. See here. I believe this error message is changing in the next release of ggplot2 in an attempt to make what is happening clearer.aosmith

2 Answers

0
votes

I think it should read

+ geom_errorbar(mapping=aes(ymin=TC-1.555, ymax=TC+1.555), width=.2)
0
votes

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)