ggplot 1-column facet wrap 的 x 轴上未对齐的点

Points not aligned on x-axis of ggplot 1-column facet wrap

请检查并建议我如何解决以下脚本生成的两个图中的差异:

time1 <- c(
  as.POSIXlt("2021-05-02 23:57:29"),
  as.POSIXlt("2021-05-02 23:58:29"),
  as.POSIXlt("2021-05-02 23:59:29"),
  as.POSIXlt("2021-05-03 11:06:00"),
  as.POSIXlt("2021-05-03 11:07:00"),
  as.POSIXlt("2021-05-03 11:08:00")
)
time2 <- c(
  as.POSIXlt("2021-05-02 23:59:29"),
  as.POSIXlt("2021-05-02 23:59:29"),
  as.POSIXlt("2021-05-02 23:59:29"),
  as.POSIXlt("2021-05-03 11:08:00"),
  as.POSIXlt("2021-05-03 11:08:00"),
  as.POSIXlt("2021-05-03 11:08:00")
)

grp <- c("A","B","C","A","B","C")
cnt <- c(29,1,30,31,2,33)

df1 <- data.frame(time1, grp, cnt)
df2 <- data.frame(time2, grp, cnt)

p1 <- ggplot(df1, aes(x = time1, y = cnt, color = grp)) +
  geom_jitter(size = 1.0, show.legend = FALSE) +
  facet_wrap(~grp, ncol = 1, shrink = FALSE)

p2 <- ggplot(df2, aes(x = time2, y = cnt, color = grp)) +
  geom_jitter(size = 1.0, show.legend = FALSE) +
  facet_wrap(~grp, ncol = 1, shrink = FALSE)

plot p1 显示了按照它们的 time1 值对齐的点。在图 p2 中,点未对齐。

当您键入 ?geom_jitter 时,您会看到点位置会随机变化:

The jitter geom is a convenient shortcut for geom_point(position = "jitter"). It adds a small amount of random variation to the location of each point, and is a useful way of handling overplotting caused by discreteness in smaller datasets.

要有确定性的布局,你应该使用 geom_point,它给你