简体   繁体   English

重新排序因子级别,这些级别是R中的日期,但只有月份和年份

[英]Reorder factor levels that are dates but only month and year in R

I'm using ggvis stacked barplots to create a graph where the x-axis is a time series of dates and the y-axis are frequencies. 我正在使用ggvis堆叠条形图创建一个图形,其中x轴是日期的时间序列,y轴是频率。

The dates however, are in the format "Apr 2015" ie months and years. 但是,日期格式为“ Apr 2015”,即月和年。

In ggvis , this means that I have to make the dates in my dataframe factors. ggvis ,这意味着我必须在数据框因子中确定日期。

I'm having trouble converting these month-year dates into factors that are ordered by their date. 我无法将这些月份日期转换为按日期排序的因子。 Instead, I get this order "Apr 2015, Jun 2015, May 2015" 相反,我收到此订单“ 2015年4月,2015年6月,2015年5月”

This is my code: 这是我的代码:

selecteddates <- with(dailyIndMelt, dailyIndMelt[date >= as.Date(input$monthlydateInd[1]) & date <= as.Date(input$monthlydateInd[2]),])
selecteddates <- aggregate(selecteddates[,3],by=list(selecteddates$variable,substr(selecteddates$date,1,7)),sum)
colnames(selecteddates) <- c("industry", "date", "vacancyno")
selecteddates$date <- as.Date(paste(selecteddates$date,"-28",sep=""))
selecteddates <- selecteddates[order(as.Date(selecteddates$date)),]
selecteddates$date <- format(selecteddates$date, "%m %Y")
selecteddates$date <- factor(selecteddates$date)
levels(selecteddates$date) <- levels(selecteddates$date)[order(as.Date(levels(selecteddates$date), format="%m %Y"))]
levels(selecteddates)

To get the levels in the order you want, you can feed the desired order to the levels argument of factor . 要按所需顺序获得级别,可以将所需的顺序提供给factorlevels参数。 For example: 例如:

selecteddates$date <- factor(selecteddates$date, 
                             levels=paste(month.abb, rep(2014:2015, each=12)))

month.abb is a built-in vector of month abbreviations. month.abb是月份缩写的内置向量。 The paste function pastes together the month abbreviations and the year values, so you get the correct ordering. paste功能将月份的缩写和年份值粘贴在一起,因此您获得了正确的顺序。 I used years 2014 and 2015 for illustration. 我用2014年和2015年为例。 Just change that to whatever years appear in your data. 只需将其更改为数据中显示的年份即可。

Here's the paste statement on its own for illustration: 这是paste语句,仅供说明:

paste(month.abb, rep(2014:2015, each=12))

 [1] "Jan 2014" "Feb 2014" "Mar 2014" "Apr 2014" "May 2014" "Jun 2014" "Jul 2014" "Aug 2014"
 [9] "Sep 2014" "Oct 2014" "Nov 2014" "Dec 2014" "Jan 2015" "Feb 2015" "Mar 2015" "Apr 2015"
[17] "May 2015" "Jun 2015" "Jul 2015" "Aug 2015" "Sep 2015" "Oct 2015" "Nov 2015" "Dec 2015"

Ideally, you can get the desired years programmatically, directly from your data. 理想情况下,您可以直接从数据中以编程方式获得所需的年份。 If your dates start out in a date format, you can do something analogous to this: 如果您的日期以日期格式开始,则可以执行以下操作:

library(lubridate) # For year function

# Fake date data
dates = seq(as.Date("2010-01-15"), as.Date("2015-01-15"), by="1 day")

# Extract the years from the dates and use them to create the month-year levels
levels = paste(month.abb, rep(sort(unique(year(dates))), each=12))

In the code above, the sort ensures the years are in order, even if the dates are out of order. 在上面的代码中,即使日期不正确, sort可以确保年份是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM