我试图在2000 - 2011年期间每周绘制“一天中的时间”。这是我之前问题的后续问题Aggregating mean “%H%M” in “week”我希望能够使用ggplot2的scale_x_date
,但我很难转换Group.1
chr 是%W /%g回到日期格式,以便能够使用此特定比例。我尝试了一些不同的东西,包括下面的脚本,但我最终没有达到我所期待的......?
out$Group.1<-as.Date(out$Group.1, "%W/%g")
out$Group.1
[1] "2006-07-18" "2010-07-18" "2010-07-18" "2011-07-18" "2004-07-18" "2010-07-18"
根据Gsee的建议,这是我的数据集:
out <- aggregate(halibut_0A$stime, list(format(halibut_0A$datetime, "%W/%g")), mean)
out[order(as.numeric(paste(substr(out[, 1], 4, 5), substr(out[, 1], 1, 2)))), ]
out
Group.1 x
1 20/06 1970-01-01 04:26:00
2 26/10 1970-01-01 03:21:00
3 27/10 1970-01-01 08:30:45
4 27/11 1970-01-01 09:12:40
5 28/04 1970-01-01 09:40:00
6 28/10 1970-01-01 09:03:24
7 28/11 1970-01-01 06:52:40
8 29/07 1970-01-01 06:07:15
9 29/08 1970-01-01 05:36:00
10 29/10 1970-01-01 08:13:16
str(out)
'data.frame': 195 obs. of 2 variables:
$ Group.1: chr "20/06" "26/10" "27/10" "27/11" ...
$ x : POSIXct, format: "1970-01-01 04:26:00" "1970-01-01 03:21:00" ...
#write now I can use geom_bar (discrete scale), but my out$Group.1, "%W/%g" is not in the chronological order...
ggplot(out, aes(Group.1, x)) + geom_bar() +
scale_y_datetime(labels = date_format("%H-%M"),breaks = date_breaks("1 hour")) + xlab("Week of the year and year")