按月订购ggplot中的日期 [英] Order dates in ggplot by month
问题描述
我有as.Date格式的 DF $ Date
,格式为"yyyy-mm-dd",如下所示.有没有一种简单的方法可以将这些数据按月在ggplot中分组?
日期2015-07-302015-08-012015-08-022015-08-062015-08-112015-08-12
我在 DF $ Month
列中添加了年月份名称"(例如2015年4月).我正在通过 DF $ Month< -strftime(DF $ Date,format =%B%Y")
是否有一种快速的方法可以将月份/年份分解为序数?我通过使用以下格式设置了解决方法: DF $ Month< -strftime(DF $ Date,format =%Y-%m")
,以便较大的数字位于第一个,然后是月份.这给出了可排序的输出:
DF $ Month"2015-07""2015-08"
此输出使我可以进行以下分组:
I have DF$Date
in the as.Date format "yyyy-mm-dd" as shown below. Is there an easy way to get these grouped by month in ggplot?
Date
2015-07-30
2015-08-01
2015-08-02
2015-08-06
2015-08-11
2015-08-12
I've added a column DF$Month
as "year Monthname" (e.g. April 2015.)
I'm doing this by DF$Month<-strftime(DF$Date,format="%B %Y")
Is there a quick way to factor the month/years so that they are ordinal?
I used a workaround by formatting using:
DF$Month<-strftime(DF$Date,format="%Y-%m")
so that the larger numbers are first and subsequently the month number.
This gives the output, which is sortable:
DF$Month
"2015-07"
"2015-08"
This output allows me to get this grouping: http://imgur.com/df1FI3s When using this plot:
MonthlyActivity<-ggplot(DF,aes(x=Month, y=TotalSteps))+
geom_boxplot()
MonthlyActivity
Any alternatives so I can use the full month name and still be in the correct time order?
There are probably other solutions, but here is one with full month names as a factor. As you already found out, you need a x variable to group by. We can then treat it as a 'order a factor' problem instead of a date-scale problem.
#first, generate some data
dat <- data.frame(date=sample(seq(as.Date("01012015",format="%d%m%Y"),
as.Date("01082015", format="%d%m%Y"),by=1),1000,T),
value=rnorm(1000))
We find the minimum and maximum month, and do some date-arithmetic to allow for all start-days (so that february doesn't get skipped when the minimum date is on the 29th/30th/31st). I used lubridate for this.
library(lubridate)
min_month = min(dat$date)-day(min(dat$date))+1
max_month = max(dat$date)-day(max(dat$date))+1
We generate a grouping variable. It is a factor with labels like 'January 2015, March 2015'. However, we force the order by creating a sequence (by month) from min date to max date and formatting it in the same way.
dat$group <- factor(format(dat$date, "%B %Y"),
levels=format(seq(min_month, max_month,by="month"),
"%B %Y"))
This forces the ordering on the axis:
这篇关于按月订购ggplot中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!