Matias Andina
Matias Andina

Reputation: 4230

gganimate independently geom_text and geom_point

I have a dataset of countries on a PC space. I would like to plot all points on this space (see image below).

ggplot(ind_countries,
       aes(PC1, PC2)) +
  geom_point()

enter image description here

I would like to show the name of the country only during the animation, and then have the name disappear while the dot is on place. I have tried gganimate but couldn't exactly get the visual I'm looking for.

ggplot(ind_countries %>% slice(1:10),
       aes(PC1, PC2)) +
  shadow_mark() +
  geom_point()+
  geom_text(aes(label=Area)) +
  transition_states(Area)

enter image description here

But I don't want to interpolate between country names, country names should be fixed. More importantly, this visual will be hugely cluttered when using all ~200 labels. Same as if I have done ggplot(...) + geom_text().

Alternatively, I would like to have all points as in the static picture and animate through the country names to highlight them (using color and name or equivalent).

Data

structure(list(Area = c("Afghanistan", "Albania", "Algeria", 
"Angola", "Antigua and Barbuda", "Argentina", "Armenia", "Australia", 
"Austria", "Azerbaijan"), PC1 = c(`1` = -533.00304848187, `2` = -733.478134192073, 
`3` = -862.469905878461, `4` = 210.741387772907, `5` = -75.9683314876284, 
`6` = -432.40416113792, `7` = -739.448970021204, `8` = -153.020907842885, 
`9` = -266.078280799454, `10` = -856.759952967809), PC2 = c(`1` = 242.086883796857, 
`2` = 204.143228785502, `3` = 179.014761012001, `4` = -453.158827266088, 
`5` = -76.6617307886724, `6` = 31.1628432885813, `7` = 206.38294390879, 
`8` = -58.8035296785091, `9` = -100.898331142746, `10` = 240.75679171712
), PC3 = c(`1` = 65.4343509859609, `2` = 83.3998919734866, `3` = 216.37491156154, 
`4` = -104.167202758037, `5` = -203.978295596104, `6` = -12.4117893989394, 
`7` = 37.4440357817306, `8` = -174.038394855287, `9` = -64.9420372105368, 
`10` = 133.284279359949)), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame"))

Upvotes: 2

Views: 796

Answers (1)

Z.Lin
Z.Lin

Reputation: 29095

Here's a possible approach for your alternative request:

animation

ind_countries %>%
  ggplot(aes(PC1, PC2)) +
  
  # static layer with all the points constantly visible
  geom_point(data = . %>% select(-Area))+ 
  
  # animated layer, each red point appears in turn (can add other
  # changes for emphasis as well)
  geom_point(aes(group = Area), 
             colour = "red") + 
  
  # animated layer, each label appears in turn slightly above point (given the
  # number of points in the full dataset, geom_label will be easier to read
  # than geom_text
  geom_label(aes(group = Area, label=Area, y = PC2 + 25)) + 
  
  transition_states(Area)

The first request (different exit vs. persist behaviour for different animated layers) can probably be done somehow, but I don't think it'd be as straightforward.

Upvotes: 1

Related Questions