MrNetherlands
MrNetherlands

Reputation: 940

How to plot days on x-axis instead of hours with lubridate's duration object

I have this data and I would like to plot the value against the time. The time column is a duration object from lubridate.

df = structure(list(value = c(0.146629368669261, -0.174585031699523, 
-0.0657191327768585, 0.206486326659542, 1.9076161363563, 0.319682206047181, 
0.00402265500880539, 0.125472536213693, 0.0868479511815217), 
    time = new("Duration", .Data = c(3600, 21600, 43200, 86400, 
    172800, 345600, 518400, 691200, 1382400))), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -9L))

df %>%
  ggplot(aes(x = time, y=value)) +
  geom_line() +
  scale_x_time()

enter image description here

How can I archive potentially with scale_x_time() that the x-axis shows not the hours in the format "Hours:Minutes:Seconds" but the number of days?

Upvotes: 0

Views: 896

Answers (1)

AndreasM
AndreasM

Reputation: 942

It seems the duration values are in seconds. Can´t you just rescale the x-ticks and labels accordingly (seconds to days)? Like this:

df %>%
  ggplot(aes(x = time, y=value)) +
  geom_line() +
  scale_x_continuous(breaks = seq(0,365*24*3600, 24*3600),
                     labels = 0:365, name = "time in days")

enter image description here

Upvotes: 2

Related Questions