按组

时间:2016-11-15 22:10:52

标签: r sjplot

我试图绘制固定效果和随机因素之间的相互作用。 sjPlot似乎是一个很好的包,但我无法更改线型和颜色。我想将线条颜色更改为具有不同线条类型的灰度方案,以区分组。我已尝试使用geom.color参数和sjp.setTheme函数,但到目前为止还未能获得所需的结果。

下面的示例代码显示了我的初步尝试,借鉴sjPlot网站上的示例:

data(efc)

efc$hi_qol <- dicho(efc$quol_5)

efc$grp = as.factor(efc$e15relat)
  levels(x = efc$grp) <- get_labels(efc$e15relat)

mydf <- data.frame(hi_qol = efc$hi_qol,
                   sex = to_factor(efc$c161sex),
                   c12hour = efc$c12hour,
                   neg_c_7 = efc$neg_c_7,
                   grp = efc$grp)
fit <- glmer(hi_qol ~ sex + c12hour + neg_c_7 + (1 | grp),
              data = mydf, family = binomial("logit"))

sjp.glmer(fit, type="ri.slope", facet.grid=F, vars="neg_c_7")

要更改线条颜色,我尝试设置geom.colors="black",但这似乎没有做任何事情。

sjp.glmer(fit, type="ri.slope", facet.grid=F, geom.colors="black", vars="neg_c_7")

接下来我尝试更改sjPlot使用的主题来更改线型,但这也没有用。

sjp.setTheme(geom.linetype = c(1:8))
sjp.glmer(fit, type="ri.slope", facet.grid=F, vars="neg_c_7")

我是否遗漏了一些明显的或正在改变线条类型和颜色的更复杂的内容?

2 个答案:

答案 0 :(得分:0)

我不知道如何使用sjplot执行此操作,但您可以使用interaction.plot函数生成绘图并添加col= c(...)以更改线条的颜色。

interaction.plot(factor, factor, fit, .. col = c("red","blue"))

答案 1 :(得分:0)

sjPlot-package不支持更改线型 - 仅支持颜色。线型美学目前不是由sjp函数映射的。但是,您可以访问用于绘图的数据并创建自己的交互图:

library(ggplot2)
library(sjmisc)
data(efc)
# create binary response
y <- ifelse(efc$neg_c_7 < median(stats::na.omit(efc$neg_c_7)), 0, 1)
# create data frame for fitted model
mydf <- data.frame(y = as.factor(y),
sex = as.factor(efc$c161sex),
barthel = as.numeric(efc$barthtot))
# fit model
fit <- glm(y ~ sex * barthel, data = mydf, family = binomial(link = "logit"))

p <- sjp.int(fit, geom.colors = "gs")
ggplot(p$data.list[[1]], aes(x = x, y = y, linetype = grp)) + geom_line()

enter image description here

更改交互图的颜色与geom.colors - 参数一起使用,请参阅?sjp.grpfrq中的详细信息

相关问题