如何在闪亮的应用程序中添加时间线栏以显示动画ggplot多年来的进度?

时间:2019-03-22 19:49:41

标签: r shiny gganimate

因此,我能够弄清楚如何使用ggplot和gganimate闪亮创建动画图。但是,到目前为止,我在图表底部添加时间线的努力一直没有结果。这是我的Shiny代码和数据:

library(ggplot2)
library(gganimate)
library(tidyverse)
library(shiny)

ui <- basicPage(
    imageOutput("myImage")

)

server <- function(input, output, session) {
  output$myImage <- renderImage({

    Data <- tribble(
  ~LinkIDs, ~Year2001Traffic, ~Year2002Traffic, ~Year2003Traffic,
  "A", 1, 10, 15,
  "B", 3, 1, 10,
  "C", 10, 5, 1)

    Data <- Data %>% gather(Year, Traffic, -LinkIDs)
    Data <- Data %>% mutate(Year= as.integer(parse_number(Year)))

    outfile <- tempfile(fileext='.gif')


    p1 <- ggplot(Data, aes(x = LinkIDs, y = Traffic)) +
      geom_bar(stat = "identity", color = 'blue', fill="white") +
      labs(title = 'Year: {frame_time}', x = 'Link ID', y = 'Volume') + 
      transition_time(Year)

    anim_save("outfile.gif", animate(p1, fps = 1, duration = 3))

    list(src = "outfile.gif",
         contentType = 'image/gif'
    )}, deleteFile = TRUE)
}

shinyApp(ui, server)

因此,我想拥有一个类似于此页面底部gapminder图表下方的时间线栏,以便能够播放/开始/停止动画:

https://towardsdatascience.com/animating-your-data-visualizations-like-a-boss-using-r-f94ae20843e3

有人可以帮我吗?任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

我第一次搜索时没有找到动画条形图示例。积分有效:

library(ggplot2)
library(plotly)
library(tidyverse)
library(shiny)

ui <- basicPage(
  plotlyOutput("myImage")

)

server <- function(input, output, session) {
  output$myImage <- renderPlotly({

    Data <- tribble(
      ~LinkIDs, ~Year2001Traffic, ~Year2002Traffic, ~Year2003Traffic,
      "A", 1, 10, 15,
      "B", 3, 1, 10,
      "C", 10, 5, 1)

    Data <- Data %>% gather(Year, Traffic, -LinkIDs)
    Data <- Data %>% mutate(Year= as.integer(parse_number(Year)))

    p1 <- ggplot(Data, aes(x = LinkIDs, y = Traffic)) +
      geom_point(aes(frame = Year),stat = "identity", color = 'blue', fill="white")# +
      #labs(title = 'Year: {frame_time}', x = 'Link ID', y = 'Volume')

    ggplotly(p1)

  })
}

shinyApp(ui, server)