如何在 r 中使用 plotly 显示每个事件本身的所有事件持续时间
How to display all durations of events per the events themselves with plotly in r
我想在 rshiny
中创建一个小部件,使用 plotly
显示事件的持续时间。
我有一个 df
,其中包含 4 个事件及其 start
ing 和 end
ing 时间。
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)
我只能想出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))
我想在 rshiny
中创建一个小部件,使用 plotly
显示事件的持续时间。
我有一个 df
,其中包含 4 个事件及其 start
ing 和 end
ing 时间。
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)
我只能想出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))