工作日因素按字母顺序显示

时间:2018-01-21 19:00:38

标签: r

我正在尝试使用下面的代码创建箱图。我还包括了样本数据。这是时间序列数据,我有一个日期时间字段,我为工作日创建了一个因子。我遇到的问题是,我在工作日显示在箱图中时出现故障。我认为它按字母顺序而不是按时间顺序排列。有谁知道如何解决这一问题?另外,我想将x轴标签旋转45度,这样它们都适合,并将它们全部显示出来。

代码:

boxplot(OrderCnt ~ Weekday, data=icartdf_factor, main="Orders vs Weekday", xlab="Weekday", ylab="Orders")

示例数据:

dput(droplevels(icartdf_factor[1:50,]))
structure(list(OrderCnt = c(1L, 1L, 0L, 0L, 0L, 2L, 5L, 12L, 
16L, 30L, 27L, 21L, 23L, 27L, 37L, 36L, 35L, 30L, 27L, 17L, 8L, 
2L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 2L, 6L, 9L, 19L, 28L, 27L, 28L, 
22L, 29L, 41L, 35L, 43L, 42L, 27L, 16L, 10L, 2L, 1L, 0L, 0L, 
0L), DateTime = structure(c(1398931200, 1398934800, 1398938400, 
1398942000, 1398945600, 1398949200, 1398952800, 1398956400, 1398960000, 
1398963600, 1398967200, 1398970800, 1398974400, 1398978000, 1398981600, 
1398985200, 1398988800, 1398992400, 1398996000, 1398999600, 1399003200, 
1399006800, 1399010400, 1399014000, 1399017600, 1399021200, 1399024800, 
1399028400, 1399032000, 1399035600, 1399039200, 1399042800, 1399046400, 
1399050000, 1399053600, 1399057200, 1399060800, 1399064400, 1399068000, 
1399071600, 1399075200, 1399078800, 1399082400, 1399086000, 1399089600, 
1399093200, 1399096800, 1399100400, 1399104000, 1399107600), class = c("POSIXct", 
"POSIXt")), Weekday = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c("Friday", 
"Saturday", "Thursday"), class = "factor"), hourcol = structure(c(2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 
17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 
20L, 21L, 22L, 23L, 24L, 1L, 2L, 3L), .Label = c("00", "01", 
"02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", 
"13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"
), class = "factor")), .Names = c("OrderCnt", "DateTime", "Weekday", 
"hourcol"), row.names = c(NA, 50L), class = "data.frame")

1 个答案:

答案 0 :(得分:2)

您只需在因子本身中指定所需的顺序即可。

 if (pot<0);
 {
 digitalWrite(led1, LOW);
 digitalWrite(led2, LOW);
 digitalWrite(led3, LOW);
 }

if ((pot > 1) && (pot < 341));
{
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
}

if ((pot>341) && (pot<682));
{
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, LOW);
}

if ((pot>682) && (pot<1024));
{
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
}

Revised Boxplot

要更改标签的角度,请查看How can I change the angle of the value labels on my axes?