代码之家  ›  专栏  ›  技术社区  ›  f_lost

使用第二个变量标记ggplot2面中的轴记号

  •  3
  • f_lost  · 技术社区  · 7 年前

    我试图使用ggplot2制作一个多面t形图,其中x轴表示一系列事件,y轴表示这些事件之间的天数。x轴应标有事件日期,但它不是一个时间序列,因为无论事件之间的实时性如何,x轴刻度之间的距离都应该是一致的。

    添加刻面层让我感到困惑。以下是一些示例数据:

    df <- data.frame(EventDT = as.POSIXct(c("2014-11-22 07:41:00", "2015-02-24 08:10:00",
                                      "2015-06-10 13:54:00", "2015-07-11 02:43:00",
                                      "2015-08-31 19:08:00", "2014-11-18 14:06:00",
                                      "2015-06-09 23:10:00", "2016-02-29 07:55:00",
                                      "2016-05-22 04:30:00", "2016-05-25 21:46:00",
                                      "2014-12-22 16:19:00", "2015-05-13 16:38:00",
                                      "2015-06-01 09:05:00", "2016-02-21 02:30:00",
                                      "2016-05-13 01:36:00")),
                 EventNBR = rep(1:5, 3),
                 Group = c(rep('A', 5), rep('B',5), rep('C',5)),
                 Y = c(15.818750, 94.020139, 106.238889, 30.534028, 51.684028,
                       187.670139, 203.377778, 264.364583, 82.857639, 3.719444,
                       169.829861, 142.013194, 18.685417, 264.725694,81.962500))
    

    忽略事件日期,我可以得出以下结论:

    g <- ggplot(df, aes(x=EventNBR, y=Y)) +
      geom_point() +
      geom_line() + 
      facet_wrap(~ Group, scales='free_x')
    

    Plot should show EventDT along X-axis, not EventNBR

    我曾尝试使用labels参数缩放x_离散,但没有成功:

    xaxis.ticks <- function(x) {
        df[which(df$EventNBR) == x] }
    
    g + scale_x_discrete(labels = xaxis.ticks)
    

    但这在某种程度上是错误的,我无法描述,因为它完全切断了我的刻度标签。

    1 回复  |  直到 7 年前
        1
  •  4
  •   drmariod    7 年前

    总的来说,如前所述,这是一件非常有问题的事情 here 还有其他几个话题。

    scales='free_x'

    您需要做的是添加一个唯一的索引列,如

    df$id <- 1:nrow(df)
    

    然后,您可以用正确的标签覆盖这些索引。

    g <- ggplot(df, aes(x=id, y=Y)) +
        geom_point() +
        geom_line() + 
        facet_wrap(~ Group, scales='free_x')
    g + scale_x_continuous(breaks=df$id, labels=df$EventDT) +
        theme(axis.text.x=element_text(angle=90, vjust=.5))
    

    可能有更简单的解决方案,但这在您的示例中有效。

    scale_x_continuous 生成正确的标签。

    编辑:

    library(ggplot2)
    df <- data.frame(EventDT = as.POSIXct(c("2014-11-22 07:41:00", "2015-02-24 08:10:00",
                                            "2015-06-10 13:54:00", "2015-07-11 02:43:00",
                                            "2015-08-31 19:08:00", "2014-11-18 14:06:00",
                                            "2015-06-09 23:10:00", "2016-02-29 07:55:00",
                                            "2016-05-22 04:30:00", "2016-05-25 21:46:00",
                                            "2014-12-22 16:19:00", "2015-05-13 16:38:00",
                                            "2015-06-01 09:05:00", "2016-02-21 02:30:00",
                                            "2016-05-13 01:36:00")),
                     EventNBR = rep(1:5, 3),
                     Group = c(rep('A', 5), rep('B',5), rep('C',5)),
                     Y = c(15.818750, 94.020139, 106.238889, 30.534028, 51.684028,
                           187.670139, 203.377778, 264.364583, 82.857639, 3.719444,
                           169.829861, 142.013194, 18.685417, 264.725694,81.962500))
    
    df$id <- 1:nrow(df)
    
    g <- ggplot(df, aes(x=id, y=Y)) +
      geom_point() +
      geom_line() + 
      facet_wrap(~ Group, scales='free_x')
    g + scale_x_continuous(breaks=df$id, labels=df$EventDT) +
      theme(axis.text.x=element_text(angle=90, vjust=.5))
    

    并产生以下输出:

    result