在 ggplot2 中控制 x 轴上的时间刻度和 binwidth
controlling time-scale & binwidth on x-axis in ggplot2
这是我正在处理的数据的输出样本:
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")
当我在 x 轴上用时间绘制此数据时,我得到:
ggplot(data = temp, aes(x = time)) +
geom_bar() +
scale_x_datetime("time") +
scale_y_continuous("frequency")
当我将原始数据集中的数据增加到 100 行时,我得到:
因为我没有指定 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
我无法理解这一点。在图表中,每个条代表多少时间?通过查看 x 轴,我可以做出一些猜测 - 就像第一张图表中的每个柱子是 1.4 分钟,而在第二张图表中可能是 40 分钟。但是我应该如何计算在 binwidth 参数中指定的值呢?
正在将@Gregor 的评论复制到答案中:
Posix times (POSIXct
) are stored in seconds, so specify a binwidth for a time duration that you want, e.g., binwidth = 5 * 60
这是我正在处理的数据的输出样本:
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")
当我在 x 轴上用时间绘制此数据时,我得到:
ggplot(data = temp, aes(x = time)) +
geom_bar() +
scale_x_datetime("time") +
scale_y_continuous("frequency")
当我将原始数据集中的数据增加到 100 行时,我得到:
因为我没有指定 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
我无法理解这一点。在图表中,每个条代表多少时间?通过查看 x 轴,我可以做出一些猜测 - 就像第一张图表中的每个柱子是 1.4 分钟,而在第二张图表中可能是 40 分钟。但是我应该如何计算在 binwidth 参数中指定的值呢?
正在将@Gregor 的评论复制到答案中:
Posix times (
POSIXct
) are stored in seconds, so specify a binwidth for a time duration that you want, e.g.,binwidth = 5 * 60