代码之家  ›  专栏  ›  技术社区  ›  Joachim Schork

通过gganimate的动画条形图:在绘图下方更改文本的批注框

  •  0
  • Joachim Schork  · 技术社区  · 6 年前

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

    enter image description here

    问题:如何在该绘图下面的白色区域中使用更改的文本对文本框进行批注?

    0 回复  |  直到 6 年前
        1
  •  1
  •   Z.Lin    6 年前

    这能满足你的需要吗?

    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 动画期间,层将保持完全静止。)

    result

    数据:

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