我想将我的数据的另一列值插入到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
变量的变化值呢?
最后:这个问题在这里被问到但没有一个可重复的例子,所以没有回答,希望这个更好。