title: How to obtain the ggplot graph for the logistic regression imitation in Wikipedia's example in R?
body: <p>(reproducible example added)</p>
  
  <p>I tried to imitate the Wikipedia's "Probability of passing an exam versus hours of study" logistic regression example <a href="https://en.wikipedia.org/wiki/Logistic_regression" rel="nofollow noreferrer">here</a>:</p>
  
  <p>I could not obtain the same ggplot graph in that page, and I couldn't figure out why.</p>
  
  <pre><code>df &lt;- data.frame(hour=c(0.50,0.75,1.00,1.25,1.50,1.75,1.75,2.00,2.25,2.50,2.75,3.00,3.25,3.50,4.00,4.25,4.50,4.75,5.00,5.50), pass=c(0,0,0,0,0,0,1,0,1,0,1,0,1,0,1,1,1,1,1,1))
  
  df
  #   hour pass
  #1   0.50    0
  #2   0.75    0
  #3   1.00    0
  #4   1.25    0
  #5   1.50    0
  #6   1.75    0
  #7   1.75    1
  #8   2.00    0
  #9   2.25    1
  #10  2.50    0
  #11  2.75    1
  #12  3.00    0
  #13  3.25    1
  #14  3.50    0
  #15  4.00    1
  #16  4.25    1
  #17  4.50    1
  #18  4.75    1
  #19  5.00    1
  #20  5.50    1
  
  df$pass &lt;- as.factor(df$pass)
  my_fit &lt;- glm(df$pass ~ df$hour, data=df, na.action=na.exclude, family="binomial")
  summary(my_fit)
  </code></pre>
  
  <p><em>NON GGPLOT PLOT WORKS PERFECTLY:</em>    </p>
  
  <pre><code>my_table &lt;- summary(my_fit)     
  my_table$coefficients[,1] &lt;- invlogit(coef(my_fit))
  my_table
  
  anova(my_fit)
  library(pscl); pR2(my_fit) # for McFadden rho^2
  
  plot(df$hour, df$pass, xlab="x", ylab="logit values")
  
  LinearPredictions &lt;- predict(my_fit); LinearPredictions
  # LinearPredictions is NOT equal to 0.01666 + 0.81827*(1:20)
  # LinearPredictions is NOT equal to -4.0777+1.5046*(1:20)
  # LinearPredictions are equal to what (I couldn't solve)?
  
  EstimatedProbability.hat &lt;- exp(LinearPredictions)/(1 + exp(LinearPredictions))
  EstimatedProbability.hat
  
  EstimatedProbability &lt;- c(0.25, 0.50, 0.75) # Estimated probabilities for which their x levels are wanted to be found
  
  HoursStudied &lt;- (log(EstimatedProbability/(1- EstimatedProbability)) - my_fit$coefficients[1])/ my_fit$coefficients[2]
  HoursStudied.summary &lt;- data.frame(EstimatedProbability, HoursStudied)
  HoursStudied.summary
  
  plot(df$hour, EstimatedProbability.hat, xlab="studying hours", ylab="estimated probability (pass)") # , xlim=c(0,6), ylim=c(0,1)
  # Add red curve
  lines(df$hour, EstimatedProbability.hat, lty=1, col="red")
  # Vertical dashes
  segments(x0=HoursStudied.summary$HoursStudied, y0=0, x1=HoursStudied.summary$HoursStudied, y1=HoursStudied.summary$EstimatedProbability,
           lty=2, col=c("darkblue","darkred","darkgreen"))
  # Horizontal dashes
  segments(x0=0, y0=HoursStudied.summary$EstimatedProbability, x1=HoursStudied.summary$HoursStudied, 
  y1=HoursStudied.summary$EstimatedProbability, lty=2, col=c("darkblue","darkred","darkgreen"))
  
  legend("bottomright", legend=c("HS0.25", "HS0.50", "HS0.75"), lty=2, col=c("darkblue","darkred","darkgreen"), bty="n", cex=0.75)
  </code></pre>
  
  <p><em>GGPLOT PLOT FAILS:</em><br>
  I tried to do the same thing in <code>ggplot</code>, but failed:</p>
  
  <pre><code>df$EstimatedProbabilities &lt;- EstimatedProbability.hat; df
  HoursStudied.summary$group &lt;- c('HS0.25','HS0.50','HS0.75')
  
  library(ggplot2)
  ggplot(df, aes(x=hour, y=df$pass)) + 
  geom_point() + 
  geom_line(aes(y=EstimatedProbabilities), colour="black") + 
  geom_segment(data=HoursStudied.summary, aes(y=EstimatedProbability,
  xend=HoursStudied, yend=EstimatedProbability, col=group), x=-Inf, linetype="dashed") + 
  geom_segment(data=HoursStudied.summary, aes(x=HoursStudied,
  xend=HoursStudied, yend=EstimatedProbability, col=group), y=-Inf, linetype="dashed")
  </code></pre>
  
  <p>Problem: The <code>ggplot</code> curve is the same with that of <code>plot</code>, however, it draws the whole function below y=0 line. Why?</p>