在某些ggplot绘图中时间轴值不正确,但在其他ggplot绘图中不正确 [英] Time Axis Values Incorrect in some ggplot plots but not others

查看:100
本文介绍了在某些ggplot绘图中时间轴值不正确,但在其他ggplot绘图中不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

论坛,

这是我的数据:

> data.cvg
Source: local data frame [938 x 5]

                  Date    Day                Time                 Parameter  Value
                (time) (fctr)              (time)                     (chr)  (dbl)
1  2016-03-05 01:35:03    Sat 2016-03-06 01:35:03 TerminalGarageUtilization 35.367
2  2016-03-05 01:40:01    Sat 2016-03-06 01:40:01 TerminalGarageUtilization 35.350
3  2016-03-05 01:43:18    Sat 2016-03-06 01:43:18 TerminalGarageUtilization 35.350
4  2016-03-05 01:45:01    Sat 2016-03-06 01:45:01 TerminalGarageUtilization 35.350
5  2016-03-05 01:50:02    Sat 2016-03-06 01:50:02 TerminalGarageUtilization 35.333
..                 ...    ...                 ...                       ...    ...

每5秒生成一个新的数据点.

A new datapoint is generated every 5 seconds.

如果使用此代码,则绘图将正确打印数据值以及时间轴.请注意,在撰写本文时,最后一个数据点位于美国东部标准时间3月7日上午1:50,以红线('Mon')标记.

If I use this code, the plot correctly prints the data values, as well as the time axis. Notice, at the time of writing this post, the last datapoint is at Mar 7th, 1:50AM EST, marked by the red line ('Mon').

ggplot(data.cvg)+geom_line(aes(x=Time,y=TerminalGarageUtilization,color=Day))

如果我尝试重新设置x轴格式:

If I attempt at re-formatting the x-axis:

 ggplot(data.cvg)+geom_line(aes(x=Time,y=TerminalGarageUtilization,color=Day))
+scale_x_datetime(date_labels = "%H:%M",date_breaks = '2 hours')

我得到了错误的x轴标签:

I get wrong x axis labels:

此图错误地将x轴标记为从5:00a到5:00a.注意相同的红线,但是如何将其标记为5a-7a.

This plot incorrectly labels the x-axis from 5:00a to 5:00a. Notice the same Red line, but how it's marked against 5a-7a.

这是为什么?

谢谢, 拉胡尔

推荐答案

已解决.

答案是还包括时区.正确的工作代码:

The answer was to also include the timezone. Correct working code:

library(scales)  


ggplot(data.cvg)+geom_line(aes(x=Time,y=TerminalGarageUtilization,color=Day))+
    scale_x_datetime(date_breaks = '2 hours',labels = date_format("%H:%M",tz = "EST"))

这篇关于在某些ggplot绘图中时间轴值不正确,但在其他ggplot绘图中不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆