Мы можем использовать reorder
для установки уровней ID
, который является фактором, устанавливаемым на основе Duration
значений.
library(plotly)
library(dplyr)
df1 %>%
group_by(ID) %>%
mutate(Dur.order = sum(as.numeric(Duration))) %>%
ungroup() %>%
mutate(ID.order = reorder(ID, Dur.order)) %>%
plot_ly(data = .,
x = ~ID.order,
y = ~Duration,
name = "Title",
type = "bar",
orientation = 'v',
order = "ascending") %>%
layout(title = "Title",
xaxis = list(title = "ID", tickangle = -45 ),
yaxis = list(title = "Time In Seconds"))
Данные:
df1 <- structure(list(ID = c("A", "A", "B", "B", "A"),
Date = c("12/18/2019 4:45:10 AM", "12/18/2019 9:06:59 PM",
"12/19/2019 4:14:13 AM", "12/19/2019 4:14:20 AM",
"12/18/2019 4:45:11 AM"),
Duration = structure(c(1, 0, 7,0 , 0),
class = "difftime", units = "secs")),
row.names = c(NA, -5L), class = "data.frame")