I am relatively new to using Plotly and am having some issues with formatting my plots.
I have a data frame (which I have summarized from my original data):
df$time_of_eating_occasion_hhmm <- chron(times = df$time_of_eating_occasion_hhmm)
df$rounded_eating_time <- round(df$time_of_eating_occasion_hhmm, "01:00:00")
df %>%
select(intake_day_of_the_week, intake_day_cat, time_of_eating_occasion_hhmm, rounded_eating_time, total_sugars_gm) %>%
group_by(intake_day_cat, rounded_eating_time) %>%
summarize(m = mean(total_sugars_gm)) %>%
arrange(match(intake_day_cat, c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")), rounded_eating_time)
The data looks like:
intake_day_cat rounded_eating_time m
<chr> <times> <dbl>
1 Monday 00:00:00 8.40
2 Monday 01:00:00 6.82
3 Monday 02:00:00 12.4
4 Monday 03:00:00 7.47
5 Monday 04:00:00 9.11
6 Monday 05:00:00 7.95
7 Monday 06:00:00 7.48
8 Monday 07:00:00 7.23
9 Monday 08:00:00 7.50
10 Monday 09:00:00 7.66
Where m is grouped by every 30 minutes, by day of the week.
When I pipe my data to plotly, it seems to want to format the x-axis as decimals (0 - 1) as portions of a day, but I want the x-axis to reflect the half hour increments in the "rounded_eating_time" column. Does anyone have any advice on what I should do?
plot_ly(x = ~rounded_eating_time, y = ~m) %>%
add_lines(color = ~intake_day_cat) %>%
layout(
xaxis = list(
type = 'time',
tickformat = "%H:%M:%S"
))
I have tried using "array" type for tickmode option but it did not change anything. Any advice is appreciated!
Update: It turns out that converting time objects like mine, which were just in hours, minutes, and seconds (HH:MM:SS) into datetimes in R work better in formatting time series ticks in Plotly. This is the code that I used, which worked in formatting my x-axis.
'''
df$eating_time <- as.POSIXct(df$time_of_eating_occasion_hhmm, format = "%H:%M:%S")
df %>%
select(intake_day_of_the_week, intake_day_cat, time_of_eating_occasion_hhmm, eating_time, !! rlang::sym(x)) %>%
mutate(eating_time = format(round(eating_time, units="hours"), format="%H:%M")) %>%
group_by(intake_day_cat, eating_time) %>%
summarize(m = mean(!! rlang::sym(x))) %>%
arrange(match(intake_day_cat,
c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")),
eating_time)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With