I am creating some interaction plots and want to add a 95% confidence interval for my interaction terms. However, using the interact_plot()
function from the interactions
package adds a geom_ribbon()
for the confidence interval, which I find difficult to read. I would like to simply have dashed lines to represent the upper and lower bounds of the confidence interval.
It seems to me this involves updating or changing the layers from the list that is stored for my plot, but I am not quite sure how to do this.
Here is an example:
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
Instead of having the ribbon, I want to just have dashed lines representing the lower and upper bounds of the confidence interval as said above.
Looking at the summary of the plot object yields:
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
You'll notice that the mapping layer mapping: x = ~Anger, ymin = ~ymin, ymax = ~ymax, fill = ~mode, group = ~mode, colour = ~mode, linetype = NA
has linetype = NA
. My thought is that this needs to change to accomplish my goal, but I do not know how I can change the layer.
Any insight would be greatly appreciated! It does not seem that interact_plot()
has an argument that will allow me to do this directly, so I think it requires some wrangling.
CodePudding user response:
One option to achieve your desired result would be to
- Add lines for the confidence intervals using two
geom_line
- Get rid of the
geom_ribbon
by setting the fill colors to transparent - Add the colors back where I used the default colors used by
interact_plot
(Note:interact_plot
uses one scale for both fill and color. Hence, when adding scale_fill_manual we also lose the colors).
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.