在 interact_plot 中更改间隔功能区
Changing the interval ribbon in interact_plot
我正在创建一些交互图并想为我的交互项添加 95% 的置信区间。但是,使用 interactions
包中的 interact_plot()
函数会为置信区间添加一个 geom_ribbon()
,我觉得这很难阅读。我想简单地用虚线表示置信区间的上限和下限。
在我看来,这涉及更新或更改为我的绘图存储的列表中的图层,但我不太确定如何执行此操作。
这是一个例子:
library(lme4)
library(tidyverse)
library(interactions)
data(VerbAgg)
mv <- glmer(r2 ~ Anger * mode + (1 | item), data = VerbAgg,
family = binomial,
control = glmerControl("bobyqa"))
p = interact_plot(mv, pred = Anger, modx = mode, vary.lty = FALSE, interval = TRUE, types = c("solid", "solid"))
p
我不想用色带,而是用虚线表示置信区间的下限和上限,如上所述。
查看绘图对象的摘要:
summary(p)
data: r2, item, mode, Anger, ymax, ymin, modx_group [200x7]
mapping: x = ~Anger, y = ~r2, colour = ~mode, group = ~mode, linetype = NULL
scales: colour, fill
faceting: <ggproto object: Class FacetNull, Facet, gg>
compute_layout: function
draw_back: function
draw_front: function
draw_labels: function
draw_panels: function
finish_data: function
init_scales: function
map_data: function
params: list
setup_data: function
setup_params: function
shrink: TRUE
train_scales: function
vars: function
super: <ggproto object: Class FacetNull, Facet, gg>
-----------------------------------
geom_path: lineend = butt, linejoin = round, linemitre = 10, arrow = NULL, na.rm = FALSE, size = 1
stat_identity: na.rm = FALSE
position_identity
mapping: x = ~Anger, ymin = ~ymin, ymax = ~ymax, fill = ~mode, group = ~mode, colour = ~mode, linetype = NA
geom_ribbon: na.rm = FALSE, orientation = NA, outline.type = both, alpha = 0.2, flipped_aes = FALSE
stat_identity: na.rm = FALSE
position_identity
您会注意到映射层 mapping: x = ~Anger, ymin = ~ymin, ymax = ~ymax, fill = ~mode, group = ~mode, colour = ~mode, linetype = NA
有 linetype = NA
。我的想法是,这需要更改以实现我的目标,但我不知道如何更改图层。
如有任何见解,我们将不胜感激! interact_plot()
似乎没有允许我直接这样做的参数,所以我认为这需要一些争论。
实现您想要的结果的一个选择是
- 使用两个
geom_line
添加置信区间线
- 通过将填充颜色设置为透明来摆脱
geom_ribbon
- 将颜色添加回我使用
interact_plot
使用的默认颜色的位置(注意:interact_plot
对填充和颜色使用一个比例。因此,在添加 scale_fill_manual 时,我们还失去颜色)。
library(lme4)
library(ggplot2)
library(interactions)
p +
geom_line(aes(Anger, ymin), linetype = "dashed") +
geom_line(aes(Anger, ymax), linetype = "dashed") +
scale_fill_manual(values = c("transparent", "transparent"), aesthetics = "fill") +
scale_color_manual(values = jtools::get_colors("CUD Bright", 2), aesthetics = "color")
#> Scale for 'colour' is already present. Adding another scale for 'colour',
#> which will replace the existing scale.
我正在创建一些交互图并想为我的交互项添加 95% 的置信区间。但是,使用 interactions
包中的 interact_plot()
函数会为置信区间添加一个 geom_ribbon()
,我觉得这很难阅读。我想简单地用虚线表示置信区间的上限和下限。
在我看来,这涉及更新或更改为我的绘图存储的列表中的图层,但我不太确定如何执行此操作。
这是一个例子:
library(lme4)
library(tidyverse)
library(interactions)
data(VerbAgg)
mv <- glmer(r2 ~ Anger * mode + (1 | item), data = VerbAgg,
family = binomial,
control = glmerControl("bobyqa"))
p = interact_plot(mv, pred = Anger, modx = mode, vary.lty = FALSE, interval = TRUE, types = c("solid", "solid"))
p
我不想用色带,而是用虚线表示置信区间的下限和上限,如上所述。
查看绘图对象的摘要:
summary(p)
data: r2, item, mode, Anger, ymax, ymin, modx_group [200x7]
mapping: x = ~Anger, y = ~r2, colour = ~mode, group = ~mode, linetype = NULL
scales: colour, fill
faceting: <ggproto object: Class FacetNull, Facet, gg>
compute_layout: function
draw_back: function
draw_front: function
draw_labels: function
draw_panels: function
finish_data: function
init_scales: function
map_data: function
params: list
setup_data: function
setup_params: function
shrink: TRUE
train_scales: function
vars: function
super: <ggproto object: Class FacetNull, Facet, gg>
-----------------------------------
geom_path: lineend = butt, linejoin = round, linemitre = 10, arrow = NULL, na.rm = FALSE, size = 1
stat_identity: na.rm = FALSE
position_identity
mapping: x = ~Anger, ymin = ~ymin, ymax = ~ymax, fill = ~mode, group = ~mode, colour = ~mode, linetype = NA
geom_ribbon: na.rm = FALSE, orientation = NA, outline.type = both, alpha = 0.2, flipped_aes = FALSE
stat_identity: na.rm = FALSE
position_identity
您会注意到映射层 mapping: x = ~Anger, ymin = ~ymin, ymax = ~ymax, fill = ~mode, group = ~mode, colour = ~mode, linetype = NA
有 linetype = NA
。我的想法是,这需要更改以实现我的目标,但我不知道如何更改图层。
如有任何见解,我们将不胜感激! interact_plot()
似乎没有允许我直接这样做的参数,所以我认为这需要一些争论。
实现您想要的结果的一个选择是
- 使用两个
geom_line
添加置信区间线
- 通过将填充颜色设置为透明来摆脱
geom_ribbon
- 将颜色添加回我使用
interact_plot
使用的默认颜色的位置(注意:interact_plot
对填充和颜色使用一个比例。因此,在添加 scale_fill_manual 时,我们还失去颜色)。
library(lme4)
library(ggplot2)
library(interactions)
p +
geom_line(aes(Anger, ymin), linetype = "dashed") +
geom_line(aes(Anger, ymax), linetype = "dashed") +
scale_fill_manual(values = c("transparent", "transparent"), aesthetics = "fill") +
scale_color_manual(values = jtools::get_colors("CUD Bright", 2), aesthetics = "color")
#> Scale for 'colour' is already present. Adding another scale for 'colour',
#> which will replace the existing scale.