9

我想将我的数据的另一列值插入到gganimate动画标题中。

例如,这里的状态级别变量是x,我想添加到标题变量y

df <- tibble(x = 1:10, y = c('a', 'a', 'b', 'd', 'c', letters[1:5]))
df

A tibble: 10 x 2
       x y    
   <int> <chr>
 1     1 a    
 2     2 a    
 3     3 b    
 4     4 d    
 5     5 c    
 6     6 a    
 7     7 b    
 8     8 c    
 9     9 d    
10    10 e 

这按预期工作:

ggplot(df, aes(x, x)) +
  geom_point() +
  labs(title = '{closest_state}') +
  transition_states(x,
                    transition_length = 0.1,
                    state_length = 0.1)

在此处输入图像描述

这失败了:

ggplot(df, aes(x, x)) +
  geom_point() +
  labs(title = '{closest_state}, another_var: {y}') +
  transition_states(x,
                    transition_length = 0.1,
                    state_length = 0.1)

eval 中的错误(解析(文本 = 文本,keep.source = FALSE),envir):
找不到对象“y”

也试过这个,但y不会改变:

ggplot(df, aes(x, x)) +
  geom_point() +
  labs(title = str_c('{closest_state}, another_var: ', df$y)) +
  transition_states(x,
                    transition_length = 0.1,
                    state_length = 0.1)

在此处输入图像描述

另一种选择是映射y为状态级别变量并使用该frame变量而不是x,但在我的应用程序y中,要么是像上面那样的非必要唯一的字符变量,要么是数字变量,但又不是必要的唯一且不是-必然有序。在这种情况下gganimate(或ggplot?)将按照它认为合适的方式对其进行排序,从而使最终结果变得奇怪,而不是按以下顺序排序x

ggplot(df, aes(x, x)) +
  geom_point() +
  labs(title = '{frame}, another_var: {closest_state}') +
  transition_states(y,
                    transition_length = 0.1,
                    state_length = 0.1)

在此处输入图像描述

那么如何简单地添加无序而非数字y变量的变化值呢?

最后:这个问题在这里被问到但没有一个可重复的例子,所以没有回答,希望这个更好。

4

3 回答 3

4

One dirty solution would be to paste together the variables and make a new one to use in the transition_states:

df <- mutate(df, title_var = factor(paste(x, y, sep="-"), levels = paste(x, y, sep="-")))
# # A tibble: 6 x 3
# x y     title_var
# <int> <chr> <fct>    
# 1     1 a     1-a      
# 2     2 a     2-a      
# 3     3 b     3-b      
# 4     4 d     4-d      
# 5     5 c     5-c      
# 6     6 a     6-a  

Then we could use gsub() in ordet to strip closest_state from the unwanted part, like this:

gsub(pattern = "\\d+-", replacement = "", "1-a") 
"a"

So:

ggplot(df, aes(x, x)) +
  geom_point() +
  labs(title = '{gsub(pattern = "\\d+-", replacement = "", closest_state)}') +
  transition_states(title_var, transition_length = 0.1, state_length = 0.1)

enter image description here

于 2018-12-20T09:49:47.337 回答
3

gganimate在我打开的问题之后,来自他自己的作者的另一种可能性更紧凑:

https://github.com/thomasp85/gganimate/issues/252#issuecomment-450846868

根据托马斯的说法:

无法访问输入数据中的随机列有多种原因,因此不太可能比这更好......

于 2019-01-02T13:20:03.393 回答
2

这是一个使用 的解决方案dplyr,基于gganimateGiora 提供的开发人员 Thomas 的解决方案。

library(tidyverse)
library(gganimate)

df <- tibble::tibble(x = 1:10, y = c('a', 'a', 'b', 'd', 'c', letters[1:5]))

a <- ggplot(df, aes(x, x)) +
  geom_point() +
  labs(title = "{closest_state}, another_var: {df %>% filter(x == closest_state) %>% pull(y)}") +
  transition_states(x,
                    transition_length = 0.1,
                    state_length = 0.1)
animate(a)

gganimate标题使用动画标题元素的glue语法,您可以dplyr在其中包含整个数据操作管道。

您可以参考调用中closest_state提供的变量。在这里,由于动画的帧由 的连续级别索引,因此我使用基于 的值对给定帧进行子集化,然后引用 column 的相应行,其中包含我想在标题中显示的附加信息。使用,您可以获取对应的单个值并将其显示在动画的标题中。gganimate::transition_states()dplyrxfilter()dfxypullyx

这是一种简洁直接的方法,其优点是您可以通过在管道中添加summarize()和其他调用来计算汇总值以即时显示。magrittr

于 2021-02-05T21:07:40.653 回答