ggplot2 - Extracting x-axis intercept from a linear fit in R -


i have data generated using following lines of code,

x <- c(1:10) y <- x^3 z <- y-20 s <- z/3 t <- s*6 q <- s*y x1 <- cbind(x,y,z,s,t,q) x1 <- data.frame(x1) 

i plot x versus y,s, , t melt data frame x1 first,

 library(reshape2)   xm <- melt(x1, id=names(x1)[1], measure=names(x1)[c(2, 4, 5)], variable = "cols"`) 

then plot them along linear fits using following code,

library(ggplot2) plt <- ggplot(xm, aes(x = x, y = value, color = cols)) +   geom_point(size = 3) +   labs(x = "x", y = "y") +    geom_smooth(method = "lm", se = false) plt 

the plot generated shown below,

enter image description here

now liked interpolate x-intercept of linear fit. point in plot y axis value 0.

the following lines of code shown here, extracts slope , y-intercept.

fits <- by(xm[-2], xm$cols, function(i) coef(lm(value ~ x, i))) data.frame(cols = names(fits), do.call(rbind, fits)) 

is there way how can extract x-intercept other manually calculating slope , y-intercept?

thanks help!

you inverse prediction implemented in package chemcal calibrations if don't want calculate yourself:

library(chemcal) res <- by(xm[-2], xm$cols, function(i) inverse.predict(lm(value ~ x, i), 0)$prediction) res[1:3] #xm$cols #y        s        t  #2.629981 2.819734 2.819734 

edit:

maybe prefer this:

library(plyr) res <- ddply(xm, .(cols),    function(i) data.frame(xinter=inverse.predict(lm(value ~ x, i), 0)$prediction)) #   cols   xinter # 1    y 2.629981 # 2    s 2.819734 # 3    t 2.819734 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

inno setup - TLabel or TNewStaticText - change .Font.Style on Focus like Cursor changes with .Cursor -