0

Im am using the interact_plot function to plot a 3-way interaction. I would like to add the Johnson-Neyman region of significance by adding some vertical and horizontal lines. When I do the simple plot with jtools, the only significant curve is with the mod2 - 1 SD group. I would like to only included the JN interval (vline and hline) in the mod2 -1 SD panel (left panel).

This is my script that works but applies vertical and horizontal lines to all 3 mod2 panels.

png(filename = 'interaction_plot.png', width=1000, height=400)
P=interact_plot(fit4, pred = mean_centered_Pren_CESD, modx = gender_male, mod2 = 
mean_centered_auc_post_cesd, interval = TRUE, plot.points = TRUE,colors = c("blue", 
 "orange"),
                x.label = "Prenatal depression",
                y.label = "ADHD",
                pred.labels = "Prenatal depression",
                modx.labels = c("Girls"=0,"Boys"=1),
                mod2.labels = c("A) Low postnatal depression","B) Average postnatal 
                depression", "C) High postnatal depression"),
                main.title = NULL,
                legend.main = "Sex")
P +
  drop_gridlines() +
  geom_vline(xintercept=c(-0.06, 1.74), linetype='dashed', size=1) +
  geom_hline(yintercept=c(-0.14, 1.33), linetype='solid', size=1)
dev.off()

In these lines of codes I would like to only apply "

geom_vline(xintercept=c(-0.06, 1.74), linetype='dashed', size=1) +
  geom_hline(yintercept=c(-0.14, 1.33), linetype='solid', size=1)

" to the mod2 group that is "Low" based on the default -1SD grouping. Can someone help me, I've tried a bunch of things and I can't figure out to call that left panel.

Thank you so much.

stefan
  • 90,330
  • 6
  • 25
  • 51
Lyly
  • 23
  • 2

1 Answers1

0

Using mtcars as example data set this could be generally achieved like so:

  1. Put the coordinates for the vertical or horizontal lines in a dataframe.
  2. Add the facetting variable to this dataframe.
  3. Put xintercept and yintercept inside aes()

This way you can specify in which panels you want the geom_vline and/or geom_hline. In my example both are only drawn in the first panel for cyl = 4.

library(ggplot2)

vh_line <- data.frame(
  xintercept = c(100, 200),
  yintercept = c(10, 20),
  cyl = c(4, 4)
)

ggplot(mtcars, aes(hp, mpg, color = factor(cyl))) +
  geom_point() +
  geom_vline(data = vh_line, aes(xintercept = xintercept), linetype='dashed', size=1) +
  geom_hline(data = vh_line, aes(yintercept = yintercept), linetype='solid', size=1) +
  facet_wrap(~cyl)

stefan
  • 90,330
  • 6
  • 25
  • 51
  • the package interactions which has the function interact_plot does the facetting already – Lyly Sep 03 '20 at 15:12
  • @Lyly. Yep. I didn't meant to add another facet_wrap. I just wanted to illustrate to you the general approach on how to put a geom_v/hline on only one of the panels in a facetted plots. If you don't get this to work for your case I would recommend you add some sample data to your post and the packages you used for your analysis. Otherwise it's hard to figure out a solution to your specific case. See also [how to make a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – stefan Sep 03 '20 at 15:54