当我只有一行时,是否要更新plot_model的颜色?

时间:2019-05-25 23:55:28

标签: r

我想更改plot_model输出的图的颜色。当通过在术语= c(...,...)中包含多个变量来创建多行时,我对此没有任何问题,但是当我执行以下代码时,我无法使颜色变为黑色以外的任何颜色:

df_test <- data.frame('subj' = c('Joe', 'Joe', 'Moe', 'Moe'), 'A' = c(1, 0, 1, 0), 'B' = c(3, 2, 1, 4))

m = glmer(A ~ B + (B | subj), data=df_test, family='binomial')

print(plot_model(m, type='pred', colors = 'blue'))

输出:https://imgur.com/a/pApsmH4

我在这里想念什么?

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

要执行此操作,我修改了data.frame的结构,并使用了ggplot2

library(lme4)
library(ggplot2)
library(scales)
library(sjPlot)
df_test <- data.frame('subj' = c('Joe', 'Joe', 'Moe', 'Moe'), 'A' = c(1, 0, 1, 0), 'B' = c(3, 2, 1, 4))

m <- lme4::glmer(A ~ B + (B | subj), data=df_test, family='binomial')

p <- sjPlot::plot_model(m, type='pred', colors = 'blue')

df <- as.data.frame(p$B$data)
df
ggplot2::ggplot(df, aes(x = x)) +
  geom_ribbon(aes(ymin = conf.low, ymax = conf.high), fill = "grey70") +
  geom_line(aes(y = predicted), col = "blue") +
  scale_y_continuous(labels = scales::percent,
                     limits = c(0, 1))

enter image description here

ggplot2::ggplot(df, aes(x = x)) +
  geom_ribbon(aes(ymin = conf.low, ymax = conf.high), 
              fill = "deepskyblue", alpha = 0.25) +
  geom_line(aes(y = predicted), col = "darkred") +
  scale_y_continuous(labels = scales::percent,
                     limits = c(0, 1))

enter image description here

答案 1 :(得分:0)

您可以使用ggeffects-package,它在创建边际效应图时由 sjPlot 内部使用。这使获取数据然后使用ggplot变得容易一些:

library(lme4)
#> Loading required package: Matrix
library(ggeffects)
library(ggplot2)
#> Registered S3 methods overwritten by 'ggplot2':
#>   method         from 
#>   [.quosures     rlang
#>   c.quosures     rlang
#>   print.quosures rlang

df_test <- data.frame('subj' = c('Joe', 'Joe', 'Moe', 'Moe'), 'A' = c(1, 0, 1, 0), 'B' = c(3, 2, 1, 4))
m<- glmer(A ~ B + (B | subj), data=df_test, family='binomial')
#> boundary (singular) fit: see ?isSingular

ggpredict(m, "B") %>%
  ggplot(aes(x, predicted)) +
  geom_ribbon(aes(ymin = conf.low, ymax = conf.high), alpha = .2) +
  geom_line(colour = "blue")

reprex package(v0.3.0)于2019-05-29创建

有几个用于手动构建图的示例,例如herehere