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

时间:2017-11-20 09:58:21

标签: r plot ggplot2 facet-wrap

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

这是我在立即绘制数据时获得的情节:

enter image description here

在此图中,ETA是点,ATA是垂直线。您可以看到所有估计值都略高于实际时间(即有延迟),但由于图表的可怕规模,您不能说太多其他内容。

我尝试了一个小平面包装,但这会产生一个新问题。

enter image description here

此图表可以更容易地看到估算值之间的差异,但由于实际时间位于所有估算值之后,因此会从视图中丢失。 (轴标签也很难看,但我可以解决这个问题。)

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

我认为可行的方法是将图表的每个方面的x限制扩展一定量。如果你愿意,可以选择一种“填充”。例如,我可以说“在每个方面前后两天添加”。但是,我找不到实现这一目标的方法。任何关于如何做到这一点的帮助(或其他可视化这些数据的方法)都将非常感激。

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

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")

1 个答案:

答案 0 :(得分:2)

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

你快到了。很好的MWE工作。

您要找的是expand的{​​{1}}参数:

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

scale_x_*有两个值,一个乘数和一个加法。乘数在两个方向上将轴扩展该量,此处为5%。然后在顶部添加添加物。你想要在两侧添加2天,所以我添加了2 x 24 x 3600.对于expand,它似乎可以在几秒钟内完成。