0

I am working with some chron data of the class 'times' and am plotting it in a scatter plot. However, I want the labeling to be in %H:%M:%S format for the variable on the x axis Tim.V. Simply adding scale_x_continuous(labels = "%H:%M:%S") to the code below does not seem to do the trick. I don't need to convert the data in any way - just the format of the label on the x axis. Any insight on how to do this? It seems like it should be simple.

doeplotnet <- ggplot(division, aes(x =Tim.V, y = Age)) + geom_point() + scale_x_reverse()

Sample Data (Age is numeric and Tim.V is 'times')

Age     Tim.V
40       00:33:08
36       00:59:27
29       01:05:33
52       00:49:14
49       01:08:00
44       00:30:45
TRW
  • 13
  • 3

2 Answers2

1

You can also use lubridate::ymd_hms to convert to a datetime with a dummy date, and plot that with ggplot2:

library(tidyverse); library(lubridate)
mydata3 <- mydata2 %>%
  mutate(time3 = lubridate::ymd_hms(paste(
    "2000-01-01", hour(time2), minute(time2), second(time2))))

ggplot(mydata3, aes(x=time3, y=pending, color=server, group=tradedate)) +
    geom_point() +
    facet_wrap(~ tradedate)

enter image description here

Sample data used:

mydata2 <-
  data_frame(time2 = new(
    "Period",
    .Data = c(23, 23, 42, 42, 24, 24, 42, 42),
    year = c(0,  0, 0, 0, 0, 0, 0, 0),
    month = c(0, 0, 0, 0, 0, 0, 0, 0),
    day = c(0,
            0, 0, 0, 0, 0, 0, 0),
    hour = c(14, 14, 14, 14, 14, 14, 14, 14),
    minute = c(5, 5, 5, 5, 6, 6, 6, 6)
  ),
  pending = runif(8),
  server = "server1",
  tradedate = rep(ymd(c(20190101, 20190102)), 4)
  )
Jon Spring
  • 55,165
  • 4
  • 35
  • 53
0

This works well:

library(chron)
library(ggplot2)
division$Tim.V <- times(division$Tim.V)
breaks2 <- seq(min(division$Tim.V), max(division$Tim.V), length.out = 5)
labels2 <- times(breaks2)

doeplotnet <- ggplot(division, aes(x = as.numeric(Tim.V), y = Age)) + geom_point() +
 scale_x_reverse(labels = labels2, breaks = breaks2)
doeplotnet

enter image description here

division <- read.table(text= "Age     Tim.V
40       00:33:08
36       00:59:27
29       01:05:33
52       00:49:14
49       01:08:00
44       00:30:45", stringsAsFactors=TRUE, header = TRUE)
Edgar Santos
  • 3,426
  • 2
  • 17
  • 29