【问题标题】:How to add specific text for each frame of an animation in R如何在R中为动画的每一帧添加特定文本
【发布时间】:2017-06-03 08:55:40
【问题描述】:

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

例如

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)

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

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

如何做这样的事情?任何人都可以提供在每个框架上添加特定文本的示例吗?

【问题讨论】:

  • 请编辑您的问题并使其成为您上一个问题的答案中所问的最小可重复示例(顺便说一句,这似乎已经解决了您的问题,以便您可以将其标记为已解决)。 Here's a guide。另外,您在哪个框架中需要什么信息?请说明。
  • lukeA,谢谢你的评论。完成:)
  • 看来我可以使用 geom_text() 添加一些文本,但是如何生成这些文本 - 我需要编写一些函数,例如get_text_func(year) 为每一帧生成这个文本并在 geom_text(get_text_func(year)) 中运行这个函数?

标签: r animation ggmap gganimate


【解决方案1】:

有一种令人难以置信的骇人听闻(且有缺陷)的方法来绘制不是statetime 的动态文本:滥用geom_text()。为了说明,我从代码(包括地图)中删除了所有绒毛:

代码

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) 

【讨论】:

    【解决方案2】:

    或者,您可以安排数据,以便时间变量(在您的情况下为“年”)的每个时刻都有相应的状态。使用 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)
    

    【讨论】:

      猜你喜欢
      • 2013-12-02
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 2022-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多