Plot logistic regression curve in R
<p>I want to plot a logistic regression curve of my data, but whenever I try to my plot produces multiple curves. Here's a picture of my last attempt:</p>

<p><a href="http://i.stack.imgur.com/EvXZg.png" rel="noreferrer">last attempt</a></p>

<p>Here's the relevant code I am using:</p>

<pre><code>fit = glm(output ~ maxhr, data=heart, family=binomial)
predicted = predict(fit, newdata=heart, type="response")

 plot(output~maxhr, data=heart, col="red4")
 lines(heart$maxhr, predicted, col="green4", lwd=2)
</code></pre>

<p>My professor uses the following code, but when I try to run it I get an error on the last line saying that the x and y lengths do not match:</p>

<pre><code># fit logistic regression model
fit = glm(output ~ maxhr, data=heart, family=binomial)
# plot the result
hr = data.frame(maxhr=seq(80,200,10))
probs = predict(fit, newdata=dat, type="response")
plot(output ~ maxhr, data=heart, col="red4", xlab ="max HR", ylab="P(heart disease)")
lines(hr$maxhr, probs, col="green4", lwd=2)
</code></pre>

<p>Any help would be appreciated.</p>

<p><strong>Edit:</strong></p>

<p>As requested, reproduceable code using the mtcars dataset:</p>

<pre><code>fit = glm(vs ~ hp, data=mtcars, family=binomial)
predicted= predict(fit, newdata=mtcars, type="response")
plot(vs~hp, data=mtcars, col="red4")
lines(mtcars$hp, predicted, col="green4", lwd=2)
</code></pre>