在r中使用ggplot2创建月份大小的bin [英] Creating month-sized bins with ggplot2 in r

查看:425
本文介绍了在r中使用ggplot2创建月份大小的bin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组数据,我需要绘制在ggplot2。它是时间排序。这是资料集:

  testset<  -  data.table(Date = as.Date(c -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 

我所做的是绘制它,并使用scale_x_date将过期数据放在图表上。但是,当我这样做,它不会把它分成月份大小的bin。

  ggplot(testset,aes(Date,fill = Action) )+ geom_bar(position =dodge)+ scale_x_date(labels = date_format(%b%Y),breaks = date_breaks(width =months))



我想做的是将每个月的所有日期放入单个bin,然后绘图。我没有找到任何解决方案时,通过StackOverflow和没有呈现本身,而通过文献ggplot2或scale_x_date。我不反对使用lubridate或动物园,如果他们可以提供我正在寻找的输出。



提前感谢!



编辑:看到以下来自@mnel的回答后,我最终发布了关于如何修改图表的问题。在@tonytonov给我的帮助和@mnel给我在这里,我想出了一个解决方案。

  ggplot ,aes(as.factor(as.yearmon(Date)),fill = Action))+ 
geom_bar(position ='dodge')+ xlab(Date)+ ylab b

最后给我这张图:



img src =https://i.imgur.com/eMvOhyg.pngalt =最终图>



感谢你们!

解决方案

您可以使用动物园 yearmon / code>包。在动物园包中有缩放_..._ yearmon 函数,其工作方式类似于缩放包中的缩放函数

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

>


I have a set of data that I need to plot in ggplot2. It is time ordered. This is the 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 I am doing is plotting it and using scale_x_date to put dated data on the graph. However, when I do this, it doesn't bin it into month-sized bins. Instead, it bins it by the day that the action was 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 I would like to do is have all of the dates for each month placed into a single bin and then graphed. I haven't found any solutions when going through StackOverflow and nothing presented itself while going through the literature for ggplot2 or scale_x_date. I'm not opposed to using lubridate or zoo if they can provide the output that I am looking for.

Thanks in advance!

EDIT: After seeing the answer below from @mnel, I ended up posting a question about how to modify plots. Between the help that @tonytonov gave me there and that @mnel gave me here, I figured out a solution.

ggplot(testset, aes(as.factor(as.yearmon(Date)), fill=Action)) + 
geom_bar(position='dodge') + xlab("Date") + ylab("Count")

This ended up giving me this graph:

Thanks to both of you!

解决方案

You can use the yearmon class from the zoo package. There is a scale_..._yearmon function in the zoo package that works similarly to the scale_x_date function in the 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()

这篇关于在r中使用ggplot2创建月份大小的bin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆