r - 如何在访问时间图中添加垂直线?

标签 r shiny plotly shinydashboard vistime

我想在我的访问时间图中添加一条代表当前日期的垂直线。我已经尝试了多种方法,但仍然无法弄清楚。 这是一个可重复的小代码,您可以在此基础上进行构建。

library(shiny)
library(plotly)
library(vistime)

pres <- data.frame(Position = rep(c("President", "Vice"), each = 3),
                   Name = c("Washington", rep(c("Adams", "Jefferson"), 2), "Burr"),
                   start = c("1789-03-29", "1797-02-03", "1801-02-03"),
                   end = c("1797-02-03", "1801-02-03", "1809-02-03"),
                   color = c('#cbb69d', '#603913', '#c69c6e'),
                   fontcolor = c("black", "white", "black"))

shinyApp(
  ui = plotlyOutput("myVistime"),
  server = function(input, output) {
    output$myVistime <- renderPlotly({
      vistime(pres, col.event = "Position", col.group = "Name")
    })
  }
)

最佳答案

您可以使用add_segments向使用vistime制作的plotly时间线添加一条垂直线。在本例中,x 轴将是年份。 yaxis 总统放置在 y = 1, 3, 5, 7 处,因此此示例中的线段应从 0 延伸到 8(y = 0yend = 8 )。

编辑:要自动从plotly对象获取y轴范围,您可以尝试使用plotly_build并以这种方式提取布局属性.

curr_year = 1801

p <- vistime(pres, col.event = "Position", col.group = "Name") %>%
  layout(showlegend = FALSE)
      
pb <- plotly_build(p)

add_segments(p, 
             x = curr_year, 
             xend = curr_year, 
             y = pb$x$layout$yaxis$range[1], 
             yend = pb$x$layout$yaxis$range[2], 
             color = I("light green"))

plotly

plot with vertical segment

关于r - 如何在访问时间图中添加垂直线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64236978/

相关文章:

r - 在 R-Studio 中使用 Lattice panel.identify

R 传单 - 如何仅等待 `map_shape_click`

python - "min() arg is an empty sequence"尝试将我的 Pandas plotly 转换为 plotly 时

r - 如何将整数转换为R中的分类数据?

r - 在ggmap中的多个地址上使用路由功能

r - Plotly shift event_data 返回中未显示的国家( Shiny )

r - Flexdashboard/plotly 交互导致奇怪的滚动条行为

python - Plotly 中文本标签的背景颜色?

python - 在 r 中使用 igraph 导出图形

r - 如何在 R Shiny 和 plotly 中处理大量数据?