1

The following code shows an interaction plot using the effects library:

model <- lm(mpg ~ hp + wt + hp:wt, data=mtcars)
library(effects)
plot(effect("hp:wt", model, list(wt=c(2.2,3.2,4.2))), multiline=TRUE)

enter image description here

I tried making the same model but holding wt at 2.2, but this model could not calculate the coefficient for wt:

mtcars$wt_2.2 <- 2.2
model2.2 <- lm(mpg ~ hp + wt_2.2 + hp:wt, data=mtcars)
coef(model2.2)

How can I make the same plot using ggplot2?

3
  • github.com/ggobi/cranvas/wiki Commented Nov 14, 2013 at 8:27
  • What have you tried? As it stands, the question is in danger of being closed and tossed into the river. Commented Nov 14, 2013 at 8:29
  • I've added what I've tried Commented Nov 14, 2013 at 9:04

1 Answer 1

5
tmp <- as.data.frame(effect("hp:wt", model, list(wt=c(2.2,3.2,4.2))))
ggplot(data=tmp, aes(x=hp, y=fit, colour=as.factor(wt))) +
       geom_line() +
       labs(colour="wt")

plot

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.