2017-07-19 77 views
0

所以我需要在R中使用interplot来描绘​​一个因子变量的相互作用。除了其中一个重要部分外,我已经能够将所有东西都映射出来:如何更改获取因子的标签绘制。这里的显示问题可复制例如: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$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) 

一旦我绘制这里的相互作用是我得到:

enter image description here 现在,我需要能够改变dummyf1dummyf2标签的面读基本上是LABEL1LABEL2。这里有一个可能的解决办法我试过但没有得到我我需要什么:

# 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) 

我还试图修改的ggplot2的方面,因为interplot使用ggplot2,但一直没能得到它的工作的。有什么建议么?提前致谢!

回答

0

您将不得不修改underlying code。它只是将整数添加到变量名称中,而不是级别。

一种解决方法是这样的:

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)