Visualising repeated measures data, grouped by two factors
Asked Answered
S

1

6

I have data from a study using a 2x2x3 repeated measures design. Participants provided a numeric response ('tcons') at two time-points (test: 'pre', 'post'). They were further grouped with respect to two between-persons factors, one with two levels (pretest_leaning: 'pro', 'anti'), the other with three levels (condition: 'both', 'counter', 'favour')

I would like to graph mean responses at both time points, grouped by combinations of each factors. That is, have means at both timepoints for each of the 6 different combinations of the other factors.

Here is the data, with means already calculated:

structure(list(condition = c("both", "both", "both", "both", 
"counter", "counter", "counter", "counter", "favour", "favour", 
"favour", "favour"), pretest_leaning = c("anti", "anti", "pro", 
"pro", "anti", "anti", "pro", "pro", "anti", "anti", "pro", "pro"
), test = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L), levels = c("pre", "post"), class = "factor"), tcons_mean = c(-0.407142857142857, 
-0.289285714285714, 0.357142857142857, 0.398809523809524, -0.357142857142857, 
-0.0676691729323308, 0.272727272727273, 0.155844155844156, -0.43984962406015, 
-0.488721804511278, 0.357142857142857, 0.279220779220779)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -12L), groups = structure(list(
    condition = c("both", "both", "counter", "counter", "favour", 
    "favour"), pretest_leaning = c("anti", "pro", "anti", "pro", 
    "anti", "pro"), .rows = structure(list(1:2, 3:4, 5:6, 7:8, 
        9:10, 11:12), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -6L), .drop = TRUE))

I have tried the following. I receive no errors, but the graph is empty.

ggplot(tcons_long, aes(x = test, y = tcons_mean, colour = pretest_leaning, linetype = condition)) + geom_path()
Speller answered 19/9 at 5:30 Comment(1)
On StackOverflow, it is customary to upvote helpful answers and to accept an answer; kindly do so with this and your following posts! See how-to: meta.stackexchange.com/a/5235/371738Ransack
P
5

As the message

geom_path(): Each group consists of only one observation. ℹ Do you need to adjust the group aesthetic?

is telling you, you have to explicitly set the group aesthetics. This is required as your x variable is a discrete variable.

library(ggplot2)

ggplot(tcons_long, aes(
  x = test, y = tcons_mean,
  colour = pretest_leaning, linetype = condition,
  group = interaction(pretest_leaning, condition)
)) +
  geom_path()

Powerful answered 19/9 at 5:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.