Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
重新排序系数级别,这些级别为R中的日期,但仅为月份和年份_R_Date_Ggvis - Fatal编程技术网

重新排序系数级别,这些级别为R中的日期,但仅为月份和年份

重新排序系数级别,这些级别为R中的日期,但仅为月份和年份,r,date,ggvis,R,Date,Ggvis,我正在使用ggvis堆叠条形图创建一个图形,其中x轴是日期的时间序列,y轴是频率 但日期格式为“2015年4月”,即月和年 在ggvis中,这意味着我必须在我的数据帧因子中创建日期 我无法将这些月-年日期转换为按日期排序的因子。相反,我得到了这个订单 “2015年4月、2015年6月、2015年5月” 这是我的代码: selecteddates <- with(dailyIndMelt, dailyIndMelt[date >= as.Date(input$monthlydateIn

我正在使用
ggvis
堆叠条形图创建一个图形,其中x轴是日期的时间序列,y轴是频率

但日期格式为“2015年4月”,即月和年

ggvis
中,这意味着我必须在我的数据帧因子中创建日期

我无法将这些月-年日期转换为按日期排序的因子。相反,我得到了这个订单 “2015年4月、2015年6月、2015年5月”

这是我的代码:

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)

selecteddates=as.Date(输入$monthlydateInd[1])&Date要按所需的顺序获取级别,可以将所需的顺序输入到
factor
levels
参数。例如:

selecteddates$date <- factor(selecteddates$date, 
                             levels=paste(month.abb, rep(2014:2015, each=12)))
理想情况下,您可以通过编程直接从数据中获取所需年份。如果日期以日期格式开始,您可以执行类似的操作:

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))
在上面的代码中,
排序
确保年份是有序的,即使日期是无序的

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))