【问题标题】:How to display all durations of events per the events themselves with plotly in r如何在 r 中显示每个事件本身的所有事件持续时间
【发布时间】:2019-08-27 13:54:29
【问题描述】:

我想在rshiny 中创建一个小部件,使用plotly 显示事件的持续时间。
我有一个 df 有 4 个事件及其 starting 和 ending 时间。

event = c("event_1","event_2","event_3","event_4","event_4","event_2","event_3","event_4","event_4","event_2","event_3","event_4","event_1","event_2","event_3","event_4")
start <- as.POSIXct(c("2019-05-02 07:08:49", "2019-05-02 07:09:21",
                     "2019-05-02 07:09:41", "2019-05-02 07:10:05",
                     "2019-05-02 07:24:52", "2019-05-02 07:28:50",
                     "2019-05-02 07:29:23", "2019-05-02 07:30:16",
                     "2019-05-02 07:33:13", "2019-05-02 07:33:43",
                     "2019-05-02 07:35:31", "2019-05-02 07:36:29",
                     "2019-05-02 07:38:14", "2019-05-02 07:43:26",
                     "2019-05-02 07:44:59", "2019-05-02 07:53:45"))
stop <- as.POSIXct(c("2019-05-02 07:09:29", "2019-05-02 07:10:02",
                   "2019-05-02 07:10:17", "2019-05-02 07:10:40",
                   "2019-05-02 07:29:10", "2019-05-02 07:29:32",
                   "2019-05-02 07:30:35", "2019-05-02 07:30:53",
                   "2019-05-02 07:33:48", "2019-05-02 07:34:18",
                   "2019-05-02 07:36:06", "2019-05-02 07:38:34",
                   "2019-05-02 07:38:49", "2019-05-02 07:45:19",
                   "2019-05-02 07:45:35", "2019-05-02 07:54:20"))

df <- data.frame(event = event, start = start, stop = stop)
df

我从这里开始学习甘特图教程:https://plot.ly/r/gantt/

cols <- RColorBrewer::brewer.pal(length(unique(df$event)), name = "Set3")
df$color <- factor(df$event, labels = cols)

library(plotly)
p <- plot_ly()

for(i in 1:(nrow(df))){
  p <- add_trace(p,
                 x = c(df$start[i], df$stop[i]),  # x0, x1
                 y = c(i, i),  # y0, y1
                 mode = "lines",
                 line = list(color = df$color[i], width = 20),
                 hoverinfo = "text",
                 evaluate = T  # needed to avoid lazy loading
  )
}

p %>% layout(legend = list(orientation = "h",   
                           xanchor = "center", 
                           x = 0.5))

这给了我这个情节1:
但这不是我所追求的。

我所追求的看起来像这样 plot2:

我可以使用ggplot2 和ggplotly() 来实现这一点,但我无法让x 轴在放大后显示几分钟。 x轴远非详细。

library(ggplot2)
gp <- ggplot(df, aes(colour = event)) + 
  geom_segment(aes(x = start, xend = stop , y = event, yend = event), size = 6) +
  theme_bw() 

ggplotly(gp)

我有两个问题,我可能应该将它们分成两个单独的帖子,尽管它们本身就是解决方法。

plotly 方式: 如何让 x 轴保持 4 个事件和 y 轴及其相应的持续时间? (如情节2)

ggplotly() 方式: 如何在放大后显示更详细的 y 轴? (就像在 plot1 中一样 - 虽然从 .png 中看不到它 - 分钟在那里)

提前谢谢你!

更新
scale_x_datetime(), date_format() 给出了详细的 xaxis。但现在它需要自动隐藏选项。

library(ggplot2)
gp <- ggplot(df, aes(colour = event)) + 
  geom_segment(aes(x = start, xend = stop , y = event, yend = event), size = 6) +
  theme_bw() +
  scale_x_datetime(breaks = ("1 min"), labels = scales::date_format("%H:%M"))

ggplotly(gp)

【问题讨论】:

    标签: r ggplot2 plotly r-plotly ggplotly


    【解决方案1】:

    我只能找出plotly 的方式。

    我认为ggplotly()函数修复了plotly的一些配置。我尝试查看布局配置以使其在缩放时更改格式,但我找不到它。

    所以我将留下我对plotly 案例的答案。

    阴谋方式

    你快到了。首先,请注意每次调用add_trace 时,它都会在您的图例中创建一个新的跟踪。由于您循环遍历行,因此您为每一行创建一个新的跟踪。

    另外,在add_trace 中,你设置了y = c(i, i)。这就解释了为什么事件没有对齐。

    我就是这样做的。

    首先,我使用了add_segments。这样我可以明确地给出x, y, xend, yend(我也可以给出每个事件的所有间隔的向量)。其次,我没有遍历行,而是遍历了事件。然后,我过滤每个事件的行,并为每个事件调用一次add_segments。我还设置了y = df2$event 和yend=df2$event(它们是具有水平[1、2、3、4]的因子)。

    p <- plot_ly()
    
    for(e in unique(df$event)){
      df2 <- df %>% filter(event == e)
    
      p <- add_segments(p,
                        x = df2$start,
                        y = df2$event,
                        xend=df2$stop,
                        yend=df2$event,
                        line = list(color = df2$color, width = 20),
                        hoverinfo = "text",
                        name=as.character(e)
                        )
    }
    
    p %>% layout(legend = list(orientation = "h",   
                               xanchor = "center", 
                               x = 0.5))
    

    【讨论】:

    • 谢谢你,kikoralston,提供解决方案和解释!我会坚持阴谋的方式。关于 ggplotly 方式,我设法在 x 轴上增加了分钟,但它有点拥挤。现在它需要某种“自动隐藏”选项,我还找不到。我会更新我的帖子。 - 一切顺利!
    猜你喜欢
    • 2023-03-10
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    • 2015-01-08
    • 1970-01-01
    • 2013-10-02
    • 1970-01-01
    • 2019-07-21
    相关资源
    最近更新 更多