【问题标题】:Animated barplot via gganimate: Annotate box with changing text below plot通过 gganimate 的动画条形图:在图下方更改文本的注释框
【发布时间】:2019-07-10 12:07:55
【问题描述】:

我想用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_textgeom_label 来完成,或者使用gganimate 的标签变量之一更新annotate,或者回退到animate 并手动进行补间。无论如何,除非您直接使用网格添加它,否则将其保留在同一个地方会很痛苦。将其放入白色部分也并不容易,这取决于您采用的方法。
  • @alistaire 感谢您的提示。很遗憾听到似乎没有能够提供所需输出的解决方案。
  • 我的意思是,有解决方案;它们并不简单。
  • @alistaire 感谢您的澄清。不幸的是,我还需要以自动方式创建图表,因为我想创建许多这样的图表。因此,手动解决方案对我来说并不是一个足够的选择。
  • 这不是手动的(不涉及点击),它只是更多的代码。预先补间,绘制每个,添加一个 textGrob,然后将所有内容收集到一个 gif 中。这完全有可能——你甚至可以将它参数化并放入一个函数中——但这并不简单。

标签: r ggplot2 gganimate


【解决方案1】:

这是否满足您的需求?

df %>%

  # calculate the value range for each year state
  group_by(year) %>%
  mutate(max.value = max(value)) %>%
  ungroup() %>%

  # add text for each year state (I came up with some random lines for illustration)
  mutate(text = case_when(year == "2001" ~ "2001: launch of Wikipedia",
                          year == "2002" ~ "Brazil wins 2002 World Cup",
                          year == "2003" ~ "2003 saw SARS outbreak",
                          year == "2004" ~ "Olympics 2004 in Greece",
                          TRUE ~ "2005 - first YouTube upload")) %>%

  ggplot(aes(x = ordering, y = value)) +
  geom_col(aes(fill = group)) +

  # add blank layer with maximum value so that the plot's overall range
  # changes smoothly between states
  geom_blank(aes(y = max.value)) +

  # add text layer positioned in the middle, below visible range
  geom_text(aes(y = max.value / 2, label = text),
            x = 0, check_overlap = TRUE) +

  # turn off clipping to show text layer
  coord_flip(clip = "off") +

  theme(axis.title = element_blank(),
        axis.ticks = element_blank(),
        axis.text  = element_blank(),
        plot.margin = unit(c(1, 1, 8, 1), "cm")) +

  # (optional) increase state_length so that the text labels can be viewed longer
  transition_states(year, transition_length = 2, state_length = 2) +
  view_follow(fixed_x = TRUE)

(如果每个年份的文本字符串都相同,则生成的geom_text 图层将在动画期间保持完全静止。)

数据:

set.seed(123)
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))

【讨论】:

猜你喜欢
  • 2019-07-05
  • 2020-11-03
  • 2020-09-09
  • 2017-10-18
  • 1970-01-01
  • 2019-12-27
  • 1970-01-01
  • 2016-03-10
  • 1970-01-01
相关资源
最近更新 更多