简体   繁体   English

R 每小时数据的日期索引

[英]date index on R hourly data

I have a data frame with hourly data and I am trying to plot but I want date on x axis not just index as numbers, I want the dates to be shown on x axis我有一个包含每小时数据的数据框,我正在尝试绘图,但我希望 x 轴上的日期不仅仅是数字索引,我希望日期显示在 x 轴上

df = read.csv('~/Desktop/data.csv', header = TRUE , stringsAsFactors = FALSE)
df$Date <- as.POSIXct(strptime(df$Date,format= "%Y-%m-%d %H:%M:%OS"))
plot(df$column1)

索引日期

The problem with your code seems to be that you feed a single variable into the plot command whereas what you want is a plot of column1 grouped by dates, right?您的代码的问题似乎是您将单个变量输入到plot命令中,而您想要的是日期分组column1绘图,对吗? If that's the case then you could use this:如果是这种情况,那么你可以使用这个:

DATA :数据

set-seed(123)
df <- data.frame(
  dates = rep(c("2009-01-01", "2009-01-02", "2009-01-03", "2009-01-04", "2009-01-05", "2009-01-06", "2009-01-07"), 1000),
  column1 = rnorm(7000, 100)
)

SOLUTION :解决方案

Then you could use ggplot2 and geom_jitter to plot column1 against dates :然后你可以使用ggplot2geom_jitter根据dates绘制column1

library(ggplot2)
ggplot(data=df, aes(x=dates, y=column1)) + geom_jitter() 

RESULT :结果

The resulting plot would roughly look like this:结果图大致如下所示: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM