How to plot data averaged by month and day?

柔情痞子 提交于 2019-12-23 05:07:09

问题


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)


回答1:


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



来源:https://stackoverflow.com/questions/35729765/how-to-plot-data-averaged-by-month-and-day

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!