GGanimate: geom_text with numeric values animates at decimal numbers instead of integers

一笑奈何 提交于 2019-12-04 16:42:30

You can try to calculate the transitions by your own beforehand...

library(gganimate)
library(tidyverse)
Data2 <- Data %>% 
  group_by(Group) %>% 
    arrange(Group) %>% 
    mutate(diff = c((Value - lag(Value))[-1],0))

Seq <- seq(1,3,0.01)
library(gganimate)
tibble(Time_new=rep(Seq,3), Group = rep(LETTERS[1:3], each = length(Seq))) %>% 
  mutate(Time=as.numeric(str_sub(as.character(Time_new),1,1))) %>% 
  left_join(Data2)  %>%
  group_by(Group, Time) %>% 
  mutate(diff = cumsum(diff/n())) %>% 
  mutate(Value2 = Value + diff) %>%
  mutate(new_old = ifelse(Time == Time_new, 2, 1)) %>% 
  ggplot(aes(Group, Value2)) +
  geom_col(position = "identity") +
  geom_text(aes(label = sprintf("%1.2f",Value2)), vjust = -1) +
  coord_cartesian(ylim = c(0, 40)) +
  transition_manual(Time_new) 

or try geom_text(aes(label = round(Value2,2)), vjust = -1)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!