Jayden.Cameron Jayden.Cameron -4 years ago 123
R Question

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

I want to use

ggplot
and
geom_path
to plot individual responses measured in a completely balanced two-way repeated-measures experimental design.


I am able to generate the plot I want for data coming from a similarly structured mixed-model design.

My datasets have similarly structured data. Each dataset has a response variable, "Response", and three independent variables.

In my first dataset (DF1), I have "Participant", "Gender", and "Condition". Three women and four men were included, making "Gender" a 'between-subjects' factor, and "Condition" a 'within-subjects' factor, and this is a mixed-model design, if I'm not mistaken.

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


In my second dataset (DF2), I have "Participant", "Time", and "Condition". The same participants were involved in all combinations of "Time" * "Condition", making this a balanced 2-way repeated-measures design.

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


For my first dataframe (DF1), I can use
ggplot
and
geom_path
with the
group
and
color
aesthetics (
aes
) to get a plot that makes sense.

library(ggplot2)

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


If I try to use the same setup for my second dataframe (DF2), the data are plotted, but in a way which does not make sense to me.

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


I can use
facet_grid
to get a plot that makes sense, but this is not ideal because I want all of my data on a single plot, similar to what I can see when plotting 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

Specifically I want:


  1. Time on the x-axis

  2. One color for "Exercise" and a different color for "Nonexercise"

  3. Two lines for each participant (one each for the "Exercise" and "Nonexercise" conditions)



Thanks in advance for any advice!

Answer Source

You want to group by both Condition and Paticipant. A way to do that is using the interaction() function:

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)

I also added point with different shapes to improve clarity.

This is what you requested. If you are interested in my opinion, swapping the mapping would improve the plot even more (provided that the changes are consistent in others plots):


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)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download