0

I am always struggeling with this, so I think it is finally time to ask some help...

I tried to make a reproducible example, but for some reason I cannot get my x$monthday in the %m-%d format :(.

x<-data.frame(seq(as.POSIXct('2012-10-01'), as.POSIXct('2015-03-01'), by= "day"))
names(x)<- "date"
x$month<- months(x$date)
x$monthday<- as.POSIXct(x$date, format= "%m-%d")
x1<- x[x$month== 'October' |x$month== 'November' | x$month== 'December' |x$month== 'January'|x$month== 'February', ]
y<- 1: nrow(x1)
x2<-cbind(x1, y)
x3<- aggregate(list(y=x2$y), list(monthday=x2$monthday), mean)
plot(x3$monthday, x3$y)

The date has the format of %m/%d and is of a time series from October-March. R orders the axis beautifully from January to December, which causes a big gap in between, because my data range from October-March.

How can I make my x axis order in the form from October-March?

Thank you very much in advance.

Rosanne
  • 610
  • 1
  • 5
  • 11
  • 4
    Could you create a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)? – David Robinson Apr 16 '15 at 15:26
  • Do you mean that you don't want any gaps in the x-axis for date-ranges where there aren't any data? – eipi10 Apr 16 '15 at 15:55
  • I want my x axis from October-March and my data aggregated by day, averaged over all years. – Rosanne Apr 16 '15 at 17:31

1 Answers1

1
library(dplyr)
library(ggplot2)
library(lubridate)

# Fake data
dat <- data.frame(date=seq(as.POSIXct('2012-10-01'), as.POSIXct('2015-03-01'), by="day"))
set.seed(23)
dat$temperature = cumsum(rnorm(nrow(dat)))

# Subset to October - March
dat <- dat[months(dat$date) %in% month.name[c(1:2,10:12)], ]

# Calculate mean daily temperature
dat = dat %>% group_by(Month=month(date), Day=day(date)) %>%
  summarise(dailyMeanTemp = mean(temperature)) %>%
  mutate(newDate = as.Date(ifelse(Month %in% 10:12, 
                            paste0("2014-", Month, "-", Day),
                            paste0("2015-", Month, "-", Day))))

The mutate function above creates a fake year, only so that we can keep the dates in "date" format and get them ordered from October to March. There's probably a better way to do it (maybe a function in the zoo or xts packages), but this seems to work.

ggplot(dat, aes(newDate, dailyMeanTemp)) +
  geom_line() + geom_point() +
  labs(y="Mean Temperature", x="Month")

enter image description here

Or, in base graphics:

plot(dat$newDate, dat$dailyMeanTemp)
eipi10
  • 91,525
  • 24
  • 209
  • 285
  • Thank you for your answer! The axis look great. But I don't need the year variable, I aggregate it to a mean temperature per day. I am not familiar with most of the things you use with the aggregate. Do you also have an example for the basic plot system? Thank you so much! – Rosanne Apr 16 '15 at 17:08
  • Now I'm a bit confused about what you want. In a comment to your question you said you were aggregating by month. There's no need to aggregate by day, since your data is already daily data (at least it is in your example). Do you mean you want the average temperature for each day of the year, averaged over all the years for which you have daily temperature measurements? – eipi10 Apr 16 '15 at 17:15
  • I am sorry for the confusing. What you describe is exactly what I want. But then for the period October-March. So the year doesn't matter, just the day of the year. – Rosanne Apr 16 '15 at 17:20
  • I've updated my answer. Let me know if this is what you were hoping for and if you have any questions about the code. – eipi10 Apr 16 '15 at 17:44
  • you've perfectly answered my question, thank you very much for your time!! Rosanne – Rosanne Apr 16 '15 at 18:08