简体   繁体   English

根据R中的整洁数据同时创建多个交互图

[英]Creating several interaction plots simultaneously from tidy data in R

I have a tidy dataframe that looks like the following: 我的数据框看起来很整洁,如下所示:

id  samediff  gainloss  factor  value
1   S         G         happy   5
1   S         G         sad     3
1   S         G         angry   4
2   D         G         happy   2
2   D         G         sad     3
2   D         G         angry   5
3   D         L         happy   1
3   D         L         sad     4
3   D         L         angry   3

Here's the reproducible data: 这是可复制的数据:

df<- data.frame(id = c(1, 1, 1, 2, 2, 2, 3, 3, 3), 
samediff = c("S", "S", "S", "D", "D", "D", "D", "D", "D"), 
gainloss = c("G", "G", "G", "G", "G", "G", "L", "L", "L"), 
factor = c("happy", "sad", "angry", "happy", "sad", "angry", "happy", "sad", "angry"), 
value = c(5, 3, 4, 2, 3, 5, 1, 4, 3))

I would like to create a series of interaction plots. 我想创建一系列交互图。 So far, I have created interaction plots by spreading the data in the following way: 到目前为止,我已经通过以下方式传播数据来创建了交互图:

id  samediff  gainloss  happy  sad  angry 
1   S         G         5      3    4
2   D         G         2      3    5
3   D         L         1      4    3

I then use the following function: 然后,我使用以下功能:

interaction.plot(df$samediff, df$gainloss, df$happy) 

Is there a way to create separate interaction plots for each factor simultaneously? 有没有一种方法可以同时为每个因子创建单独的交互图? In my actual dataset, I have many more factors than the 3 listed here (happy, sad, angry), so it would be useful for me to know if there is a way to generate these efficiently. 在我的实际数据集中,与这里列出的3个因素相比,我有更多的因素(快乐,悲伤,愤怒),因此对于我是否有办法有效地生成这些因素很有用。

Using the example here, I'd also need plots where the last term in the interaction.plot function is df$sad and df$angry. 使用此处的示例,我还需要在interaction.plot函数中最后一项为df $ sad和df $ angry的图。 The first two terms in the interaction.plot function can stay the same. interact.plot函数中的前两个术语可以保持不变。

Not very elegant but hopefully it's clear what's going on and how it can be tweaked if there are other changes to your request. 不太优雅,但希望可以弄清楚发生了什么,如果您的请求有其他更改,可以如何进行调整。

df <- data.frame(id = c(1, 1, 1, 2, 2, 2, 3, 3, 3), 
                 samediff = c("S", "S", "S", "D", "D", "D", "D", "D", "D"), 
                 gainloss = c("G", "G", "G", "G", "G", "G", "L", "L", "L"), 
                 factor = c("happy", "sad", "angry", "happy", "sad", "angry", "happy", "sad", "angry"), 
                 value = c(5, 3, 4, 2, 3, 5, 1, 4, 3))
df_2 <- tidyr::spread(df, factor, value)

# Unique values of factor to iterate over and obtain interaction plots for
factor_values <- unique(df$factor)
size <- ceiling(sqrt(length(factor_values)))

par(mfrow = c(size, size))
for(i_factor_value in factor_values) {
  interaction.plot(df_2$samediff, df_2$gainloss, df_2[[i_factor_value]], ylab = i_factor_value)
}
par(mfrow = c(1, 1))

在此处输入图片说明

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

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