Reputation: 1203
I would like to reproduce that plot:
from link
I'm almost able to do that but I miss something:
library(lubridate)
library(ggplot2)
# get the data
dates <- seq(as.Date("2012-01-01"), as.Date("2014-12-30"), by = 1)
series <- seq(from = 1, to = 1095, by = 1)
df<-data.frame(dates,series)
# for aggregation
df$month<-as.numeric(format(df$date, "%m"))
df$week<-week(as.POSIXct(df$date))
df$weekday<-weekdays(as.POSIXct(df$date))
df$days<-as.numeric(format(df$date, "%d"))
df$week_days<-as.numeric(format(df$date, "%w"))
# for plotting
for_plot=aggregate(series ~ + weekday+ month, data =df, FUN=mean)
ggplot(for_plot, aes(x=weekday, y=series)) + geom_line(aes(group=month, color=month),size=2,alpha=0.5)
Upvotes: 0
Views: 2611
Reputation: 21497
You are very close!
Just two little changes to the class
of the for_plot columns:
Change month to factor to get one color per month
for_plot$month = as.factor(for_plot$month)
As weekday is a character
ggplot by default tries to sort it as characters. To prevent this turn weekday in a factor
and specify the levels.
for_plot$weekday = factor(for_plot$weekday,
levels = c("Monday", "Thursday", "Tuesday", "Wednesday", "Friday", "Saturday", "Sunday"))
As you run R with a german locale you have do do it in German, so levels=c("Montag",...,"Sonntag")
To get the dots on the line just add geom_point
as follows:
ggplot(for_plot, aes(weekday, series, group=month, col=month)) +
geom_line() +
geom_point()
This gives you
Upvotes: 2