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.
I want it to look like this, only with daily data instead of monthly. ggplot: Multiple years on same plot by month
format(df$date, "%d %m")
. – r2evansdput(data)
and edit the output into your original post. I'm sure your problem has a simple solution, but it's not possible to verify with the data you've provided so far. You stated that you want to overlay data for multiple years, but the data provided is all 2017. I also couldn't reproduce that graph using the data you provided. – Jul