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

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. Commented Sep 29, 2017 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. I believe this error message is changing in the next release of ggplot2 in an attempt to make what is happening clearer. Commented Sep 29, 2017 at 18:16

2 Answers 2

1

I think it should read

+ geom_errorbar(mapping=aes(ymin=TC-1.555, ymax=TC+1.555), width=.2)
Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.