What does the 'group' aes do in the particular context?

I was reading the reference for gganimate, and the page was talking about transition_states. In the code below i dont understand why group is being used and why is it necessary. There was also a reasoning given for this:

Object permanence

transition_states uses the group aesthetic of each layer to identify which rows in the input data correspond to the same graphic element and will therefore define which elements will turn into each other between states. The group aesthetic, if not set, will be calculated from the interaction of all discrete aesthetics in the layer (excluding label), so it is often better to set it explicitly when animating, to make sure your data is interpreted in the right way. If the group aesthetic is not set, and no discrete aesthetics exists then all rows will have the same group. If the group aesthetic is not unique in each state, then rows will be matched first by group and then by index. Unmatched rows will appear/disappear, potentially using an enter or exit function.

Can someone explain what does this mean in the most fundamental way, I dont get it a bit. And also what the group aesthetic does in code below.

iris$group <- seq_len(nrow(iris))
anim1 <- ggplot(iris, aes(Sepal.Width, Petal.Width, group = group)) +
  geom_point() +
  labs(title = "{closest_state}") +
  transition_states(Species, transition_length = 3, state_length = 1) +
  enter_fade() +
  exit_fade()

group is all about discrete data that falls into chunks, as opposed to continuous data. A rule of thumb is that any variable that can take up to eight different distinct values is a candidate for discrete treatment. Above 12, probably not because it becomes hard to make so many distinctions visually. Working the examples helps to clarify.

The variable in the provided snippet uses a variable with 150 distinct values, which is clear off.

Here is what I just did

library(ggplot2)

# all species shown without distinction
ggplot(iris, aes(Sepal.Width, Petal.Width)) + geom_point()


# break out by species with color
ggplot(iris, aes(Sepal.Width, Petal.Width, color = Species)) + geom_point()

# or
ggplot(iris, aes(Sepal.Width, Petal.Width, color = Species)) + geom_point(aes(color = Species))


# change aesthetic to line
ggplot(iris, aes(Sepal.Width, Petal.Width)) + geom_line()


# distinguish by color
ggplot(iris, aes(Sepal.Width, Petal.Width, color = Species)) + geom_line()


# distinguish (partially) by position
ggplot(iris, aes(Sepal.Width, Petal.Width, group = Species)) + geom_line()

# or
ggplot(iris, aes(Sepal.Width, Petal.Width)) + geom_line(aes(group = Species))

Created on 2023-06-18 with reprex v2.0.2

This topic was automatically closed 21 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.