【问题标题】:How to color code timevis plot in R shiny如何在 R 中为 timevis 绘图着色
【发布时间】:2019-02-22 11:43:39
【问题描述】:

我想根据 Date_Bucket 列对 timevis 输出图进行颜色编码,任何帮助将不胜感激。

head(file)
Record_ID	Start	End	Date_Bucket
1	01-01-2017	31-12-2021	Greater than 2 Years
2	01-11-2013	31-10-2028	Greater than 2 Years
3	01-11-2017	31-10-2022	Greater than 2 Years
4	22-04-2014	30-09-2020	1-2 Years
5	15-12-2017	30-06-2019	0-6 Months
6	01-11-2017	31-10-2022	Greater than 2 Years
7	22-04-2014	30-09-2020	1-2 Years
8	11-01-2013	31-08-2019	6-12 Months
9	11-10-2013	31-08-2019	6-12 Months
file$Start <- as.Date(file$Start)
file$End <- as.Date(file$End)

if (interactive()) 
library(shiny)
shinyApp(
ui = fluidPage(
  timevisOutput("timeline"),
  actionButton("btn", "Fit all items")
),
server = function(input, output) {
  output$timeline <- renderTimevis(
    timevis(data.frame(
     id = file$Record_ID, start =  file$Start , end = file$End, content = 
 file$Date_Bucket
    ))
  )
  observeEvent(input$btn, {
    fitWindow("timeline", list(animation = TRUE))
  })
 }
 )
 }

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    这是实现您想要的非常简单天真的方法。根据您的用例和不同值的数量(以及它是静态数字还是动态数字),您可能想做一些更聪明的事情,但这应该是一个好的开始。您可能还想添加更多的 CSS,以便每个框的轮廓不是蓝色的。基本上我在这里所做的只是使用 timevis 的 className 参数为每​​个 Date_Bucket 分配不同的类,并为每个类添加 CSS。

    library(shiny)
    library(timevis)
    
    file$Start <- as.Date(file$Start)
    file$End <- as.Date(file$End)
    cols <- c("redBg", "blueBg", "greenBg", "orangeBg")
    file$className <- cols[file$Date_Bucket]
    
    shinyApp(
      ui = fluidPage(
        tags$style(
          ".redBg { background: red; }
          .blueBg { background: blue; }
          .greenBg { background: green; }
          .orangeBg { background: orange; }"
        ),
        timevisOutput("timeline"),
        actionButton("btn", "Fit all items")
      ),
      server = function(input, output) {
        output$timeline <- renderTimevis(
          timevis(data.frame(
            id = file$Record_ID, start =  file$Start , end = file$End, content = 
              file$Date_Bucket, className = file$className
          ))
        )
        observeEvent(input$btn, {
          fitWindow("timeline", list(animation = TRUE))
        })
      }
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-29
      • 1970-01-01
      • 2016-02-08
      • 1970-01-01
      • 2020-04-13
      相关资源
      最近更新 更多