Search code examples
ggplot2tidyverse

Using ggplot2 group aesthetic not working


I'm trying to use the group aesthetic to separate my data in a line plot and I can't figure out why it is not working.

library(tidyverse)

df = data.frame(ID = rep(c("A", "B"), each = 6),
                TIME = rep(1:3, times = 4),
                REP = rep(c(1,2,1,2), each = 3),
                MEASURE = c(10,5,2,20,10,4,15,3,1,8,4,1))

ggplot(df, aes(x = TIME, y = MEASURE, color = ID)) +
  geom_line(aes(group = factor(REP)))

I thought this would give a plot with 4 lines (2 replicates of each ID) but I instead get a very strange plot with vertical lines for A and two lines for B. strange plot

If I instead use linetype to provide the groupings I get the correct plot.

ggplot(df, aes(x = TIME, y = MEASURE, color = ID)) +
  geom_line(aes(linetype = factor(REP)))

what I was expecting (except for the line differences)

How can I use the group aesthetic to separate the data the way I intended?


Solution

  • This happens because the group argument is different to other arguments for the aes() function, such as color or fill, in that it "is by default set to the interaction of all discrete variables in the plot" (run ?group to read full explanation). Therefore, by manually setting the group aesthetic, you are overriding the proper functioning of the color aesthetic, i.e. undoing the interaction between REP and ID. That's why setting the interaction of both variables fixed your plot, which is the same that would happen internally in the group argument if you use linetype (or any other aesthetic) instead of group.

    If you use linetype as a group in your geom_line layer, it will interact that variable with your other variable, which is mapped in color, and just by looking at your dataframe you can see that the interaction between both variables will yield four lines, separated both by color and linetype. Also, notice that both linetype and color arguments will separate your lines with a distinct visual characteristic (a different line type, a different color), while group is not mapped to any aesthetic quality, so it just splits the lines into multiple lines without further graphical differences.