plot_interaction()Since plot_interaction() just returns a
ggplot object, the output be customized using a combination
of different ggplot2 functions.
Here we fit a simple model, and create a baseline plot, which we’ll customize.
library(modsem)
#> This is modsem (1.0.14). Please report any bugs!
library(ggplot2)
m <- '
X =~ x1 + x2 + x3
Z =~ z1 + z2 + z3
Y =~ y1 + y2 + y3
Y ~ X + Z + X:Z
'
fit <- modsem(m, data = oneInt, method = "lms")
p <- plot_interaction(x = "X", z = "Z", y = "Y", vals_z = c(-1, 1), model = fit)
print(p)The title of the plot can be changed using the ggtitle()
function.
The axis labels can be changed using the xlab() and
ylab() functions.
Changing the legend title is a bit trickier, but it can be done using
the guides() function. Since
plot_interaction() uses both the fill and
color aesthetic, both must be specified, with the same
title.
p + guides(fill=guide_legend(title = "New Legend Title"),
color=guide_legend(title = "New Legend Title"))The colors of both the fill and color
aesthetic can be changed using different functions.
Here we replace the fill aesthetic with a greyscale.
Here we do the same for the
color aesthetic.
Combining both, we get a greyscale version of the plot.
NOTE: As of version 1.0.14 there is a
separate greyscale argument, for using a greyscale theme,
simplifying the process.
ggplot2 comes with a few different color palettes, which
we can use to the replace the default color palette. Here we have
numerous options (you can also download additional packages), but here
we can see a few examples using some of the brewer
palettes.
To see the available palettes you can run this snippet.
Here we have an example using the Dark2 palette.
# We need to apply the color scale to both the fill and color aesthetics
p + scale_fill_brewer(palette = "Dark2") +
scale_color_brewer(palette = "Dark2")Here we have an example using the Accent palette.
# We need to apply the color scale to both the fill and color aesthetics
p + scale_fill_brewer(palette = "Accent") +
scale_color_brewer(palette = "Accent")Changing the linetypes is a more compliacted than changing the colors
in the plot. This is because we have to add a whole new aesthetic to the
plot. This can done using the aes() function. Behind the
scenes, modsem uses a variable cat_z to form
the groups in the output from plot_interaction, which is
why we pass linetype = cat_z.
Here we can see that we had an unfortunate side effect, where we created
a second Legend in our plot. This can however be fixed by renaming the
Legend title (see the Legend Title
section).
Here we can see an example combining the different options.
p +
ggtitle("Customized Plot") + # New Title
xlab("Values of X") + # New X-axis label
ylab("Predicted values of Y") + # New y-axis label
scale_color_grey() + # Add Grey theem
scale_fill_grey() +
aes(linetype = cat_z) + # Add linetypes
guides(color = guide_legend("Values of Z"), # Change Legend Title
fill = guide_legend("Values of Z"),
linetype = guide_legend("Values of Z"))