2013年9月23日星期一

R :通过 cut + ggplot 迅速画出 按时段 week / month 统计图

见:Plot Weekly or Monthly Totals in R

切 week / month :

log$Month <- as.Date(cut(log$Date,  breaks = "month"))

log$Week <- as.Date(cut(log$Date,  breaks = "week",  start.on.monday = FALSE))

统计并画图:

ggplot(data = log,
  aes(Month, Quantity)) +
  stat_summary(fun.y = sum, # adds up all observations for the month
    geom = "bar") + # or "line"
  scale_x_date(
    labels = date_format("%Y-%m"),
    breaks = "1 month") # custom x-axis labels


用ggplot2画折线图、面积图、直方图等等,笔记:chart_example

没有评论:

发表评论