我希望使用
考虑以下示例:
gganimate
包创建一个动画条形图。在条形图下面,我想要注释一个文本框。盒子内的文本应随时间变化而改变。条形图的x轴应该移动(由view_follow
指定)。然而,文本框应在绘图的固定点显示。考虑以下示例:
# Create example data
df <- data.frame(ordering = c(rep(1:3, 2), 3:1, rep(1:3, 2)),
year = factor(sort(rep(2001:2005, 3))),
value = round(runif(15, 0, 100)),
group = rep(letters[1:3], 5))
library("gganimate")
library("ggplot2")
# Create animated ggplot with coord_flip
ggp <- ggplot(df, aes(x = ordering, y = value)) +
geom_bar(stat = "identity", aes(fill = group)) +
transition_states(year, transition_length = 2, state_length = 0) +
view_follow(fixed_x = TRUE) +
coord_flip() +
theme(axis.title.x = element_blank(),
axis.ticks.x = element_blank(),
axis.text.x = element_blank(),
axis.title.y = element_blank(),
axis.ticks.y = element_blank(),
axis.text.y = element_blank(),
plot.margin = unit(c(1, 1, 8, 1), "cm"))
问题:如何在此图下方的白色区域中用变化的文本注释文本框?
annotate
传递一个美学元素,因此你需要通过geom_text
或者geom_label
来实现,或者使用gganimate的标签变量来更新annotate
,或者回退到animate
并手动完成缓动。不管怎样,保持它在同一位置都会很麻烦,除非你直接使用grid添加它。根据你采取的方法,在白色部分放置它也不容易。 - alistaire