title: Predicted values for logistic regression from glm and stat_smooth in ggplot2 are different
body: <p>I'm trying to make this logistic regression graph in <code>ggplot2</code>.</p>
  <pre><code>df &lt;- structure(list(y = c(2L, 7L, 776L, 19L, 12L, 26L, 7L, 12L, 8L,
  24L, 20L, 16L, 12L, 10L, 23L, 20L, 16L, 12L, 18L, 22L, 23L, 22L,
  13L, 7L, 20L, 12L, 13L, 11L, 11L, 14L, 10L, 8L, 10L, 11L, 5L,
  5L, 1L, 2L, 1L, 1L, 0L, 0L, 0L), n = c(3L, 7L, 789L, 20L, 14L,
  27L, 7L, 13L, 9L, 29L, 22L, 17L, 14L, 11L, 30L, 21L, 19L, 14L,
  22L, 29L, 28L, 28L, 19L, 10L, 27L, 22L, 18L, 18L, 14L, 23L, 18L,
  12L, 19L, 15L, 13L, 9L, 7L, 3L, 1L, 1L, 1L, 1L, 1L), x = c(18L,
  19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L,
  32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L,
  45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 59L,
  62L, 63L, 66L)), .Names = c(&quot;y&quot;, &quot;n&quot;, &quot;x&quot;), class = &quot;data.frame&quot;, row.names = c(NA,
  -43L))
  
  
  mod.fit &lt;- glm(formula = y/n ~ x, data = df, weight=n, family = binomial(link = logit),
          na.action = na.exclude, control = list(epsilon = 0.0001, maxit = 50, trace = T))
  summary(mod.fit)
  
  Pi &lt;- c(0.25, 0.5, 0.75)
  LD &lt;- (log(Pi /(1-Pi))-mod.fit$coefficients[1])/mod.fit$coefficients[2]
  LD.summary &lt;- data.frame(Pi , LD)
  LD.summary
  
  
  plot(df$x, df$y/df$n, xlab = &quot;x&quot;, ylab = &quot;Estimated probability&quot;)
  
  lin.pred &lt;- predict(mod.fit)
  pi.hat &lt;- exp(lin.pred)/(1 + exp(lin.pred))
  lines(df$x, pi.hat, lty = 1, col = &quot;red&quot;)
  
  
  segments(x0 = LD.summary$LD, y0 = -0.1, x1 = LD.summary$LD, y1 = LD.summary$Pi,
           lty=2, col=c(&quot;darkblue&quot;,&quot;darkred&quot;,&quot;darkgreen&quot;))
  segments(x0 = 15, y0 = LD.summary$Pi, x1 = LD.summary$LD, y1 = LD.summary$Pi,
           lty=2, col=c(&quot;darkblue&quot;,&quot;darkred&quot;,&quot;darkgreen&quot;))
  legend(&quot;bottomleft&quot;, legend=c(&quot;LD25&quot;, &quot;LD50&quot;, &quot;LD75&quot;), lty=2, col=c(&quot;darkblue&quot;,&quot;darkred&quot;,&quot;darkgreen&quot;), bty=&quot;n&quot;, cex=0.75)
  </code></pre>
  <p><img src="https://i.stack.imgur.com/NCmez.png" alt="enter image description here" /></p>
  <p>Here is my attempt with <code>ggplot2</code></p>
  <pre><code>library(ggplot2)
  
  p &lt;- ggplot(data = df, aes(x = x, y = y/n)) +
              geom_point() +
              stat_smooth(method = &quot;glm&quot;, family = &quot;binomial&quot;)
  
  p &lt;- p + geom_segment(aes(
                              x = LD.summary$LD
                            , y = 0
                            , xend = LD.summary$LD
                            , yend = LD.summary$Pi
                           )
                           , colour=&quot;red&quot;
                         )
  
  p &lt;- p + geom_segment(aes(
                              x = 0
                            , y = LD.summary$Pi
                            , xend = LD.summary$LD
                            , yend = LD.summary$Pi
                           )
                           , colour=&quot;red&quot;
                         )
  
  print(p)
  </code></pre>
  <p><img src="https://i.stack.imgur.com/s3qwT.png" alt="enter image description here" /></p>
  <h2>Questions</h2>
  <ol>
  <li>Predicted values for <code>glm</code> and <code>stat_smooth</code> look different. Are these two methods produces different results or I'm missing something here.</li>
  <li>My ggplot2 graph is not exactly as base R graph.</li>
  <li>How to use different colours for line segments in ggplot2?</li>
  <li>And how to put legend in ggplot2?</li>
  </ol>
  <p>Thanks in advance for your help and time. Thanks</p>