R图指定时间刻度线的数量 - 时间/日期相当于漂亮
我想知道在 x 轴上绘制时间时如何绘制更多刻度线。
基本上,相当于漂亮的时间。很明显,它不能很好地处理时间,因为它使用 1、2、5 和 10 的因子。对于时间,人们可能需要例如小时、半小时……
plot(as.POSIXct(x,origin="1960-01-01"),y,type="l",xlab="Time")
给出的刻度线确实太少且间隔很宽。
zoox<-zoo(y,as.POSIXct(stats$Time,origin="1960-01-01"))
plot(zoox)
给出相同的。
谢谢
编辑:
只是为了澄清(到目前为止答案没有解决我的问题):我正在寻找的是一个类似于日期的函数,例如一个函数,它需要开始日期、结束日期、刻度数,并输出刻度的位置。也就是说,我很清楚可以绘制小时,绘制分钟,以及其他什么,但是相当自动化数字的刻度距离,并且日期的结果函数应该自行决定是否使用天,小时,分钟,秒、毫秒、微秒、30 分钟、500 微秒、5 秒等间隔。无论如何,这就是 Pretty 对数字的作用。
EDIT2:
这是我当前用来决定时间轴格式的函数(请注意,这不适用于日期):
mydiff <- end-start
if(mydiff>1800) {
axis.POSIXct(1,xrange,format="%H:%M")
} else if(mydiff>30) {
axis.POSIXct(1,xrange,format="%H:%M:%S")
} else if(mydiff>0.5) {
axis.POSIXct(1,xrange,format="%H:%M:%OS3")
} else
axis.POSIXct(1,xrange,format="%H:%M:%OS6")
}
我没有增加刻度线的函数,所以我使用默认的刻度线数量
I was wondering how I could plot more tick marks when plotting time on the x-axis.
Basically, a time equivalent to pretty. Pretty obviously doesn't work so well with times, as it uses factors of 1,2,5 and 10. For time one probably wants e.g. hours, half hours, ...
plot(as.POSIXct(x,origin="1960-01-01"),y,type="l",xlab="Time")
gives really too few and widely spaced tickmarks.
zoox<-zoo(y,as.POSIXct(stats$Time,origin="1960-01-01"))
plot(zoox)
gives the same.
Thanks
EDIT:
Just to clarify (so far answers don't address my issue): What I am looking for is a function like pretty for dates, e.g. a function, that takes a start date, an end date, a number of ticks, and outputs the location of the ticks. That is, I am well aware it is possible to plot hours, to plot minutes, and what else, but pretty automates the tick distance for numbers, and a resulting function for dates should decide by itself whether to use days, hours, minutes, second, milliseconds, microseconds, 30 minutes, 500 micros, 5 seconds, etc. intervals. That is what pretty does for numbers, anyway.
EDIT2:
This is the function I currently use to decide the format for the time axis (note that this doesn't work for dates):
mydiff <- end-start
if(mydiff>1800) {
axis.POSIXct(1,xrange,format="%H:%M")
} else if(mydiff>30) {
axis.POSIXct(1,xrange,format="%H:%M:%S")
} else if(mydiff>0.5) {
axis.POSIXct(1,xrange,format="%H:%M:%OS3")
} else
axis.POSIXct(1,xrange,format="%H:%M:%OS6")
}
I don't have a function that increase tick marks, so I use the default number of tick marks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通过一个可重现的示例,
我们可以使用 axis.POSIXct() 函数(也可以使用 Axis() S3 通用函数)来添加自定义轴情节。这里的要点是,您(用户)可以完全控制刻度线的绘制位置以及它们的标签方式,如果默认值不适合您,您只需要多努力一点即可。
首先,我们绘制数据,但禁止绘制 x 轴:
接下来,我将在每小时开始时添加一个主要刻度线。为此,我创建了一个日期时间序列,
ceiling()
将我们移至下一小时),by = "1 hour"
)为单位递增序列。该序列被提供给
axis.POSIXct()
的at
参数。如果不阅读?axis.POSIXct
和?par
,其余部分应该很容易理解结果图如下所示:
为了显示更精细的控制,我现在在每个半小时位置添加小刻度线,但抑制这些刻度线的注释(通过
label
code> argument) 并让小刻度更短(通过图形参数tcl
)。请注意seq()
方法的by
参数如何获取指定间隔的数值现在的图如下所示:
您可以添加自己的标签,而不是
axis.POSIXct
函数提供的标签。如果您想这样做,那么我们应该将 seq() 的输出分配给一个对象,然后我们可以在该对象上使用 format() 函数。例如:结果图如下所示:
format()
返回格式化日期时间的字符串。您可以将paste()
粘贴到您想要的任何其他内容上,或者查看可用于格式化?strftime
中的日期时间对象的其他占位符Using a reproducible example
we can make use of the
axis.POSIXct()
function (one could also use theAxis()
S3 generic as well) to add a custom axis to the plot. The main point here is that you, the user, are in full control of where ticks are draw and how they are labelled, you just need to work a little harder if the defaults don;t work for you.First we plot the data but suppress drawing of the x-axis:
Next I will add a major tick mark at the start of each hour. For this I create a sequence of datetimes that goes
ceiling()
to move us on to the next hour),by = "1 hour"
) units.This sequence is supplied to the the
at
argument ofaxis.POSIXct()
. The rest should be easy to follow if not read?axis.POSIXct
and?par
The resulting figure looks like this:
To show finer control, I now add minor tick marks at each half-hour location, but suppress the annotation of those ticks (via the
labels
argument) and also make the minor ticks shorter (via graphics parametertcl
). Notice how theseq()
method'sby
argument can take a numeric amount of the stated intervalThe plot now looks like this:
You can add your own labels rather than the ones that the
axis.POSIXct
function comes up with. If you want to do this, then we should assign the output fromseq()
to an object that we can then use theformat()
function on. For example:The resulting plot is shown below:
format()
returns a character string of the formatted datetime. You canpaste()
on anything else you want or look at the other placeholders that can be used to format datetime objects in?strftime
axis.POSIXct() 已经很难猜测轴的合适漂亮值,所以我将从破解它开始。目前,它在内部依赖于将
pretty()
应用于日期时间的某些函数。它使用pretty()
的默认值,因此您可以修改该函数以添加n
或min.n
参数,这会增加选择了漂亮的标记。将 axis.POSIXct() 复制到您自己的函数/文件(为其指定一个新名称)。在定义中添加
n
或min.n
参数,默认值可能比pretty()
函数使用的值更大。并将其传递给每个进行的pretty()
调用。尝试一下。如果它工作得相当好,那么您可以执行
fixInNamespace(axis.POSIXct)
对实际函数进行相同的更改,以便在调用它的所有绘图上使用它。PS 这是一个可能的 hack
与原始函数的差异可以在此处查看
axis.POSIXct()
already works quite hard to guess suitable pretty values for the axis, so I would start by hacking that. At the moment, it relies internally on usingpretty()
applied to the some function of the datetimes. It uses the defaults forpretty()
so you could hack the function to add ann
ormin.n
argument which would increase the number of pretty marks selected.Copy
axis.POSIXct()
to your own function/file (give it a new name). Add an
ormin.n
argument to the definition, probably with larger values as defaults than those used by thepretty()
function. And pass that to each of thepretty()
calls that is made.Try it out. If it works reasonably well, then you can do
fixInNamespace(axis.POSIXct)
to make the same changes to the actual function so it gets used on all plots for which it is called.P.S. Here is a possible hack
Differences to the original function can be seen here
我倾向于使用函数 axis.POSIXct 和/或函数 cut.POSIXt。假设您的日期向量是
time
,而您的值向量与x
相关:并且,更进一步,使用
cut.POSIXt
:I tend to use function
axis.POSIXct
and/or functioncut.POSIXt
. Let's say your vector of date istime
and your vector of value associatedx
:And, going further, with
cut.POSIXt
:抑制绘图中轴可用的默认值(axes = FALSE,或单个轴)并使用函数 axis 来详细说明您想要的轴(特别是 at 参数) )。请参阅
?轴
Suppress the defaults available for the axis in plot (
axes = FALSE
, or individual axis) and use the function axis to detail what you want for the axis (specifically theat
argument). see?axis