Anke
Anke

Reputation: 651

Extract day and month from date

I'm trying to extract only the day and the month from as.POSIXct entries in a dataframe to overlay multiple years of data from the same months in a ggplot.

I have the data as time-series objects ts.

data.ts<-read.zoo(data, format = "%Y-%m-%d")
ts<-SMA(data.ts[,2], n=10)

df<-data.frame(date=as.POSIXct(time(ts)), value=ts)

ggplot(df, aes(x=date, y=value), 
           group=factor(year(date)), colour=factor(year(date))) +
  geom_line() +
  labs(x="Month", colour="Year") +
  theme_classic()

Now, obviously if I only use "date" in aes, it'll plot the normal time-series as a consecutive sequence across the years. If I do "day(date)", it'll group by day on the x-axis. How do I pull out day AND month from the date? I only found yearmon(). If I try as.Date(df$date, format="%d %m"), it's not doing anything and if I show the results of the command, it would still include the year.

data:

> data
         Date  V1
1  2017-02-04 113.26240
2  2017-02-05 113.89059
3  2017-02-06 114.82531
4  2017-02-07 115.63410
5  2017-02-08 113.68569
6  2017-02-09 115.72382
7  2017-02-10 114.48750
8  2017-02-11 114.32556
9  2017-02-12 113.77024
10 2017-02-13 113.17396
11 2017-02-14 111.96292
12 2017-02-15 113.20875
13 2017-02-16 115.79344
14 2017-02-17 114.51451
15 2017-02-18 113.83330
16 2017-02-19 114.13128
17 2017-02-20 113.43267
18 2017-02-21 115.85417
19 2017-02-22 114.13271
20 2017-02-23 113.65309
21 2017-02-24 115.69795
22 2017-02-25 115.37587
23 2017-02-26 114.64885
24 2017-02-27 115.05736
25 2017-02-28 116.25590

If I create a new column with only day and month

 df$day<-format(df$date, "%m/%d")

ggplot(df, aes(x=day, y=value), 
           group=factor(year(date)), colour=factor(year(date))) +
  geom_line() +
  labs(x="Month", colour="Year") +
  theme_classic()

I get such a graph for the two years.

Two time-series in ggplot

I want it to look like this, only with daily data instead of monthly. ggplot: Multiple years on same plot by month

Upvotes: 3

Views: 5906

Answers (1)

Naveen
Naveen

Reputation: 1210

You are almost there. As you want to overlay day and month based on every year, we need a continuous variable. "Day of the year" does the trick for you.

data <-data.frame(Date=c(Sys.Date()-7,Sys.Date()-372,Sys.Date()-6,Sys.Date()-371,
                         Sys.Date()-5,Sys.Date()-370,Sys.Date()-4,Sys.Date()-369,
                         Sys.Date()-3,Sys.Date()-368),V1=c(113.23,123.23,121.44,111.98,113.5,114.57,113.44, 121.23, 122.23, 110.33))

data$year = format(as.Date(data$Date), "%Y")
data$Date = as.numeric(format(as.Date(data$Date), "%j"))

ggplot(data=data, mapping=aes(x=Date, y=V1, shape = year, color = year)) + geom_point() + geom_line() 
  theme_bw()

enter image description here

Upvotes: 3

Related Questions