controlling time-scale & binwidth on x-axis in ggplot2

时间:2022-11-30 09:36:10

Here is a dput sample of the data I am working with:

这是我正在使用的数据的输入样本:

structure(list(time = structure(c(1426552275, 1426552184, 1426552085, 
1426551044, 1426550965, 1426550791, 1426550346, 1426549180, 1426549031, 
1426548975), class = c("POSIXct", "POSIXt"), tzone = "EST"), 
    location = c("South Africa,New York City", "Utah", "United States Of Africa", 
    "New York", "ATLANTA", "Atlanta, GA", "New York City!", "NYC via Chicago", 
    "Las Vegas, Nevada, USA", "Memphis TN"), uniqueid = c(5.77553e+17, 
    5.77552e+17, 5.77552e+17, 5.77548e+17, 5.77547e+17, 5.77547e+17, 
    5.77545e+17, 5.7754e+17, 5.77539e+17, 5.77539e+17)), .Names = c("time", 
"location", "uniqueid"), row.names = c(1L, 2L, 22L, 23L, 24L, 
27L, 28L, 29L, 30L, 31L), class = "data.frame")

When I plot this data with time on x-axis I get:

当我在x轴上用时间绘制这些数据时,我得到:

ggplot(data = temp, aes(x = time)) + 
  geom_bar() + 
  scale_x_datetime("time") + 
  scale_y_continuous("frequency")

controlling time-scale & binwidth on x-axis in ggplot2

When I increase the data to a 100 rows from my original dataset, I get:

当我将数据从原始数据集增加到100行时,我得到:

controlling time-scale & binwidth on x-axis in ggplot2

Because I am not specifying binwidth, I get the message :

因为我没有指定binwidth,所以收到消息:

stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
Warning message:
position_stack requires constant width: output may be incorrect 

I am not able to understand this. In the charts, how much time does each bar represent? By looking at the x-axis, I can make some guess - like the first graph each bar is 1.4mins and in the second graph probably 40 mins. But how should I calculate the value to specify in the binwidth argument?

我无法理解这一点。在图表中,每个条形图表示多长时间?通过观察x轴,我可以做出一些猜测 - 就像每个条形图的第一个图形是1.4分钟,而第二个图形可能是40分钟。但是我该如何计算要在binwidth参数中指定的值?

1 个解决方案

#1


1  

Copying @Gregor's comment into an answer:

将@ Gregor的评论复制到答案中:

Posix times (POSIXct) are stored in seconds, so specify a binwidth for a time duration that you want, e.g., binwidth = 5 * 60

Posix时间(POSIXct)以秒为单位存储,因此请指定所需持续时间的binwidth,例如binwidth = 5 * 60

#1


1  

Copying @Gregor's comment into an answer:

将@ Gregor的评论复制到答案中:

Posix times (POSIXct) are stored in seconds, so specify a binwidth for a time duration that you want, e.g., binwidth = 5 * 60

Posix时间(POSIXct)以秒为单位存储,因此请指定所需持续时间的binwidth,例如binwidth = 5 * 60