重新调整24小时X轴

时间:2017-08-23 19:59:34

标签: r datetime ggplot2 time-series timeserieschart

与此相似 - Rearrange x-axis of hour when plotting

然而,当我尝试重新排列X轴以表示从6:00到6:00而不是0:00到24:00的数据时,它会弄乱X轴。

数据:

ggplot(data=intraday_txt_file, aes(x=Time, y=INTA_Lots_mean)) +
geom_point() + geom_line() +
geom_errorbar(aes(ymin=INTA_Lots_mean-INTA_Lots_std,
                  ymax=INTA_Lots_mean+INTA_Lots_std), width=.1)

代码:

public class EnterPress {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        //press enter programmatically

        String readString = scanner.nextLine();
        while (readString != null) {
            if (readString.isEmpty()) {
                System.out.println("Enter obtained!");
                break;
            }

            if (scanner.hasNextLine()) {
                readString = scanner.nextLine();
            } else {
                readString = null;
            }
        }
    }
}

图表:

enter image description here

1 个答案:

答案 0 :(得分:2)

在您的数据集中,时间变量的类是hms& difftime。您链接的解决方案需要将时间转换为Factor类,以便用户可以指定级别的顺序。

试试这个:

# specify order for Time. In this case with 15 min intervals, the 25th position
# for alphabetically sorted time is 06:00:00
Time.order <- sort(as.character(intraday_txt_file$Time))[c(25:96, 1:24)]

# specify label breaks for x-axis by changing some to blanks. "by = 12" is for 3-hour breaks,
# since 12 x 15min interval = 3 hours. for 2-hour breaks, use "by = 8", etc.
Time.breaks <- Time.order
Time.breaks[setdiff(seq(1, 96), seq(1, 96, by = 12))] <- ""

ggplot(data=intraday_txt_file %>% mutate(Time = factor(Time, levels = Time.order)), 
       aes(x=Time, y=INTA_Lots_mean)) +
  geom_point() + geom_line() +
  geom_errorbar(aes(ymin=INTA_Lots_mean-INTA_Lots_std,
                    ymax=INTA_Lots_mean+INTA_Lots_std), width=.1) +
  scale_x_discrete(labels = Time.breaks)

plot