分组和过滤后,使条形文字保持水平条形

时间:2019-08-15 05:18:02

标签: r ggplot2 gganimate

我尝试将条形文字粘贴到每个水平条上,仅显示最上面的几项,在这里我使用filter和groupby,不确定这是否是一种好方法

DataFrame:

null

我希望这样,但是请允许我过滤每种状态下的前几项

具有预期动画的Gif,但未过滤前几项 Gif with expected animation but without filtering the top several items

代码:

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))

我当前的Gif: My approach

代码:

ggplot(df, aes(y = ordering, x = value)) +
  geom_barh(stat = "identity", aes(fill = group)) +
  geom_text(aes(x= value, y = ordering, label = paste(group, " ")), 
            vjust = 0.3, hjust = 1,color = 'white',size = 11) +
  transition_states(year, transition_length = 2, state_length = 0) +
  view_follow(fixed_y = TRUE)

0 个答案:

没有答案