I'm looking to plot a histogram of times in R with ggplot and plotly. I have the times saved in seconds. I would like the axis and tooltip/hover text to display the time in MM:SS format though. I can get the axis to work (mostly - it only has 2 labels right now and I'm having trouble getting it to show more), but I cannot get the hover text to work.
Is this possible? I've tried dozens of iterations of this, but here is what I have right now:
p2 <- df %>%
mutate(time_formatted = gsub("^0+", "",
sprintf("%02d:%05.2f",
floor(time_seconds/60),
time_seconds %% 60))) %>%
mutate(bucketed_time = floor(time_seconds)) %>%
mutate(hover_text = paste0('Time: ', custom_labels(time_seconds))) %>%
ggplot(aes(x = time_seconds)) +
geom_histogram(fill = mako_palette[50],
binwidth = 1,
position = 'dodge') +
labs(title = 'Times Histogram')) +
scale_y_continuous(name = 'Count') +
scale_x_reverse(name = 'Time',
labels = custom_labels) +
theme_minimal()
ggplotly(p2, dynamicTicks = FALSE, tooltip = 'text', originalData = TRUE, hoverinfo = 'text', text = ~hover_text) %>%
config(displayModeBar = FALSE)
with the function:
custom_labels_bucketed <- function(x) {
minutes <- floor(x / 60)
seconds <- x %% 60
sprintf("%d:%02d", minutes, seconds)
}
Per the comments, I've edited to try to share a sample:
structure(list(time_seconds = c(60.46, 50.84, 64.54, 54.1, 59.21,
73.45, 40.39, 38.31, 69.11, 74.17), name = c("A", "B",
"C", "D", "E", "F", "G",
"H", "I", "J")), row.names = c(NA, -10L
), class = c("tbl_df", "tbl", "data.frame"))