An alternative that uses functions in package languageR
. I call your data set df
.
library(lme4)
library(languageR)
library(ggplot2)
# fit model
# n.b. I don't claim that this is a sensible model
# It is just used to demonstrate the plot
mod <- lmer(DV ~ TMT1 * TMT2 + (1|Block), data = df)
# create MCMC matrix
mcmc <- pvals.fnc(mod, nsim = 1000, withMCMC = TRUE)
# pval.fnc also calculates MCMC-based p-values and HPD confidence intervals,
# and plot the posterior distributions of the parameters
# plot using plotLMER.fnc
# in addition, set withList = TRUE to create a list of data frames with plot data
# which can be used for a (possibly prettier) plot in ggplot
ll <- plotLMER.fnc(mod, withList = TRUE, pred = "TMT1",
intr = list(
"TMT2",
c("C", "D"),
"end",
list(c("red", "blue"), rep(1, 2))),
addlines = TRUE,
mcmcMat = mcmc$mcmc)
# here follows additional steps to plot using ggplot
# convert list to data frame
df <- do.call(rbind, ll$TMT1)
# rename
names(df)[names(df) == "Levels"] <- "TMT1"
# add TMT2
df$TMT2 <- rep(c("C", "D"), each = 2)
# plot using ggplot
dodge <- position_dodge(width = 0.1)
ggplot(data = df, aes(x = TMT1, y = Y, col = TMT2, group = TMT2)) +
geom_point(position = dodge, size = 3) +
geom_errorbar(aes(ymax = upper, ymin = lower, width = 0.1), position = dodge) +
geom_line(position = dodge) +
ylab("DV") +
theme_classic()