Reputation: 461
So I need to plot the interaction of a factor variable using interplot
in R. I have been able to figure everything out except an important part of it: how to change the label of the factor that gets plotted. Here's a replicable example showing the issue:
set.seed(507)
df <- data.frame(
outcome = sample(1:7, 1000, replace = T),
scale = sample(1:7, 1000, replace = T),
dummy = sample(0:2, 1000, replace = T))
# factor the dummy
df$dummyf <- factor(df$dummy)
# linear model
lm.out <- lm(outcome ~ scale * dummyf, data = df)
# interplot
library(interplot)
interplot(lm.out, "dummyf", "scale", plot = T, hist = F, ci = 0.95)
Once I plot the interaction here's what I get:
Now, I need to be able to change the
dummyf1
and dummyf2
labels in the facets to read essentially LABEL1
and LABEL2
. Here's a possible solution I tried but that isn't getting me what I need:
# possible solution?
levels(df$dummyf)[levels(df$dummyf) == 1] <- "LABEL1"
levels(df$dummyf)[levels(df$dummyf) == 2] <- "LABEL2"
# linear model
lm.out.1 <- lm(outcome ~ scale * dummyf, data = df)
# interplot
library(interplot)
interplot(lm.out, "dummyf", "scale", plot = T, hist = F, ci = 0.95)
I also tried to modify the facets of ggplot2
since interplot
uses ggplot2
but haven't been able to get it to work either. Any suggestions? Thanks in advance!
Upvotes: 1
Views: 798
Reputation: 70653
You will have to modify the underlying code. It just appends integers to the variable name, not levels.
A workaround would look like this:
library(interplot)
set.seed(507)
df <- data.frame(
outcome = sample(1:7, 1000, replace = T),
scale = sample(1:7, 1000, replace = T),
dummy = sample(0:2, 1000, replace = T))
# factor the dummy
df$LABEL <- factor(df$dummy)
# df$LABEL <- df$dummyf
lm.out.1 <- lm(outcome ~ scale * LABEL, data = df)
interplot(lm.out.1, "LABEL", "scale", plot = T, hist = F, ci = 0.95)
Upvotes: 1