I have this file in csv format:
我有一个csv格式的文件:
timestamp,pages
2011-12-09T11:20:50.33,4
2012-01-23T17:44:02.71,132
2012-01-28T15:07:59.34,168
The first column is a timestamp, the second one is a page count. I need to plot the page count on the vertical axis and the timestamp on the horizontal axis.
第一列是时间戳,第二个列是页面计数。我需要在垂直轴上绘制页数,在横轴上标出时间戳。
The timestamps are not regularly spaced, I have one day in december ant two close days in january.
时间戳不是有规律的间隔,我在12月有一天,在1月份的两天。
I tried this code
我试着这段代码
df = read.csv("my_data.csv")
df$timestamp = strptime(df$timestamp, "%Y-%m-%dT%H:%M:%S")
plot(df$timestamp,df$pages)
and I got a plot with just one tick on the middle of the x axis and with the label "Jan": it's not wrong but I would like to have three ticks with just the day number and the month.
我在x轴的中间画了一个勾,标上“Jan”:这没错,但我想要有3个节拍,只有当天和月号。
I tried
我试着
plot(df$timestamp,df$pages,xaxt="n")
axis.Date(1,df$timestamp,"days")
but no x axis is plotted. Any idea? Thank you
但是没有x轴。任何想法?谢谢你!
3 个解决方案
#1
12
I would as.Date()
your timestamp
like this:
你的时间戳是这样的:
df$timestamp = as.Date(strptime(df$timestamp, "%Y-%m-%dT%H:%M:%S"))
This works then:
这个作品:
plot(df$timestamp,df$pages,xaxt="n")
axis.Date(1,at=df$timestamp,labels=format(df$timestamp,"%b-%d"),las=2)
#2
10
This will work:
这将工作:
plot(df$timestamp,df$pages,xaxt="n")
axis.POSIXct(1, at=df$timestamp, labels=format(df$timestamp, "%m/%d"))
Essentially in axis.POSIXct
(note that you have POSIXct
dates in your data frame) you specify where to have the axis ticks (at
) and what the labels are.
基本上在轴。POSIXct(注意,您的数据帧中有POSIXct日期),您指定了在哪里有轴刻度(at)和标签是什么。
Typically I like my dates label vertical rather than horizontal. To get that use par(las=2)
before the plot.
通常我喜欢我的日期标签是垂直的而不是水平的。在情节之前使用par(las=2)。
#3
5
I found this: http://personality-project.org/r/r.plottingdates.html
我发现这:http://personality-project.org/r/r.plottingdates.html
Which gave me my solution...
这给了我解决的办法……
dm = read.csv("my_data.csv", sep=",", head=TRUE)
dm$DateTime <- as.POSIXct(dm$timestamp, format="%Y-%m-%dT%H:%M:%S")
daterange=c(as.POSIXlt(min(dm$DateTime)), as.POSIXlt(max(dm$DateTime)))
plot(pages ~ DateTime, dm, xaxt = "n")
axis.POSIXct(1, at=seq(daterange[1], daterange[2], by="day"), format="%b %d")
The important parts being daterange
and at=seq(..., by="day")
.
重要的部分是daterange和at=seq(…=“天”)。
#1
12
I would as.Date()
your timestamp
like this:
你的时间戳是这样的:
df$timestamp = as.Date(strptime(df$timestamp, "%Y-%m-%dT%H:%M:%S"))
This works then:
这个作品:
plot(df$timestamp,df$pages,xaxt="n")
axis.Date(1,at=df$timestamp,labels=format(df$timestamp,"%b-%d"),las=2)
#2
10
This will work:
这将工作:
plot(df$timestamp,df$pages,xaxt="n")
axis.POSIXct(1, at=df$timestamp, labels=format(df$timestamp, "%m/%d"))
Essentially in axis.POSIXct
(note that you have POSIXct
dates in your data frame) you specify where to have the axis ticks (at
) and what the labels are.
基本上在轴。POSIXct(注意,您的数据帧中有POSIXct日期),您指定了在哪里有轴刻度(at)和标签是什么。
Typically I like my dates label vertical rather than horizontal. To get that use par(las=2)
before the plot.
通常我喜欢我的日期标签是垂直的而不是水平的。在情节之前使用par(las=2)。
#3
5
I found this: http://personality-project.org/r/r.plottingdates.html
我发现这:http://personality-project.org/r/r.plottingdates.html
Which gave me my solution...
这给了我解决的办法……
dm = read.csv("my_data.csv", sep=",", head=TRUE)
dm$DateTime <- as.POSIXct(dm$timestamp, format="%Y-%m-%dT%H:%M:%S")
daterange=c(as.POSIXlt(min(dm$DateTime)), as.POSIXlt(max(dm$DateTime)))
plot(pages ~ DateTime, dm, xaxt = "n")
axis.POSIXct(1, at=seq(daterange[1], daterange[2], by="day"), format="%b %d")
The important parts being daterange
and at=seq(..., by="day")
.
重要的部分是daterange和at=seq(…=“天”)。