Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replicate a Monthly Cycle Chart in R

Tags:

plot

r

ggplot2

I'd like to output a chart similar to the one represented on this page (on the right) using R and any package that would make it look good:

http://processtrends.com/pg_charts_monthly_cycle_chart.htm

Anyone up to the challenge? :)

Thanks!

like image 383
datayoda Avatar asked Apr 29 '11 00:04

datayoda


2 Answers

The stats package in R base already has a function to do this. Here is my one-liner and the output that it produces

monthplot(redata, labels = month.abb, ylab = 'Listings')

enter image description here

Building on this an example of using monthplot for a weekly cycle plot is here (gives full R code and source data): http://figshare.com/figures/index.php/OpenURL_Router_Data:_Total_Requests_by_Weekday

monthplot(ts(sdf$values, frequency = 7, start=c(12,5)), labels = dow, ylab = "No. requests / day", xlab = "Weekday") 

which gives this weekly cycle plot: Example of monthplot to create a graph showing a weekly cycle http://figshare.com/figures/images/a/a7/Total_requests_by_weekday_01_Apr_to_31_Jul_2011.jpeg

like image 127
Ramnath Avatar answered Oct 15 '22 16:10

Ramnath


Of course no graphical challenge will be complete without a ggplot solution. The tricky bit is to use ddply to summarise the monthly averages, and to pass this as data to a separate layer to ggplot.

library(lubridate)
library(plyr)
library(ggplot2)

df$month <- factor(month(df$dates), levels=1:12, labels=month.abb, ordered=TRUE)
df$year  <- year(df$dates)

hline.data <- ddply(df, .(month), summarize, avgvalue=mean(values))


ggplot() + 
  geom_line(aes(x=year, y=values, group=month), data=df, colour="blue") +
  geom_hline(aes(yintercept=avgvalue), data=hline.data, colour="blue", size=2) + 
  facet_grid(~month) +
  opts(axis.text.x = theme_blank()) +
  xlab("")

The data:

df <- structure(list(dates = structure(c(8415, 8446, 8474, 8505, 8535, 
                8566, 8596, 8627, 8658, 8688, 8719, 8749, 8780, 8811, 8839, 8870, 
                8900, 8931, 8961, 8992, 9023, 9053, 9084, 9114, 9145, 9176, 9204, 
                9235, 9265, 9296, 9326, 9357, 9388, 9418, 9449, 9479, 9510, 9541, 
                9570, 9601, 9631, 9662, 9692, 9723, 9754, 9784, 9815, 9845, 9876, 
                9907, 9935, 9966, 9996, 10027, 10057, 10088, 10119, 10149, 10180, 
                10210, 10241, 10272, 10300, 10331, 10361, 10392, 10422, 10453, 
                10484, 10514, 10545, 10575, 10606, 10637, 10665, 10696, 10726, 
                10757, 10787, 10818, 10849, 10879, 10910, 10940, 10971, 11002, 
                11031, 11062, 11092, 11123, 11153, 11184, 11215, 11245, 11276, 
                11306, 11337, 11368, 11396, 11427, 11457, 11488, 11518, 11549, 
                11580, 11610, 11641, 11671, 11702, 11733, 11761, 11792, 11822, 
                11853, 11883, 11914, 11945, 11975, 12006, 12036, 12067, 12098, 
                12126, 12157, 12187, 12218, 12248, 12279, 12310, 12340, 12371, 
                12401, 12432, 12463, 12492, 12523, 12553, 12584, 12614, 12645, 
                12676, 12706, 12737, 12767, 12798, 12829, 12857, 12888, 12918, 
                12949, 12979, 13010, 13041, 13071, 13102, 13132), class = "Date"), 
    values = c(1093, 1182, 1299, 1372, 1319, 1362, 1239, 1162, 
        1059, 921, 815, 720, 835, 853, 1034, 1030, 1240, 1388, 1429, 
        1319, 1231, 1184, 1076, 825, 991, 1093, 854, 808, 1079, 1092, 
        1220, 1251, 1130, 1131, 1052, 951, 950, 1006, 1112, 1119, 
        1250, 1322, 1347, 1310, 1215, 1128, 1035, 992, 1079, 1018, 
        1112, 1224, 1323, 1344, 1326, 1267, 1171, 1075, 916, 932, 
        888, 904, 939, 1018, 1140, 1174, 1285, 1311, 1298, 1231, 
        1091, 1088, 991, 1028, 1177, 1322, 1322, 1398, 1389, 1174, 
        1196, 1115, 756, 496, 693, 673, 748, 777, 820, 948, 966, 
        1027, 960, 865, 767, 675, 765, 732, 613, 632, 659, 705, 684, 
        734, 715, 626, 551, 487, 500, 536, 575, 595, 736, 798, 832, 
        797, 792, 726, 650, 584, 567, 524, 574, 571, 591, 657, 699, 
        756, 867, 795, 760, 685, 609, 588, 521, 581, 614, 623, 668, 
        702, 777, 697, 647, 562, 523, 508, 493, 504, 534, 586, 621, 
        620, 636, 600, 549, 557)), .Names = c("dates", "values"), row.names = c(NA, 
    -156L), class = "data.frame")

enter image description here

like image 20
Andrie Avatar answered Oct 15 '22 16:10

Andrie