我可以在 facet wrapped ggplot 中设置动态轴限制吗?

Can I set dynamic axis limits in facet wrapped ggplot?

我有一个 ETA 数据集和我想要绘制的实际到达时间,以便我可以将估计时间与实际时间进行比较。挑战在于 ETA 靠近 ATA,但 ATA 彼此相距很远。

这是我直接绘制数据时得到的图:

在此图中,ETA 是点,ATA 是垂直线。你可以看到所有的估计都略早于实际时间(即有延迟),但你不能说太多,因为图表的规模太可怕了。

我试过小面包裹,但这会产生一个新问题。

此图可以更容易地看出估计值之间的差异,但由于实际时间位于所有估计值之后,因此从视图中看不到。 (轴标签也很难看,但我可以解决)。

最后一个复杂因素:我有很多这样的数据集,我想动态地绘制它们。我想我可以将 x 限制硬编码到图中,但是当我引入新数据集时,该图将完全无用。因此,将 x 范围硬编码到图中的解决方案不会有帮助。

我认为可行的方法是将图表每个方面的 x 限制扩展一定数量。一种 'padding',如果你愿意的话。例如,我可以说 "add two days before and after in every facet"。但是,我找不到实现这一目标的方法。任何有关如何做到这一点(或其他可视化此数据的方法)的帮助将不胜感激。

下面是生成数据和图表的代码。

library(ggplot2)

etas <- structure(list(eta = structure(c(1497391200, 1497391200, 1497391200, 
1497391200, 1497391200, 1497391200, 1497391200, 1497391200, 1497391200, 
1497391200, 1497391200, 1497391200, 1502229600, 1502298000, 1497391200, 
1497438000, 1502229600, 1502193600, 1502229600, 1497391200, 1497333600, 
1502229600, 1502229600, 1502229600, 1502229600, 1502229600, 1497391200, 
1497391200, 1502229600, 1502229600, 1502229600, 1497438000, 1502229600, 
1502229600, 1502229600, 1502276400, 1502229600, 1497391200, 1497391200, 
1502208000, 1502229600, 1502229600, 1497391200, 1502229600, 1502229600, 
1497391200, 1497391200, 1497391200, 1502283600, 1497391200, 1497438000, 
1502229600, 1497391200, 1497391200, 1502283600, 1502229600, 1502229600, 
1497391200, 1502283600, 1502229600, 1502229600, 1502229600, 1497391200, 
1497391200, 1497391200, 1497438000, 1497391200, 1497391200, 1502276400, 
1497438000, 1497391200, 1497438000, 1502229600, 1502229600, 1497391200, 
1497391200, 1497438000, 1502283600, 1502229600, 1502229600, 1502229600, 
1497391200, 1502229600, 1497391200, 1502229600, 1497438000, 1497348000, 
1502283600, 1497391200, 1497391200, 1502283600, 1497438000, 1497391200, 
1502229600, 1502229600, 1502229600, 1497391200, 1502229600, 1502229600, 
1502283600, 1497391200, 1497391200, 1502229600, 1502233200, 1497391200, 
1497376800, 1502229600, 1497391200, 1497391200, 1502229600, 1502229600, 
1497391200, 1502229600, 1497391200, 1497391200, 1502229600, 1502229600, 
1502229600, 1502229600, 1497391200, 1502229600, 1502229600, 1497391200, 
1502283600, 1497438000, 1502229600, 1497391200, 1502229600, 1502229600, 
1502229600, 1502229600, 1497391200, 1502229600, 1497391200, 1502229600, 
1502229600, 1502229600, 1502229600, 1502229600, 1502276400, 1502229600, 
1502298000, 1502283600, 1502276400, 1502229600, 1502229600, 1502229600, 
1502229600), class = c("POSIXct", "POSIXt"), tzone = ""), travel_time = c(32.4625, 
32.4625, 32.4625, 32.4625, 32.4625, 32.4625, 32.4625, 32.4625, 
32, 32, 32, 32, 34.5416666666667, 34.5416666666667, 23, 23, 23, 
23, 23, 23, 23, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 
21, 21, 21, 21, 21, 21, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 27, 27, 27, 27, 
27, 27, 27), id = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 
1, 1, 2, 2, 2, 1, 1, 2, 2, 2, 2, 2, 1, 1, 2, 2, 2, 1, 2, 2, 2, 
2, 2, 1, 1, 2, 2, 2, 1, 2, 2, 1, 1, 1, 2, 1, 1, 2, 1, 1, 2, 2, 
2, 1, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 2, 1, 1, 1, 
2, 2, 2, 2, 1, 2, 1, 2, 1, 1, 2, 1, 1, 2, 1, 1, 2, 2, 2, 1, 2, 
2, 2, 1, 1, 2, 2, 1, 1, 2, 1, 1, 2, 2, 1, 2, 1, 1, 2, 2, 2, 2, 
1, 2, 2, 1, 2, 1, 2, 1, 2, 2, 2, 2, 1, 2, 1, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2)), row.names = 157697:157844, class = "data.frame", .Names = c("eta", 
"travel_time", "id"))

atas <- structure(c(1502310387, 1497461116), class = c("POSIXct", "POSIXt"
), tzone = "Europe/Amsterdam")

plot <- ggplot(etas, aes(eta, travel_time, color = factor(id))) +
    geom_point() +
    geom_vline(xintercept = as.integer(atas))

plot
plot + facet_wrap(~ id, nrow = 1, scales = "free_x")

使用expand,A. Stam,使用A. Stam。

你快到了。在不错的 MWE 上做得很好。

您要查找的是 scale_x_*:

expand 参数

plot + facet_wrap(~ id, nrow = 1, scales = "free_x") + scale_x_datetime(expand=c(0.05,48*3600))

expand 有两个值,一个乘数和一个加法。乘数在两个方向上按该量扩展轴,此处为 5%。然后将添加物添加到顶部。你想在两边都增加 2 天,所以我增加了 2 x 24 x 3600。对于 scale_x_datetime,它似乎在几秒钟内有效。