If you have only one predictor x, you can use it on the x axis, on the y axis you can put the binary outcome and additionaly, you can plot the function with the predicted values. Here is such a plot:
Further, keep in mind that "one very common way of assessing the usefulness of a binary classifier is the ROC curve" (here). So you can also make a ROC curve plot for a logistic regression. For the example above the ROC curve looks like this:
If you are not familiar with roc curves you can see the link above or here.
The R Code
library(ggplot2)
set.seed(1) # make it reproducible
x1 = rnorm(1000) # some continuous variables
z = 1 + 5*x1 # linear combination with a bias
pr = 1/(1+exp(z)) # pass through an inv-logit function
y = rbinom(1000,1,pr) # bernoulli response variable
df = data.frame(y=y,x1=x1) # make a dataframe
lmodel <- glm( y~x1,data=df,family="binomial") # now feed it to glm:
df$pred <- predict(lmodel, type = 'response') # save predicted values
# plot it
ggplot(df, aes(x1, y)) +
geom_point() +
geom_line(aes(x = x1, y = pred), color = 'red', size = 0.3)
# ROC
# vector of tresholds
treshold <- c(-Inf, seq(min(x1), max(x1), 0.1), Inf)
# calculate sensitivity and specifiticity per treshold
results <- sapply(treshold, function(treshold_i){
# what cases are over the treshold?
test_results <- factor(x1 >= treshold_i, levels=c(TRUE, FALSE))
# create a table
table_results <- table(test_results, y)
# estimate sensitivity
sens <- table_results[1, 1]/ sum(table_results[ , 1])
# estimate specifiticity
spec <- table_results[2, 2]/ sum(table_results[ , 2])
# save both and the used treshold in a matrix
m <- matrix(c(treshold_i, sens, spec), ncol= 3)
# return matrix
return(m)
})
# organize the data
# flip matrix
results <- t(results)
# name columns
colnames(results) <- c("treshold", "sens", "spec")
# ROC curve (2nd plot)
plot(1 - results[ , "spec"], results[ , "sens"], type= "l", col= "red",
xlab= "1 - Specificity", ylab= "Sensitivity")
# diagonal line
abline(0, 1)