1

I'm trying to plot 'time of day' per week during 2000-2011. This is a follow-up to my earlier question Aggregating mean “%H%M” in “week” I would like to be able to use scale_x_date of ggplot2, but I'm having a hard time to transform my Group.1 chr which is %W/%g back to a Date format to be able to use this particular scale. I have tried a few different things, including the script below, but I don't end-up with what I am expecting...?

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"

Following Gsee's recommendations, this is my dataset:

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")
Community
  • 1
  • 1
GodinA
  • 1,053
  • 3
  • 17
  • 27

0 Answers0