Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logistic regression + histogram with ggplot2

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

enter image description here

Now, is it possible to do this with ggplot2?

like image 424
PaoloCrosetto Avatar asked Nov 04 '15 12:11

PaoloCrosetto


1 Answers

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")
like image 176
Thierry Avatar answered Nov 08 '22 18:11

Thierry