r - 使用 ggplot2 进行逻辑回归 + 直方图

标签 r ggplot2 histogram logistic-regression

我有一些二进制数据,我想在同一个图中绘制逻辑回归线和 0 和 1 的相对频率的直方图。
我在这里使用 popbio 包遇到了一个非常好的实现:shizuka lab's page
这是一个与库(popbio)一起运行的 MWE(由静香实验室提供)

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")
产生
enter image description here
现在,可以用 ggplot2 做到这一点吗?

最佳答案

这里有一些想法

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")

关于r - 使用 ggplot2 进行逻辑回归 + 直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33521539/

相关文章:

html - 如何避免在 R Shiny 应用程序中显示为 HTML 标题的代码?

linux - 如何在从 R 中的 GPS 接收到静态 map PNG 文件时显示坐标?

r - 如何使用ggplot2在刻度上添加单位?

r - geom_dotplot 中未对齐的点

Python - 如何在多个直方图上具有相同的最大值

python - 使用 matplotlib 中的 dataframe.plot() 函数编辑条的宽度

r - 在另一个模块中使用一个 Shiny 模块的变量值

r - map_dfr 输出一行而不是一列

r - 如何使用 ggplot 自定义多面图中的背景颜色

python - 操作 pandas 中的直方图