The code below is supposed to loop over 11 rows of data in a data.frame, take the means of subsets of each row, and then bar chart the means of each row's subsets against the day of the week. When it makes these bar charts, each mean is correctly plotted with the right day of the week, but the bars are ordered alphabetically instead of in the order the data was presented. Why is that and how can it be fixed?
dailyData[1] <- lapply(dailyData[1], function(x) (x-2)%%7 + 1)
data <- vector(mode="numeric", length=7)
dotw <- weekdays(x=as.Date(seq(7), origin="1950-01-01"))
for(i in 0:10){
for(day in 1:7){
ss <- subset(dailyData, DOTY == day)
data[day] <- sum(ss[[i+2]])/length(ss[[i+2]])
}
df <- data.frame(data)
myplot <- ggplot(df, aes(y=df[[1]], x=dotw))
myplot + geom_bar(stat="identity") + xlab("Day Of The Week") + ylab("Mean Hourly Usage") + ggtitle(paste("The effects of DOTW on usage for customer", i, "in 2012", sep=" "))
ggsave(filename=paste("C:\\SCRATCHDIR\\Plots\\DOTW\\", i, ".jpg", sep=""))
}