1

Trying to create animation of a map in R using gganimation package and frame parameter.

e.g.

library(ggmap)
library(ggplot2)
library(gganimate)
library(readxl)
library(animation)
#adding data
channels_contracted=c(10,20,30,40,50,70,10,1)
year=c(1999,1999,2000,2000,2001,2002,2003,2003)
latitude=c(44.61217,46.97676,46.66602,46.51235,46.77762, 41.00222, 46.51235,46.77762)
longitude=c(30.72798,30.71394, 31.94281, 30.70631, 33.47262, 29.90559, 30.71394, 30.71775)
type=c("ASZ", "AGS", "ASZ", "AGS", "GNS", "GNS1", "GNS1", "AGS")
df = data.frame(channels_contracted, year, latitude, longitude, type) 

p <- ggmap(get_map(c(32.10399,49.04548), zoom = 5))

suppressWarnings(p <- p + geom_point(aes(longitude,latitude,frame=year,cumulative=FALSE, size = channels_contracted, color = type), df, alpha=0.3))

ani.options(interval=2)
gganimate(p)

It works fine and I get year number in title of an image. enter image description here (#note - you need imagemagick installed and configured.)

But i need to add specific data to each frame - e.g., for a frame where year is 2001 i need to add the sum of the contracts of the 2001 and another data in caption or in some other places.

How to do something like this? Can anybody help with an example of adding specific text on each frame?

twistfire
  • 425
  • 1
  • 5
  • 15
  • Please edit your question and make it a minimal reproducible example as asked in the answer to your last question (which btw seems to have solved your issue, so that you can mark it as solved). [Here's a guide](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#answer-5963610). Also, where do you need what info in which frame? Please specify. – lukeA Jan 18 '17 at 21:28
  • lukeA, thank's for your remark. done :) – twistfire Jan 21 '17 at 16:50
  • it seems i can add some text using geom_text(), but how to generate this text - i need to write some functions, e.g. get_text_func(year) to generate this text for each frame and just run this function in geom_text(get_text_func(year))? – twistfire Jan 26 '17 at 16:02

2 Answers2

4

There is one incredibly hacky (and flawed) way to plot a dynamic text that is not state or time: by abusing geom_text(). I removed all the fluff from the code (including the map) for illustration:

1

Code

ga <- ggplot(df, aes(longitude, latitude,
               size = channels_contracted, color = type, 
               label = paste("Revenue:", format(round(text/1000000, 2), nsmall = 2), "Mn"))) +
    geom_point() + 
    transition_time(year) +
    labs(title = paste("{round(frame_time)}")) +
    geom_text(aes(30, 47), size = 8, hjust = 0, color = "black")

animate(ga, nframes = 80)

Data

channels_contracted <- c(10, 20, 30, 40, 50, 70, 10, 1)
year <- c(1999, 1999, 2000, 2000, 2001, 2002, 2003, 2003)
latitude <- c(44.61217, 46.97676, 46.66602, 46.51235, 46.77762, 41.00222, 46.51235,46.77762)
longitude <- c(30.72798, 30.71394, 31.94281, 30.70631, 33.47262, 29.90559, 30.71394, 30.71775)
type <- c("ASZ", "AGS", "ASZ", "AGS", "GNS", "GNS1", "GNS1", "AGS")
text <- c(2021494, 2021494, 2298584, 2298584, 2324324, 2324324, 2553534, 2553534)

df <- data.frame(channels_contracted, year, latitude, longitude, type, text) 
Roman
  • 4,744
  • 2
  • 16
  • 58
4

Alternatively, you can arrange your data so that each moment of your time variable ("year" in your case) has a corresponding state. Using ggplot and curly braces, you can accomplish everything you need. A toy example is presented below.

Data

```{r}

data <- data.frame(cbind(x = rnorm(100)))
data <- cbind(data, y = data$x + rnorm(100))
data <- cbind(data, z = data$y + rnorm(100))
data <- cbind(data, a = data$z + rnorm(100))
data <- cbind(data, b = data$a + rnorm(100))
data <- cbind(data, c = data$b + rnorm(100))
data <- cbind(data, d = data$c + rnorm(100))
data <- cbind(data, e = data$d + rnorm(100))
data <- cbind(data, f = data$e + rnorm(100))
data <- cbind(data, g = data$f + rnorm(100))
data <- cbind(data, h = data$g + rnorm(100))
data <- cbind(data, i = data$h + rnorm(100))
data <- data %>% unlist

data2 <- data.frame(cbind(x = rnorm(100)))
data2 <- cbind(data2, y = data2$x + rnorm(100))
data2 <- cbind(data2, z = data2$y + rnorm(100))
data2 <- cbind(data2, a = data2$z + rnorm(100))
data2 <- cbind(data2, b = data2$a + rnorm(100))
data2 <- cbind(data2, c = data2$b + rnorm(100))
data2 <- cbind(data2, d = data2$c + rnorm(100))
data2 <- cbind(data2, e = data2$d + rnorm(100))
data2 <- cbind(data2, f = data2$e + rnorm(100))
data2 <- cbind(data2, g = data2$f + rnorm(100))
data2 <- cbind(data2, h = data2$g + rnorm(100))
data2 <- cbind(data2, i = data2$h + rnorm(100))
data2 <- data2 %>% unlist

nums <- c(rep(1,100),rep(2,100),
rep(3,100),rep(4,100),
rep(5,100),rep(6,100),
rep(7,100),rep(8,100),
rep(9,100),rep(10,100),
rep(11,100),rep(12,100))

lets <- c(rep("a",100),rep("b",100),
      rep("c",100),rep("c",100),
      rep("e",100),rep("f",100),
      rep("g",100),rep("h",100),
      rep("i",100),rep("j",100),
      rep("k",100),rep("l",100))

data_fin <-  data.frame(cbind(x = data, y= data2, moment = nums, letters = lets))

data_fin$x <- as.numeric(as.character(data_fin$x))
data_fin$y <- as.numeric(as.character(data_fin$y))
data_fin$moment <- as.numeric(as.character(data_fin$moment)) 
data_fin$letters <- as.character(data_fin$letters) 

Plot - here's a plot of points randomly dispersing over time. I'm modifying both the title and the x-axis according to the "nums" variable, which is the time variable in this example. The title is a function of the time variable. The x-axis shows the value of the variable "letters" that corresponds with the time variable. This would be the changing "state" in your example. Hope this helps...

ggplot(data_fin, aes(x = x, y = y)) +
   geom_point() + 
   labs(title = "Time = {minute(seconds_to_period(70 - round(frame_time)))}:{sprintf('%02d', second(seconds_to_period(70 - round(frame_time))))}",
   x = "X  = {data_fin$letters[data_fin$moment == round(frame_time)][1]}") +
   transition_time(nums)

enter image description here

big parma
  • 337
  • 1
  • 13