0

我正在处理“时间”类的一些计时数据,并将其绘制在散点图中。但是,我希望标签的%H:%M:%S格式适用于 x 轴上的变量Tim.V。简单地添加scale_x_continuous(labels = "%H:%M:%S") 到下面的代码似乎并不能解决问题。我不需要以任何方式转换数据 - 只需 x 轴上标签的格式。关于如何做到这一点的任何见解?看起来应该很简单。

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

样本数据(年龄是数字,Tim.V 是“次”)

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
4

2 回答 2

1

您还可以使用lubridate::ymd_hms虚拟日期转换为日期时间,并使用 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)

在此处输入图像描述

使用的样本数据:

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)
  )
于 2019-06-01T13:46:21.863 回答
0

这很好用:

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

在此处输入图像描述

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)
于 2017-05-17T23:39:50.200 回答