[英]How can I plot Posix data hourly in ggplot2?
I have a dataframe:我有一个 dataframe:
bvar time
0.000000000 2003-03-14 19:00:00
0.200000000 2003-03-14 20:00:00
0.044000000 2003-03-14 21:00:00
Here, time is POSIXct:在这里,时间是 POSIXct:
str(tsdat$time)
POSIXct[1:193], format: "2003-03-14 19:00:00"
When I plot it, I want to control the x-axis by showing every hour:当我 plot 它时,我想通过每小时显示一次来控制 x 轴:
ggplot(ts) +
geom_line(aes(x=time, y=bvar))+
theme(axis.text.x = element_text(angle = 0, hjust = 1))+
scale_x_date(labels=date_format("%Y %H:%M")) +
ylab('BVAR [mm]')
Error: Invalid input: date_trans works with objects of class Date only错误:输入无效:date_trans 仅适用于 class 日期的对象
How can I make this hourly?我怎样才能每小时做一次? In another question, they suggested using
as.Date
.在另一个问题中,他们建议使用
as.Date
。 But this doesn't work for me as my data is for 2 days only.但这对我不起作用,因为我的数据只有 2 天。
I think you can use scale_x_datetime
for POSIXct instead of scale_x_date
.我认为您可以将
scale_x_datetime
用于 POSIXct 而不是scale_x_date
。 To get hourly breaks on the xaxis, also add breaks = "1 hour"
.要在 x 轴上按小时休息,还要添加
breaks = "1 hour"
。
library(ggplot2)
library(scales)
ggplot(ts) +
geom_line(aes(x=time, y=bvar))+
theme(axis.text.x = element_text(angle = 0, hjust = 1))+
scale_x_datetime(labels=date_format("%Y %H:%M"), breaks = "1 hour") +
ylab('BVAR [mm]')
Output Output
Data数据
ts <- structure(list(bvar = c(0, 0.2, 0.044), time = structure(c(1047690000,
1047693600, 1047697200), class = c("POSIXct", "POSIXt"), tzone = "")), row.names = c(NA,
-3L), class = "data.frame")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.