我有一个这样的数据框:
dput(头(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与我的后数据框架进行比较时,它们是相同的:
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 ...
当我绘制他的数据数据框时,我会在y轴上得到小时和分钟。当我尝试绘制t数据框时,y轴为0.00,0.25,0.50,0.75和1.0。
答案 0 :(得分:0)
TIME1
中的值可能未存储为实际时间。当我读取您的数据并执行此操作时:
sapply(t, class)
它报告TIME1
存储为character
。您需要将其更改为时间对象,例如:
t$TIME1 <- as.POSIXlt(t$TIME1, format='%H:%M')
这些情节对我有用。至少我认为这是你要求的。
P.S。我会避免使用t
作为变量名称,而是base function。