我想重现那个情节:
来自link
我几乎能够做到这一点,但我想念一些事情:
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)
答案 0 :(得分:2)
你非常接近!
只需对 for_plot 列的class
进行两处小修改:
将月更改为每月获得一种颜色的因子
for_plot$month = as.factor(for_plot$month)
由于工作日是character
ggplot,默认情况下会尝试将其排序为字符。要防止此转为factor
中的工作日并指定级别。
for_plot$weekday = factor(for_plot$weekday,
levels = c("Monday", "Thursday", "Tuesday", "Wednesday", "Friday", "Saturday", "Sunday"))
当您使用德语区域设置运行R时,您已用德语进行操作,因此levels=c("Montag",...,"Sonntag")
要获得该行上的点,只需添加geom_point
,如下所示:
ggplot(for_plot, aes(weekday, series, group=month, col=month)) +
geom_line() +
geom_point()
这会给你