使用来自 ggplot2 的 geom_path 绘制双向重复测量设计的个体响应

Plotting individual responses for two-way repeated measures design with geom_path from ggplot2

我想使用 ggplotgeom_path 绘制在完全平衡的双向重复测量实验设计中测得的个体响应。

我能够为来自类似结构的混合模型设计的数据生成我想要的图

我的数据集具有类似的结构化数据。每个数据集都有一个响应变量 "Response" 和三个自变量。

在我的第一个数据集 (DF1) 中,我有 "Participant"、"Gender" 和 "Condition"。包括三名女性和四名男性,使 "Gender" 成为 'between-subjects' 因素,而 "Condition" 成为 'within-subjects' 因素,这是一个混合模型设计,如果我不是打错了。

DF1 <-
  data.frame(
    Response = c(3.2, 3.4, 3.5, 3.7, 3.2, 3.6, 3.5, 3.3, 3.3, 3.3, 3.3, 3.2, 3.4, 3.3),
    Participant = as.factor(c(24, 33, 40, 24, 33, 40, 27, 30, 35, 42, 27, 30, 35, 42)),
    Gender = c("female", "female", "female", "female", "female", "female", "male", "male", "male", "male", "male", "male", "male", "male"),
    Condition = c("trained", "trained", "trained", "untrained", "untrained", "untrained", "untrained", "untrained", "trained", "untrained", "trained", "trained", "untrained", "trained"))

在我的第二个数据集 (DF2) 中,我有 "Participant"、"Time" 和 "Condition"。相同的参与者参与了 "Time" * "Condition" 的所有组合,使其成为平衡的 2 向重复测量设计。

DF2 <-
  data.frame(
    Response = c(6.0, 6.4, 5.8, 6.3, 6.9, 6.2, 7.6, 7.2, 6.9, 7.0, 7.1, 7.1),
    Participant = as.factor(c(2, 3, 4, 2, 3, 4, 2, 3, 4, 2, 3, 4)),
    Time = as.factor(c(6, 6, 6, 6, 6, 6, 18, 18, 18, 18, 18, 18)),
    Condition = c("Nonexercise", "Nonexercise", "Nonexercise", "Exercise", "Exercise", "Exercise", "Nonexercise", "Nonexercise", "Nonexercise", "Exercise", "Exercise", "Exercise"))

对于我的第一个数据框 (DF1),我可以使用 ggplotgeom_path 以及 groupcolor 美学 (aes) 来获得一个有意义的情节。

library(ggplot2)

ggplot(DF1,
       aes(x = Condition,
           y = Response,
           group = Participant,
           color = Gender)) +
  geom_path()

如果我尝试对我的第二个数据帧 (DF2) 使用相同的设置,则会绘制数据,但以一种对我来说没有意义的方式绘制。

ggplot(DF2,
       aes(x = Time,
           y = Response,
           group = Participant,
           color = Condition)) +
  geom_path()

我可以使用 facet_grid 得到一个有意义的图,但这并不理想,因为我希望我的所有数据都在一个图上,类似于我在绘制 DF1 时看到的。

ggplot(DF2,
       aes(x = Time,
           y = Response,
           group = Participant,
           color = Participant)) +
  geom_path() + facet_grid(.~Condition)

Here is a .jpg of all of my plots side-by-side

具体我要:

  1. x 轴上的时间
  2. "Exercise" 一种颜色,"Nonexercise"
  3. 另一种颜色
  4. 每个参与者两行("Exercise" 和 "Nonexercise" 条件各一行)

提前感谢您的任何建议!

您想按 ConditionPaticipant 进行分组。一种方法是使用 interaction() 函数:

library(ggplot2)
DF2 <- data.frame(
        Response = c(6.0, 6.4, 5.8, 6.3, 6.9, 6.2, 7.6, 7.2, 6.9, 7.0, 7.1, 7.1),
        Participant = as.factor(c(2, 3, 4, 2, 3, 4, 2, 3, 4, 2, 3, 4)),
        Time = as.factor(c(6, 6, 6, 6, 6, 6, 18, 18, 18, 18, 18, 18)),
        Condition = c("Nonexercise", "Nonexercise", "Nonexercise", "Exercise", "Exercise", "Exercise", "Nonexercise", "Nonexercise", "Nonexercise", "Exercise", "Exercise", "Exercise"))
ggplot(DF2) +
    geom_path(aes(x = Time, y = Response, group = interaction(Condition, Participant), color = Condition)) +
    geom_point(aes(x = Time, y = Response, shape = Participant, color = Condition), size = 3)

我还添加了不同形状的点以提高清晰度。

这是您的要求。如果您对我的意见感兴趣,交换映射将进一步改善情节(前提是更改与其他情节一致):


ggplot(DF2) +
    geom_path(aes(x = Time, y = Response, group = interaction(Condition, Participant), color = Participant)) +
    geom_point(aes(x = Time, y = Response, shape = Condition, color = Participant), size = 3)