我有一些二进制数据,我想在同一图中同时绘制逻辑回归线和相对频率0和1的直方图。
我在这里使用popbio软件包遇到了一个非常好的实现:shizuka lab's page
这里有一个用库运行的MWE(popbio)(礼貌的shizuka实验室)
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")
产生
答案 0 :(得分:4)
以下是一些想法
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")