如何在R中为动画的每个帧添加特定文本

时间:2017-01-18 20:10:20

标签: r animation ggmap gganimate

尝试使用gganimation包和frame参数在R中创建地图的动画。

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)

它工作正常,我在图像的标题中获得年份编号。 enter image description here (#note - 您需要安装和配置imagemagick。)

但我需要为每个帧添加特定数据 - 例如,对于年份为2001的帧,我需要添加2001年合同的总和以及标题或其他地方的其他数据。

如何做这样的事情?任何人都可以帮助在每个帧上添加特定文本的示例吗?

2 个答案:

答案 0 :(得分:0)

通过滥用state来绘制非timegeom_text()的动态文本,是一种难以置信的骇人(且有缺陷)的方式。我从代码(包括地图)中删除了所有绒毛,以进行说明:

1

代码

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)

数据

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) 

答案 1 :(得分:0)

或者,您可以排列数据,以便时间变量的每个时刻(在您的情况下为“年”)具有对应的状态。使用ggplot和花括号,您可以完成所需的一切。下面是一个玩具示例。

数据

```{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) 

图-这是随时间随机分散的点的图。我正在根据“ nums”变量(在此示例中为时间变量)修改标题和x轴。标题是时间变量的函数。 x轴显示与时间变量相对应的变量“字母”的值。在您的示例中,这将是不断变化的“状态”。希望这对您有帮助...

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)

Why is COMPOSER_HOME empty?