0

我有一个这样的数据框:

输入(头(t,30))

structure(list(DATE = structure(c(15744, 15744, 15744, 15744, 
15744, 15744, 15744, 15744, 15744, 15744, 15744, 15744, 15744, 
15744, 15744, 15744, 15744, 15744, 15744, 15744, 15744, 15744, 
15744, 15744, 15744, 15744, 15744, 15744, 15744, 15744), class = "Date"), 
    TIME1 = c("00:14", "00:29", "00:44", "00:59", "01:14", "01:29", 
    "01:44", "01:59", "02:14", "02:29", "02:44", "02:59", "03:14", 
    "03:29", "03:44", "03:59", "04:14", "04:29", "04:44", "04:59", 
    "05:14", "05:29", "05:44", "05:59", "06:14", "06:29", "06:44", 
    "06:59", "07:14", "07:29"), CPU = c(27.7058823529412, 28.1, 
    25.5444444444444, 24.4333333333333, 25.3222222222222, 22.3666666666667, 
    20.8555555555556, 19.5777777777778, 20.8555555555556, 20.0333333333333, 
    19.1888888888889, 18.5444444444444, 19.3333333333333, 19.0222222222222, 
    17.3111111111111, 17.2777777777778, 17.2777777777778, 17.1555555555556, 
    17.2333333333333, 17.3777777777778, 17.5444444444444, 18.2222222222222, 
    17.7444444444444, 18.6333333333333, 21.6333333333333, 23.9, 
    27.9666666666667, 28.5222222222222, 32.1777777777778, 33.0111111111111
    )), .Names = c("DATE", "TIME1", "CPU"), row.names = c(NA, 
30L), class = "data.frame")

我正在尝试创建一个 ggplot,它将在 x 轴上具有 DATE,在 y 轴上具有 TIME1。目前,如果我尝试绘制它,我会在 y 轴上看到 TIME1 的所有值,这使得阅读图表变得非常困难。

有没有办法在 y 轴上缩放 TIME1?

正如博客文章(http://blog.ggplot2.org/post/29433173749/defining-a-new-transformation-for-ggplot2-scales-part)所强调的,我做了以下事情:

t$TIME<-times(t$TIME1)

times_trans <- function() {
    fmt <- function(x) {
        format(x, simplify = !any(diff(x) < 1/(24*60)))
    }
    trans_new("chrontimes",
              transform = as.numeric,
              inverse = times,
              breaks = pretty_breaks(),
              format = fmt,
              domain=c(0,1))
}


timesreverse_trans <- function() {
    trans <- function(x) {-as.numeric(x)}
    inv <- function(x) {times(-x)}
    fmt <- function(x) {format(x, simplify = !any(diff(x) < 1/(24*60)))}
    trans_new("chrontimes-reverse",
              transform = trans,
              inverse = inv,
              breaks = pretty_breaks(),
              format = fmt,
              domain=c(0,1))
}


scale_y_times <- function(..., trans=NULL) {
    scale_y_continuous(trans=timesreverse_trans(), ...)
}

然后

当我这样做时:

ggplot(t,aes(DATE, TIME1, group=SERVER, fill=CPU)) + geom_tile() + facet_wrap(~SERVER) +scale_y_times()

我从 00:00, 00:25, 00:50, 00:75, y 轴上的 1 个值中获取值。有什么我可能在这里遗漏的想法吗?

当我将 Brian Diggs post dat 数据框与我的比较时,它们是相同的:

str(t)
'data.frame':   55076 obs. of  4 variables:
 $ TIME  :Class 'times'  atomic [1:55076] 0.0101 0.0205 0.0309 0.0413 0.0517 ...
  .. ..- attr(*, "format")= chr "h:m:s"
 $ CPU   : num  27.7 28.1 25.5 24.4 25.3 ...
 $ SERVER: chr  "cigp04a4a002" "cigp04a4a002" "cigp04a4a002" "cigp04a4a002" ...
 $ DATE  : Date, format: "2013-02-08" "2013-02-08" "2013-02-08" "2013-02-08" ...

\n

str(dat)
'data.frame':   11 obs. of  2 variables:
 $ time :Class 'times'  atomic [1:11] 0.776 0.702 0.629 0.556 0.482 ...
  .. ..- attr(*, "format")= chr "h:m:s"
 $ value: int  7 6 9 11 10 1 4 2 3 5 ...

当我绘制他的 dat 数据框时,我确实在 y 轴上得到了小时和分钟。当我尝试绘制我的 t 数据框时,y 轴从 0.00、0.25、0.50、0.75 和 1.0 开始。

4

1 回答 1

0

中的值TIME1可能未存储为实际时间。当我阅读您的数据并执行此操作时:

sapply(t, class)

它报告TIME1存储为character. 您需要将其更改为时间对象,例如:

t$TIME1 <- as.POSIXlt(t$TIME1, format='%H:%M')

这些情节对我有用。至少我认为这就是你所要求的。

PS 我会避免使用t作为变量名,这是一个基本函数

于 2013-05-02T09:28:26.433 回答