增加默认轴标签R plot

时间:2022-01-18 14:59:28

I got a data frame with the repetitions of an action on one minute intervals (if it is 0 it jumps that minute) something like this:

我得到了一个数据帧,其中一分钟间隔重复一次动作(如果它是0则跳过那一分钟)这样的事情:

    timestamp          count
 2015-01-07 01:15:00     2
 2015-01-07 01:16:00     1
 2015-01-07 01:18:00     1
 2015-01-07 01:20:00     3
 2015-01-07 01:25:00     1
 2015-01-07 01:26:00     2

When I try to plot, it only used 5 labels on the plot (x axis) like this:

当我尝试绘图时,它只在图(x轴)上使用了5个标签,如下所示:

增加默认轴标签R plot

What I want is to simply increase this to 10 labels on the x axis.

我想要的是简单地将其增加到x轴上的10个标签。

This data is variable so I can't use axis at

这个数据是可变的,所以我不能使用轴

So, is there a way to tell R to set 10 labels instead of 5?

那么,有没有办法告诉R设置10个标签而不是5个?

This is the code I use, df is a data frame as show above

这是我使用的代码,df是如上所示的数据帧

 X <- zoo(df$count, order.by=as.POSIXct(as.character(df[,1])))
 plot(X, main="Repetitions", sub="", xlab="Time of the Action", ylab="Number of Actions")

1 个解决方案

#1


1  

You can use axis.POSIXct to add the axis and pretty to choose 10 tick marks for the at argument. Note that the plot function does not add the x axis (xaxt="n"). I also reduced the size of the labels with cex.axis=0.7 to make the 10 fit.

您可以使用axis.POSIXct添加轴,并且可以为at参数选择10个刻度线。请注意,绘图功能不添加x轴(xaxt =“n”)。我还用cex.axis = 0.7减小了标签的大小,使10合适。

library(zoo)
y.POSIXct <- ISOdatetime(2015, 01, 1:24, 1:24, 24, 24)
y <- zoo(rnorm(5), y.POSIXct)
plot(y, xaxt="n")
axis.POSIXct(1,index(y), at=pretty(index(y), n = 10), cex.axis=0.7)

增加默认轴标签R plot

#1


1  

You can use axis.POSIXct to add the axis and pretty to choose 10 tick marks for the at argument. Note that the plot function does not add the x axis (xaxt="n"). I also reduced the size of the labels with cex.axis=0.7 to make the 10 fit.

您可以使用axis.POSIXct添加轴,并且可以为at参数选择10个刻度线。请注意,绘图功能不添加x轴(xaxt =“n”)。我还用cex.axis = 0.7减小了标签的大小,使10合适。

library(zoo)
y.POSIXct <- ISOdatetime(2015, 01, 1:24, 1:24, 24, 24)
y <- zoo(rnorm(5), y.POSIXct)
plot(y, xaxt="n")
axis.POSIXct(1,index(y), at=pretty(index(y), n = 10), cex.axis=0.7)

增加默认轴标签R plot