r - ggplot2: scale_*_time formats incorrectly -


goal

use ggplot2::scale_*_time format time axis in particular way (with recent version of ggplot2).

minimal reprex

# example data tib1 <- tibble::tibble(     x = 1:10,     y = lubridate::as.duration(seq(60, 600, length.out = 10)) ) 

using ggplot2::scale_*_time format = "%r" doesn't work - axes formatted using %h:%m:%s:

# doesn't work ggplot2::ggplot(tib1) +     ggplot2::geom_point(ggplot2::aes(x,y)) +     ggplot2::scale_y_time(labels = scales::date_format(format = "%r")) 

however, can add time variable (y) arbitrary date, , format resultant datetime object:

# works tib2 <- tib1 %>%     dplyr::mutate(z = lubridate::ymd_hms("2000-01-01 00:00:00") + y)  ggplot2::ggplot(tib2) +     ggplot2::geom_point(ggplot2::aes(x,z)) +     ggplot2::scale_y_datetime(labels = scales::date_format(format = "%r")) 

obviously, i'd prefer not add arbitrary date times axis format correctly (which had until ggplot2::scale_*_time introduced, hope avoid now).

your durations silently converted hms object, displayed hh:mm:ss (i.e. hms). scales::date_format uses format doesn't hms objects, other converting them character vectors. ideally, easy have proper format.hms method can control how shown, stands hms:::format.hms doesn't take format argument.

a solution delete first 3 characters:

ggplot2::ggplot(tib1) +   ggplot2::geom_point(ggplot2::aes(x,y)) +   ggplot2::scale_y_time(labels = function(x) substring(x, 4)) 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -