date - Creating month-sized bins with ggplot2 in r -


i have set of data need plot in ggplot2. time ordered. data set:

testset <- data.table(date=as.date(c("2013-07-02","2013-08-03","2013-09-04","2013-10-05","2013-11-06","2013-07-03","2013-08-04","2013-09-05","2013-10-06","2013-11-07")), action = c("a","b","c","d","e","b","a","b","c","a","b","e","e","c","a"), rating = runif(30)) 

what doing plotting , using scale_x_date put dated data on graph. however, when this, doesn't bin month-sized bins. instead, bins day action taken.

ggplot(testset, aes(date, fill=action)) + geom_bar(position="dodge") + scale_x_date(labels = date_format("%b %y"), breaks = date_breaks(width = "months")) 

what have of dates each month placed single bin , graphed. haven't found solutions when going through stackoverflow , nothing presented while going through literature ggplot2 or scale_x_date. i'm not opposed using lubridate or zoo if can provide output looking for.

thanks in advance!

edit: after seeing answer below @mnel, ended posting a question how modify plots. between @tonytonov gave me there , @mnel gave me here, figured out solution.

ggplot(testset, aes(as.factor(as.yearmon(date)), fill=action)) +  geom_bar(position='dodge') + xlab("date") + ylab("count") 

this ended giving me graph:

final graph

thanks both of you!

you can use yearmon class zoo package. there scale_..._yearmon function in zoo package works scale_x_date function in scales package

library(zoo) ggplot(testset, aes(as.yearmon(date), fill=action)) +    geom_bar(position = position_dodge(width  = 1/12),             binwidth=1/12, colour='black') +    scale_x_yearmon() 

enter image description here


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -