【问题标题】:Points not aligned on x-axis of ggplot 1-column facet wrapggplot 1-column facet wrap的x轴上未对齐的点
【发布时间】:2021-07-26 18:38:20
【问题描述】:

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

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)

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

【问题讨论】:

  • 当数据间隔很近或相同时,可能是 geom_jitter 做不同的事情。因此,在 P1 中,它会在分隔值的一分钟内抖动它们,而在 P2 中,它会在相隔 11 小时的两个唯一值之间抖动。您可能需要手动应用抖动,然后使用geom_point
  • @RichardTelford 的评论是对我的查询最早的评论/答案,而且也很到位。

标签: r datetime ggplot2 facet-wrap posixlt


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多