简体   繁体   English

使用“交互”package 在 R 中创建多面图 2 向交互

[英]create faceted plots 2-way interaction in R using 'interactions' package

I am using the cat_plot function from the 'interactions' package in R (which is a wrapper for ggplot) to plot a 2-way interaction with 2 categorical variables.我正在使用 cat_plot function 来自 R 中的“交互”package(这是 ggplot 的包装器)到 plot 与 2 个分类变量的双向交互。 I can do this easily using the code below (reprex from the "diamonds" dataset)我可以使用下面的代码轻松地做到这一点(来自“钻石”数据集的代表)

require(interactions)

data("diamonds")

m <- glm(price ~ cut*color, data = diamonds)

cat_plot(m, pred = cut, modx = color, geom = "bar", colors = "Set1")

This produces the following graph这会产生下图

切色图

However, what I would like is to have a faceted graph with each of the cuts presented separately, to make it visually easier to interpret.然而,我想要的是有一个分面图,每个切割分别呈现,使其在视觉上更容易解释。 This can be done for 3-way interactions using the facet.modx = TRUE command, but when I try this with only a 2-way interaction with cat_plot(m, pred = cut, modx = color, geom = "bar", colors = "Set1", facet.modx = TRUE) I get the following error这可以使用facet.modx = TRUE命令针对 3 向交互来完成,但是当我尝试仅使用cat_plot(m, pred = cut, modx = color, geom = "bar", colors = "Set1", facet.modx = TRUE)的 2 向交互时cat_plot(m, pred = cut, modx = color, geom = "bar", colors = "Set1", facet.modx = TRUE)我收到以下错误

Error in prep_data(model = model, pred = pred, modx = modx, pred.values = pred.values,  : 
  formal argument "facet.modx" matched by multiple actual arguments

Is there a way to easily facet the graph for 2 way interactions?有没有一种方法可以轻松地为 2 种交互方式对图形进行分面? My real-life dataset is actually a glmer model so I would prefer to stay within the "interactions" package if possible.我的真实数据集实际上是 glmer model,所以如果可能的话,我更愿意留在“交互”package 中。

EDIT: based on the suggestion from @stefan I tried the following syntax cat_plot(m, pred = cut, modx = color, geom = "bar", colors = "Set1") + facet_wrap(~cut) which produced the graph below.编辑:根据@stefan 的建议,我尝试了以下语法cat_plot(m, pred = cut, modx = color, geom = "bar", colors = "Set1") + facet_wrap(~cut)生成了下图。 This is almost exactly what I want, except it has seemed to keep the other 'cuts' on the x-axis and just removed the bars.这几乎正是我想要的,除了它似乎保留了 x 轴上的其他“切口”并且只是删除了条形图。 Ideally, colours would be on the x-axis instead.理想情况下,颜色应该在 x 轴上。

在此处输入图像描述

EDIT 2:编辑 2:

I have recreated the problem using data which is more similar to what I am actually working with, with a binary outcome, random effects from glmer etc.我使用与我实际使用的数据更相似的数据重新创建了问题,具有二元结果、来自 glmer 的随机效应等。

require(lme4)
require(interactions)

set.seed(123)
id <- rep(1:150, each = 4)
condition <- rep(c("a", "b", "c"), each = 4, times = 50)
cat_mod <- rep(c("cat_1", "cat_2", "cat_3", "cat_4"), each = 1, length.out = 600)
control_mod <- rep(c("control_1", "control_2"), each = 4, length.out = 600)
binary_choice <- rbinom(600, 1, 0.5)


simdat <- data.frame(id, condition, cat_mod, binary_choice, control_mod)

m <- glmer(binary_choice ~ condition*cat_mod + control_mod + (1 | id), family=binomial, data = simdat)

cat_plot(m, pred = condition, modx = cat_mod, geom = "bar", colors = "Set1")

在此处输入图像描述

I would like to preserve the response scale on the y-axis, and the model accounting for the random intercept, which is why I was trying to avoid using ggplot directly, as the interactions package is already built to accommodate glmms, which is super convenient.我想保留 y 轴上的响应比例,model 负责随机截距,这就是为什么我试图避免直接使用 ggplot,因为已经构建了交互 package 以适应 glmms,这非常方便.

SOLVED解决了

Following the suggestion from @RStam I modified the code slightly so that all y-axes had the same scale, and removed the duplicate facet labels at the bottom.根据@RStam 的建议,我稍微修改了代码,使所有 y 轴具有相同的比例,并删除了底部重复的小平面标签。

cat_plot(m, pred = condition, modx = cat_mod, geom = "bar", colors = "Set1") + 
  scale_x_discrete(labels = c(a = " ", b = " ", c = " ")) + 
  facet_wrap(condition~., scales= "free_x")

This was the final result这是最后的结果

在此处输入图像描述

Original Answer原始答案

cat_plot(m, pred = cut, modx = color, geom = "bar", colors = "Set1") + 
facet_wrap(~cut, scales = "free_x")

Edit 1编辑 1

After that it still wasn't resolving your issue I've updated my answer.在那之后它仍然没有解决你的问题我已经更新了我的答案。 This should resolve the issue you are having.这应该可以解决您遇到的问题。

library(tidyverse)
ggplot(diamonds, aes(x=color,y=price, fill = color)) +
geom_col() + facet_wrap(~cut, scales = "free")

Edit 2编辑 2

Using your new data and the interactions package I found a rather unpleasant 'hack' using scale_x_discrete() but it should give the desired outcome.使用您的新数据和交互 package 我发现使用 scale_x_discrete() 的一个相当不愉快的“hack”,但它应该会产生预期的结果。

library(interactions)
library(lme4)

set.seed(123)
id <- rep(1:150, each = 4)
condition <- rep(c("a", "b", "c"), each = 4, times = 50)
cat_mod <- rep(c("cat_1", "cat_2", "cat_3", "cat_4"), each =  
1, length.out = 600)
control_mod <- rep(c("control_1", "control_2"), each = 4, 
length.out = 600)
binary_choice <- rbinom(600, 1, 0.5)


simdat <- data.frame(id, condition, cat_mod, binary_choice, 
control_mod)

m <- glmer(binary_choice ~ condition*cat_mod + control_mod + 
(1 | id), family=binomial, data = simdat)

cat_plot(m, pred = condition, modx = cat_mod, geom = "bar", 
colors = "Set1") + scale_x_discrete() + 
facet_wrap(condition~., scales= "free")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM