0

I want to plot a 3-way interaction between continuous variables and show the data.

I get three panels side by side and the marginal means for each group factor (tiv).

I now want to adding the data points manually using geom_point and facet_grid/facet_wrap and a different grouping factor. However, when I do that the lines plotting the marginal means now connect the data points. This only happens when I split the data points into panels, ie. when I add facet_grid/facet_wrap. However, when I don't add facet_grid all the data points are shown in all panels.

Does anyone know how to fix this?

The code and figure is below.

library(lmertest)
library(sjPlot)
library(ggplot2)

tiv <- rnorm(100)
dbs <- rnorm(100)
cs <- rnorm(100)
dt <- rep(c(0:3), 25)
sbj <- rep(c(1:25), each=4)
tmp <- data.frame(tiv, dbs, cs, dt, sbj)

tmp$dbs_group <- cut(tmp$dbs, c(-2.5, -0.84, 0.83, 2.5), labels = c(-1.01, -0.03, 0.95)) 
tmp$tiv_group <- cut(tmp$tiv, c(-2.7, -0.74, 0.68, 2.4), labels = c(-1.11, 0.01, 1.13)) 


mmod <- lmer(cs ~ dt*tiv*dbs + (dt | sbj), data = tmp, control=lmerControl(optimizer="bobyqa")) 

p <- plot_model(mmod, type = "emm", terms = c("dt [all]", "dbs", "tiv"), 
                mdrt.values = "quart", show.legend = T, line.size = 2, show.data =F) 
p + geom_point(data = tmp, mapping = aes(x = dt, y = cs, group = dbs_group, colour = dbs_group), inherit.aes = F) + 
  scale_fill_manual(values = c("red", "blue", "black")) + scale_color_manual(values = c("red", "blue", "black"))+ facet_grid(~tiv_group)

enter image description here

The figure below is created without using facet_grid. All the datapoints are shown in all panels.

enter image description here

Ideally what I am looking for is a solution where the datapoints are split into different panels, and the lines plot the marginal means.

EDIT: After playing more with it using ggpredict, I think I figured that the problem is with the two facet_* statements. Only one seems to apply. Is there a way to overlay two datasets split across the same panels?

Here is a snippet of the code that I tried with ggpredict (I plot both as points, because it helps see what's happening with the lines)

library(ggeffects)
pr <- ggpredict(mmod, terms = c("dt [all]", "dbs", "tiv"), type = "fe")
ggplot(pr, aes(x=x, y = predicted, colour = group)) + facet_wrap(~facet) + geom_point() + geom_point(data = tmp, mapping = aes(x = dt, y = cs, group = dbs_group, colour = dbs_group), inherit.aes = F) +  facet_wrap(~tiv_group)

When I ran this line, all the data from "pr" are plotted in all panels. If I remove the last facet_* statement then the data from "pr" are split correctly into panels, but the data from "tmp" are now shown in all panels!

Many thanks for any help! Marina

leonheess
  • 16,068
  • 14
  • 77
  • 112
  • Can you provide a reproducible example of your data ? see: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – dc37 Jan 23 '20 at 15:41
  • 1
    Thank you @dc37 . I just updated the post to include a test dataset. Many thanks – user12379599 Jan 23 '20 at 16:25

0 Answers0