celestialgod

ggplot count

Aug 13th, 2015
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 0.88 KB | None | 0 0
  1. library(tidyr)
  2. library(dplyr)
  3. library(magrittr)
  4. library(data.table)
  5. library(ggplot2)
  6. dat = data.table(date=c("2013-10-09","2013-10-05","2014-06-17","2014-06-21","2015-02-26",
  7.          "2013-02-25","2013-04-25","2013-09-18","2014-01-16","2015-02-10","2015-06-18",
  8.          "2013-01-30","2014-01-14","2014-05-30","2014-06-14","2014-10-27","2014-11-05",
  9.          "2013-05-25","2013-10-01","2014-01-11","2014-02-09","2014-10-08","2014-11-02"))
  10. dat %<>% mutate(date = as.Date(date,"%Y-%m-%d"), Y = year(date), M = month(date))
  11. dat2 = dat %>% dcast.data.table(Y ~ M, length)
  12. tmp = setdiff(as.character(1:12), names(dat2)[-1])
  13. dat2 %<>% cbind(matrix(as.integer(0), nrow(dat2), length(tmp))) %>%
  14.   setnames(paste0("V", 1:length(tmp)), tmp) %>% gather(M, count, -Y)
  15. ggplot(dat2, aes(x=M, y=count, group=Y, colour=Y)) +
  16.   geom_line(size=1.2,color='steelblue2')+
  17.   facet_grid(Y~.,scales='free_x')
Advertisement
Add Comment
Please, Sign In to add comment