如何在R studio的概率图中添加显示条件变量分布的层?

如何在R studio的概率图中添加显示条件变量分布的层?,r,ggplot2,plot,predict,sjplot,R,Ggplot2,Plot,Predict,Sjplot,我正在拟合以下回归: model显然,我必须重新创建您的数据才能使其正常工作,因此它不会忠实于您的原始数据,但如果我们假设您的绘图是这样的: p <- plot_model(model, type = "pred", terms = c("predictor [all]", "conditions")) + xlab("Xlab") + ylab("Ylab") + theme_

我正在拟合以下回归:
model显然,我必须重新创建您的数据才能使其正常工作,因此它不会忠实于您的原始数据,但如果我们假设您的绘图是这样的:

p <- plot_model(model, type = "pred", terms = c("predictor [all]", "conditions")) +
  xlab("Xlab") +
  ylab("Ylab") +
  theme_minimal() +
  ggtitle("Title")

p
p + geom_histogram(data = d, inherit.aes = FALSE, 
                   aes(x = predictor, y = ..count../1000),
                   fill = "gray85", colour = "gray50", alpha = 0.3)

如果你想在ggplot中完成整个过程,你需要记住告诉
geom_smooth
你的glm是一个probit模型,否则它将只适合一个正态线性回归。对于这个例子,我也复制了调色板,不过请注意,组的平滑线从它们的最低x值开始,而不是推回0

ggplot(d, aes(x = predictor, y = DV, color = conditions))+
  geom_smooth(method = "glm", aes(fill = conditions),
              method.args = list(family = binomial(link = "probit")),
              alpha = 0.15, size = 0.5) +
  xlab("Xlab") +
  scale_fill_manual(values = c("#e41a1c", "#377eb8")) +
  scale_colour_manual(values = c("#e41a1c", "#377eb8")) +
  ylab("Ylab") +
  theme_minimal() +
  ggtitle("Title") + 
  geom_histogram(aes(y = ..count../1000),
                 fill = "gray85", colour = "gray50", alpha = 0.3)


数据

set.seed(69)

n_each     <- 500
predictor  <- rgamma(2 * n_each, 2.5, 3)
predictor  <- 1 - predictor/max(predictor)
log_odds   <- c((1 - predictor[1:n_each]) * 5 - 3.605, 
              predictor[n_each + 1:n_each] * 0 + 0.57)
DV         <- rbinom(2 * n_each, 1, exp(log_odds)/(1 + exp(log_odds)))
conditions <- factor(rep(c("  ", " "), each = n_each))
d          <- data.frame(DV, predictor, conditions)
set.seed(69)

n_每一个我想这将是有益的,如果你愿意有频率以外的情节非常感谢你。这太完美了!我的数据真的令人印象深刻!再次感谢你,你刚刚救了我的周末。
p + geom_histogram(data = d, inherit.aes = FALSE, 
                   aes(x = predictor, y = ..count../1000),
                   fill = "gray85", colour = "gray50", alpha = 0.3)
ggplot(d, aes(x = predictor, y = DV, color = conditions))+
  geom_smooth(method = "glm", aes(fill = conditions),
              method.args = list(family = binomial(link = "probit")),
              alpha = 0.15, size = 0.5) +
  xlab("Xlab") +
  scale_fill_manual(values = c("#e41a1c", "#377eb8")) +
  scale_colour_manual(values = c("#e41a1c", "#377eb8")) +
  ylab("Ylab") +
  theme_minimal() +
  ggtitle("Title") + 
  geom_histogram(aes(y = ..count../1000),
                 fill = "gray85", colour = "gray50", alpha = 0.3)
set.seed(69)

n_each     <- 500
predictor  <- rgamma(2 * n_each, 2.5, 3)
predictor  <- 1 - predictor/max(predictor)
log_odds   <- c((1 - predictor[1:n_each]) * 5 - 3.605, 
              predictor[n_each + 1:n_each] * 0 + 0.57)
DV         <- rbinom(2 * n_each, 1, exp(log_odds)/(1 + exp(log_odds)))
conditions <- factor(rep(c("  ", " "), each = n_each))
d          <- data.frame(DV, predictor, conditions)