'Overlay geom_histogram onto geom_smooth, so that histogram of predictor variable is on the x-axis?

I am trying to mimic the output of Figure 4.6 in Medical Risk Prediction Models: With Ties to Machine Learning, located here

Plot of predicted risk overlayed with histogram of predictor variable

The figure is a plot of the predicted risk of ovarian hyperstimulation syndrome (OHSS) by age. The data set, ivf, is located here

I am unable to overlay a histogram of age on the x-axis of the smooth plot of the logistic regression, using R.

library(ggplot2)

ggplot(ivf) + 
geom_smooth(aes(x=age, y=ohss), method=glm, method.args= list(family="binomial"))

ggplot(ivf) + 
geom_smooth(aes(x=age, y=ohss), method=glm, method.args= list(family="binomial")) + 
geom_histogram(aes(x=age), inherit.aes=FALSE, fill="white", colour="gray50", binwidth=5)`


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source