如何在R中创建时间序列?

时间:2018-12-27 07:09:48

标签: r ggplot2

我想在R中创建时间序列,但是我遇到了问题。我有30.07的风速数据。至02.09。每小时的分辨率。在x轴上应该是日期和时间,在y轴上应该是风速。我尝试了此脚本,但不幸的是它没有用。谁能帮帮我吗?这是我的代码:

options(stringsAsFactors = FALSE)



 input1 <- "C:\\Users\\wind_speed.csv"

    wind_speed <- read.csv(input1, sep=";")

    dput(wind_speed)

    library(ggplot2)

    wind_speed$dateasdate <-gsub("\\.", "-", wind_speed$date)

    wind_speed$dateasdate <- dmy(wind_speed$dateasdate)

    wind_speed$date = as.Date(wind_speed$date, format = "%d.%m%.%y")

    time <- strptime(wind_speed$time, format = "%H:%M:%S")

    wind_speed$x <- paste(wind_speed$date,wind_speed$time)

    timee2<- strptime(wind_speed$x, format= "%d.%m.%y %H:%M")

    p <-ggplot(wind_speed, aes(x=x, y=speed)) + geom_point(stat="identity") + 
      geom_line(linetype="dashed") 

print(p)

[时间序列应该看起来像图像,但x轴上带有日期和时间] [2]

My data looks like this

这是我的数据示例:

head(wind_speed)
        date     time speed
1 27.07.2018 01:00:00   1.3
2 27.07.2018 02:00:00   0.8
3 27.07.2018 03:00:00   1.2
4 27.07.2018 04:00:00   0.6
5 27.07.2018 05:00:00   0.8
6 27.07.2018 06:00:00   1.8

1 个答案:

答案 0 :(得分:1)

使用-

library(xts)
a <- xts(df$speed, order.by=as.POSIXct(paste(df$date,df$time), format="%d.%m.%Y %H:%M:%S"))
plot.xts(a)

enter image description here