ggplot2: color aesthetic doesn't provide any grouping for geom_line()?

I'm trying to plot some longitudinal data across two phases of a treatment (baseline phase, treatment phase) with two experimental conditions (Condition one and condition two).

When I use color = condition, and group = phase, geom_line() doesn't group by condition (plot 1 below). If I use group = condition, the two conditions are appropriately grouped in the plot, however, I lose the break between the baseline and treatment phases which I would like to keep. How can I acheive a plot that looks like #2 below, but has no line between sessions 5 and 6? Thanks!

Reprex:

library(tidyverse)

tibble(
  session = rep(seq(1,10,1),2),
  phase = rep(c(rep("baseline", 5), rep("treatment", 5)), 2),
  condition = c(rep("one", 10), rep("two", 10)),
  score = c(1, 2, 2, 4, 3, 5, 7, 8, 10, 9, 2, 4, 3, 3, 4, 7, 8, 9, 9, 10)
) %>%
  ggplot(aes(x = session, y = score, group = phase, color = condition)) +
  geom_line() + 
  geom_point()

library(tidyverse)

tibble(
  session = rep(seq(1,10,1),2),
  phase = rep(c(rep("baseline", 5), rep("treatment", 5)), 2),
  condition = c(rep("one", 10), rep("two", 10)),
  score = c(1, 2, 2, 4, 3, 5, 7, 8, 10, 9, 2, 4, 3, 3, 4, 7, 8, 9, 9, 10)
) %>%
  ggplot(aes(x = session, y = score, group = condition, color = condition)) +
  geom_line() + 
  geom_point()

image

Update: I realized that setting fill = phase rather than group = phase achieves this result, though I'm not sure why.

So you do or don't want the vertical line? If you want the plot like #2 with a line at some value, you can just use geom_vline :

library(tidyverse)

tibble(
  session = rep(seq(1,10,1),2),
  phase = rep(c(rep("baseline", 5), rep("treatment", 5)), 2),
  condition = c(rep("one", 10), rep("two", 10)),
  score = c(1, 2, 2, 4, 3, 5, 7, 8, 10, 9, 2, 4, 3, 3, 4, 7, 8, 9, 9, 10)
) %>%
  ggplot(aes(x = session, y = score, group = condition, color = condition)) +
  geom_line() + 
  geom_point() +
  geom_vline(xintercept=5.5, linetype=2) #<---add a line

Sorry, I see how my question was not clear. Here is the desired result:

library(tidyverse)

tibble(
  session = rep(seq(1,10,1),2),
  phase = rep(c(rep("baseline", 5), rep("treatment", 5)), 2),
  condition = c(rep("one", 10), rep("two", 10)),
  score = c(1, 2, 2, 4, 3, 5, 7, 8, 10, 9, 2, 4, 3, 3, 4, 7, 8, 9, 9, 10)
) %>%
  ggplot(aes(x = session, y = score, fill = phase, color = condition)) +
  geom_line() + 
  geom_point() +
  geom_vline(aes(xintercept = 5.5), linetype = "dashed")

image

However, I still don't understand why setting fill = phase but not group = phase achieves this result.

According to your posts, I assume that the two groups you want to present in the plot is a combination of phase and condition. Then it would be:

library(tidyverse)

tibble(
  session = rep(seq(1,10,1),2),
  phase = rep(c(rep("baseline", 5), rep("treatment", 5)), 2),
  condition = c(rep("one", 10), rep("two", 10)),
  score = c(1, 2, 2, 4, 3, 5, 7, 8, 10, 9, 2, 4, 3, 3, 4, 7, 8, 9, 9, 10)
) %>%
  ggplot(aes(x = session, y = score, color = condition, 
                         group=interaction(phase, condition))) +
  geom_line() + 
  geom_point() +
  geom_vline(aes(xintercept = 5.5), linetype = "dashed")

1 Like

Wow- I have been using ggplot for 3 years and did not know about the Interaction() function.

I also did not realize that the group aesthetic defaults to all discrete variables in the plot, which I think explains why using group = phase doesn't work.

thank you!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.