R+ggplot:为Plot订购不规则的时间字符串
我有一个有两列的数据框。第一个是数值,另一个是描述时间的字符串。时间格式看起来像yyyy-mm-dd-hh-mm-ss-??????e、 g.2015-03-04-12-11-35-669696,我不知道最后6位是什么意思。例如R+ggplot:为Plot订购不规则的时间字符串,r,string,time,ggplot2,format,R,String,Time,Ggplot2,Format,我有一个有两列的数据框。第一个是数值,另一个是描述时间的字符串。时间格式看起来像yyyy-mm-dd-hh-mm-ss-??????e、 g.2015-03-04-12-11-35-669696,我不知道最后6位是什么意思。例如 y time 1 4.548 2014-08-11--09-07-44-202586 2 4.548 2014-08-11--09-07-54-442586 3 4.548 2014-08-11--09-
y time
1 4.548 2014-08-11--09-07-44-202586
2 4.548 2014-08-11--09-07-54-442586
3 4.548 2014-08-11--09-08-04-522586
4 4.478 2014-08-11--09-08-14-762586
5 4.431 2014-08-11--09-08-24-522586
6 4.446 2014-08-11--09-08-34-922586
7 4.492 2014-08-11--09-08-44-522586
8 4.508 2014-08-11--09-08-54-442586
9 4.486 2014-08-11--09-09-04-202586
10 4.497 2014-08-11--09-09-14-442586
11 4.461 2014-08-11--09-09-24-202586
我想用你的方式来策划
ggplot(df, aes(x=time, y=y)) + geom_line()
但我有一个问题,ggplot不知道如何处理类字符的数据,特别是我给定的时间格式。
我试图使用pacage{sfsmisc}中的AsciiToInt将字符串转换为数值,但它会为每个字符串重复一个整数列表,当然,每个字符对应一个数字。
我还可以从pakage{gtools}中使用mixedsort对时间字符串进行排序,但我不知道如何将其应用于绘图,同时还要记住距离
另一个问题是,我不希望每次字符串在x轴上都显示为勾号,因为我有大约20k行。也许我可以像中那样解决这个问题,但只要第一个问题出现,我就无法检查
你能帮我在x轴上用类似于数字的时间值绘制这样的数据吗?我将你的数据加载为一个名为time dat的.txt文件。首先,我将数据转换为POSIXct类型。为了使测试用图表更清晰,我省略了seconds字段,如果您想添加它们,只需使用注释掉的行即可
library(ggplot2)
timedat<-read.csv("~/Work/Timedat.csv")
timedat
str(timedat)
> str(timedat)
'data.frame': 11 obs. of 3 variables:
$ X : int 1 2 3 4 5 6 7 8 9 10 ...
$ y : num 4.55 4.55 4.55 4.48 4.43 ...
$ time: Factor w/ 11 levels "2014-08-11--09-07-44-202586",..: 1 2 3 4 5 6 7 8 9 10 ...
#timedat$time<-as.POSIXct(as.character(timedat$time),format = "%Y-%m-%d--%H-%M-%S")
timedat$time<-as.POSIXct(as.character(timedat$time),format = "%Y-%m-%d--%H-%M")
qplot(data=timedat,y=y,x=time)+theme_bw()
> timedat
X y time
1 1 4.548 2014-08-11--09-07-44-202586
2 2 4.548 2014-08-11--09-07-54-442586
3 3 4.548 2014-08-11--09-08-04-522586
4 4 4.478 2014-08-11--09-08-14-762586
5 5 4.431 2014-08-11--09-08-24-522586
6 6 4.446 2014-08-11--09-08-34-922586
7 7 4.492 2014-08-11--09-08-44-522586
8 8 4.508 2014-08-11--09-08-54-442586
9 9 4.486 2014-08-11--09-09-04-202586
10 10 4.497 2014-08-11--09-09-14-442586
11 11 4.461 2014-08-11--09-09-24-202586
这将生成以下日期顺序良好的绘图。
当我还想添加秒数时,我使用了格式=%Y-%m-%d-%H-%m-%S,但刻度只显示分钟数45,00,15,30,45,。。。。所以我使用scale\x\u datetimebreaks=date\u breaks 30秒,labels=date\u格式%Y-%m-%d%H:%m:%S来解决这个问题。