Adding gridlines to stats package interaction plot

Summary

Is there a way to add gridlines to interaction plots made with the stats package?

If gridlines are unfeasible, then adding y-axis point labels would also be helpful.

I'm also open to using a different package to create a similar plot.

Example:

#make this example reproducible
set.seed(10)

#create data frame
data <- data.frame(gender = rep(c("Male", "Female"), each = 30),
                   exercise = rep(c("None", "Light", "Intense"), each = 10, times = 2),
                   weight_loss = c(runif(10, -3, 3), runif(10, 0, 5), runif(10, 5, 9),
                                   runif(10, -4, 2), runif(10, 0, 3), runif(10, 3, 8)))

#create interaction plot

interaction.plot(x.factor = data$exercise, #x-axis variable
                 main = paste("Interaction plot of weight loss and exercise intensity",
                              "This example is slightly modified from this source:",
                              "https://www.statology.org/interaction-plot-r/",
                              sep="\n"),
                 trace.factor = data$gender, #variable for lines
                 response = data$weight_loss, #y-axis variable
                 fun = median, #metric to plot
                 ylab = "Weight Loss",
                 xlab = "Exercise Intensity",
                 col = c("red", "blue"),
                 lty = 1, #line type
                 lwd = 2, #line width
                 trace.label = "Gender",
                 type = "b",
                 pch = c(19,17))

Try running the grid() function immediately after running interaction.plot() and see if that gets you what you want.

That's exactly what I needed! Thank you.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.