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

问题描述

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

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-03 11:08:00"),as.POSIXlt("2021-05-03 11:08:00")
)

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

df1 <- data.frame(time1,grp,cnt)
df2 <- data.frame(time2,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,shrink = FALSE)

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

Plot p1

Plot p2

解决方法

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

jitter geom 是 geom_point(position = “抖动”)。它为位置添加了少量随机变化 每个点,并且是处理由 较小数据集中的离散性。

要获得确定性布局,您应该使用 geom_point,它为您提供 enter image description here