【发布时间】:2017-05-19 08:45:34
【问题描述】:
我有以下数据框
df
Time Load1 Load2
0 375.5 375.5
10 374.6 374.6
20 350.1 350.1
30 334.5 334.5
40 304.9 347.7
50 285.4 331.6
.... .... .. ...
我无法将时间列的格式从 10,20 更改为:00:10, 00:20...
我想通过这个命令使用 ggplot 绘制这个数据框:
P<- ggplot(df,aes(Time))+
geom_line(aes (y=0))+
geom_vline(xintercept = 0,)+
geom_line(aes(y=Load2,linetype = "twodash"))+
geom_line(aes(y=Load1, linetype = "solid"))+
theme(legend.position='none')+ # to remove the legend.
geom_vline(xintercept = 650, colour="purple",linetype ="longdash")+
geom_vline(xintercept = 1500, colour="purple",linetype ="longdash")+
geom_vline(xintercept = 2200, colour="purple",linetype ="longdash")+
labs(title = "Daily load demands", x = "Time of day", y = "Power
consumption_Kw/h")
P
但是,目前的结果是:
我尝试了以下命令来更改时间格式:
df<- data.frame(Time,Load1,Load2)
df$Time <- as.POSIXct(df$Time, format="%H:%M:%S")
P<- ggplot(df,aes(df$Time))+...
但是没有变化。请问,是否有任何建议以 05:00、10:00、15:00、20:00 格式而不是 500、1000、1500、2000 格式绘制 x 轴。非常感谢。
【问题讨论】:
-
只是为了理解。如果在您的数据框中您有
10那将是10:00?因为我在您的数据框中没有看到 10,20 之类的东西... 编辑:哦,好的,我现在明白了。10只需要 10 分钟吧? -
感谢 fasttouch 的回答。首先抱歉不清楚的问题。是的,“00:10”格式的 10 只需要 10 分钟,但是“10:00”格式的 1000 需要是上午 10 点。我想在图中显示出来。谢谢。
-
感谢@fasttouch 的重复:检查答案链接,我通过以下代码更改格式:temp2