Logistic regression + histogram with ggplot2

I have several binary data, and I want to build a logistic regression line and a histogram of the relative frequencies 0s and 1s in the same section.

I came across a very good implementation using the popbio package here: shizuka lab page

Here's the MWE that works with the library (popbio) (courtesy of shizuka lab)

bodysize=rnorm(20,30,2) # generates 20 values, with mean of 30 & sd=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

enter image description here

Now, can this be done with ggplot2?

+5
source share
1 answer

Here are some ideas

 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") 
+3
source

Source: https://habr.com/ru/post/1235193/


All Articles