I have some binary data, and I want to plot both a logistic regression line and the histogram of relative frequencies of 0s and 1s in the same plot.
I ran into a very nice implementation using the package popbio here: shizuka lab's page
Here a MWE that runs with library(popbio) (courtesy shizuka lab)
bodysize=rnorm(20,30,2) # generates 20 values, with mean of 30 & s.d.=2
bodysize=sort(bodysize) # sorts these values in ascending order.
survive=c(0,0,0,0,0,1,0,1,0,0,1,1,0,1,1,1,0,1,1,1) # assign 'survival' to these 20 individuals non-randomly... most mortality occurs at smaller body size
dat=as.data.frame(cbind(bodysize,survive))
#and now the plot
library(popbio)
logi.hist.plot(bodysize,survive,boxp=FALSE,type="hist",col="gray")
which produces
Here are some idea's
ggplot(dat, aes(x = bodysize, y = survive)) +
geom_dotplot(
aes(fill = factor(survive)), method = "histodot", binpositions = "all",
stackgroups = TRUE, stackdir = "centerwhole", binwidth = 1
) +
geom_smooth(method = "glm", family = "binomial")
ggplot(dat, aes(x = bodysize, y = survive)) +
geom_hex(bins = 10) +
geom_smooth(method = "glm", family = "binomial")
ggplot(dat, aes(x = bodysize, y = survive)) +
geom_bin2d(bins = 10) +
geom_smooth(method = "glm", family = "binomial")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With