如何使用Sjplot更改facet_grid和点估计的形状?

时间:2019-05-05 13:52:04

标签: r ggplot2 sjplot

我需要使用plot_model自定义我的图,plot_model是用于绘制广义线性混合效应模型的函数。 我既有显示系数的森林图,也有显示概率的线图。

在下面的森林图中,我有两个年龄级别的年龄因子(年轻人,老年人)。我要实现的是更改两组的点估计形状,例如,为年轻人显示一个圆圈,为老年人显示一个菱形。在下图中,我希望“ Age:New”,“ Age:Lure”,“ Age:Old”具有不同的形状。但是,我找不到允许我这样做的任何其他参数。有想法吗?

森林图

enter image description here

类似地,在下面的线图中,我想将facet_grid标签从0和1更改为“年轻成年人”和“老年人”。 ggplot2是可以实现的,但是当我使用sjplot时,我想知道是否可以在其中使用相同的方法来实现它。

线图

enter image description here

我已经绘制了模型,但是我想获得一些额外的灵活性。

这是森林图的代码:

plot_model(model1_original_conf, type = "est", transform = NULL, terms = c("ZcConfusabilitySimWeightedSim",
                                                                           "Age1:ZcConfusabilitySimWeightedSim",
                                                                           "Conditionlure:ZcConfusabilitySimWeightedSim",
                                                                           "Conditionold:ZcConfusabilitySimWeightedSim",
                                                                           "Age1:Conditionlure:ZcConfusabilitySimWeightedSim",
                                                                           "Age1:Conditionold:ZcConfusabilitySimWeightedSim"),

           colors = c("seagreen3", "deepskyblue2", "orchid"), 
           show.values = TRUE,
           value.offset = .4,
           value.size = 6,
           dot.size = 6,
           line.size = 2.5,
           vline.color = "red",
           width = 0.5,
           axis.labels = c("Age:New", "New", "Age:Lure", "Lure", "Age:Old", "Old"),
           title = "Semantic Confusability",
           order.terms = c(4,6,3,5,1,2),
           group.terms = c(1,1,2,3,2,3))

这是折线图的代码:

plot_model(model1_original_conf, type = "pred",
           terms = c("ZcConfusabilitySimWeightedSim", "Condition", "Age"),
           colors = c("seagreen3", "deepskyblue2", "orchid"),
           title = "Raw probabilities",
           axis.title = c("Semantic Confusability", "p (judge item is OLD)"),
           line.size = 2.0)

我希望找到1)用于森林图中点估计的不同形状(例如圆形和三角形或菱形),以及2)用于线图的0和1的适当标签(“年轻人”和“老年人”) “)。

1 个答案:

答案 0 :(得分:1)

  

但是,我没有发现任何其他允许我这样做的论点。有想法吗?

您需要ggplot2::aes()来更新映射,然后可以将scale_shape_manual()添加到从plot_model()返回的绘图中。

示例:

library(sjPlot)
library(ggplot2)
m <- lm(Sepal.Length ~ Petal.Width + Petal.Length + Species, data = iris)

plot_model(m, group.terms = c(1, 1, 2, 2)) + 
  aes(shape = group) +
  scale_shape_manual(values = c(1, 4))

reprex package(v0.2.1)于2019-05-06创建

  

我想将facet_grid标签从0和1更改为“年轻人”和“老年人”。

当您拥有一个具有所需因子水平的因子时,这应该起作用:

library(sjPlot)
library(ggplot2)
library(sjmisc)

set.seed(123)
iris$binary <- sample(c(0, 1), nrow(iris), replace = T)
iris$Sepal.Length_d <- sjmisc::dicho(iris$Sepal.Length)
m2 <- glm(Sepal.Length_d ~ Petal.Width + Petal.Length * Species * binary, data = iris, family = binomial())
plot_model(m2, type = "pred", terms = c("Petal.Length", "Species", "binary"), ci.lvl = NA)

iris$binary <- sjmisc::rec(iris$binary, rec = "0=young adults;1=older adults", as.num = FALSE)
m3 <- glm(Sepal.Length_d ~ Petal.Width + Petal.Length * Species * binary, data = iris, family = binomial())
plot_model(m3, type = "pred", terms = c("Petal.Length", "Species", "binary"), ci.lvl = NA)

reprex package(v0.2.1)于2019-05-06创建