带有 x 时间轴的 R 图:如何强制刻度标签为天? [英] R plot with an x time axis: how to force the ticks labels to be the days?

查看:44
本文介绍了带有 x 时间轴的 R 图:如何强制刻度标签为天?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 csv 格式的文件:

I have this file in csv format:

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.

时间戳不固定,我在 12 月有一天,在 1 月有两天关闭.

The timestamps are not regularly spaced, I have one day in december ant two close days in january.

我试过这个代码

df = read.csv("my_data.csv")
df$timestamp = strptime(df$timestamp, "%Y-%m-%dT%H:%M:%S")
plot(df$timestamp,df$pages)

我得到了一个图,在 x 轴的中间只有一个刻度,标签为Jan":这没有错,但我想要三个刻度,只有日期和月份.

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.

我试过了

plot(df$timestamp,df$pages,xaxt="n")
axis.Date(1,df$timestamp,"days")

但没有绘制 x 轴.任何想法?谢谢

but no x axis is plotted. Any idea? Thank you

推荐答案

我会 as.Date() 你的 timestamp 像这样:

I would as.Date() your timestamp like this:

df$timestamp = as.Date(strptime(df$timestamp, "%Y-%m-%dT%H:%M:%S"))

这样就可以了:

plot(df$timestamp,df$pages,xaxt="n")
axis.Date(1,at=df$timestamp,labels=format(df$timestamp,"%b-%d"),las=2)

这篇关于带有 x 时间轴的 R 图:如何强制刻度标签为天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆