1

I'm using gganimate, and I need to add a table (data frame) near the "moving" plot. I don't care if the table is static or not.

I can do that when plotting ggplot plots by using grid.arrange command from the gridExtra package, but I'm afraid I have no idea how to do that when using gganimate.

georgeawg
  • 48,608
  • 13
  • 72
  • 95
  • 1
    Please share your data and the code you're working on so others can help. See more here [How to make a great R reproducible example?](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Tung Aug 12 '18 at 18:44
  • The author of the package has said that he has plans to implement this feature, but has not done so yet. See [here](https://github.com/thomasp85/gganimate/issues/86). – raheem Aug 13 '18 at 18:34
  • @Tung I'm afraid I cannot share the data, that's why I didn't share the code. Will do my best next time to share something. Thanks. – tomer_shoham Aug 16 '18 at 06:16

1 Answers1

2

Definitely possible with the geom_table from ggpmisc.

1

Code

g <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, color = continent)) + 
    geom_point() + 
    scale_x_log10() +
    annotate(geom = "table", x = Inf, y = -Inf,
             label = list(mytable), 
             vjust = 0, hjust = 1) +
    transition_time(year) +
    labs(title = "Year: {frame_time}")

animate(g)

Data

library(gapminder)
library(ggplot2)
library(gganimate)
library(ggpmisc)

# Transform to numeric to prevent an integer overflow 
gapminder$pop <- as.numeric(gapminder$pop)    

# Create table
mytable <- gapminder %>%
    filter(year == 2007) %>%
    group_by(continent = continent) %>%
    summarise(pop_mn_2007 = round(sum(pop)/1000000, 1),
              avg_lifeExp_2007 = round(mean(lifeExp), 2))
Roman
  • 4,744
  • 2
  • 16
  • 58