简体   繁体   中英

Problems with ggplot2 and geom_errorbar()

Greeting, I'm having a hard time with ggplot2 and the geom_error function. I have a data frame with individuals(rows) and size(column 1) and density(column2). My aim is to plot influence of density on size in a quadratic model. lm(size ~ poly(density, 2, raw=TRUE)) for that matter I used.

ggplot(df, aes(x = density, y = size, col = Sexo)) + 
geom_smooth(method = lm, formula = y ~ x + I(x^2), size = 1)+                      
geom_point())

在此处输入图片说明

It went fine. But now I want to plot the same data set with geom_errorbar . I tried.

ggplot(cg.cvic, aes(x = as.factor(density), y = size, col = sex)) + 
    geom_errorbar(ymin = size-sd, ymax = size + sd))   

And I'm guettint the response:

Error in size - sd : non-numeric argument to binary operator

What am I doing wrong?

Firstly there is no column sd in your data frame. Moreover R has build in function sd which is a function not a variable or a number. So from R perspective you are trying to add variable to a function, so R tells you that one of the argument is non-numeric and your are trying to perform on him action which can only be perfomed on numbers. You have extract somehow the standard deviation of your model predictions, write it in your data frame and after that use it in ggplot. And don't name it sd , use something else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM