使用第二个变量来标记 ggplot2 方面的轴刻度

f_l*_*ost 4 ggplot2 facet-wrap axis-labels

我正在尝试使用 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))
Run Code Online (Sandbox Code Playgroud)

忽略事件的日期,我可以生成以下内容:

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

绘图应沿 X 轴显示 EventDT,而不是 EventNBR

我尝试使用 labels 参数来scale_x_discrete 但没有成功:

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

g + scale_x_discrete(labels = xaxis.ticks)
Run Code Online (Sandbox Code Playgroud)

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

因为该数据集的 EventNBR 和 EventDT by Group 之间存在 1-1 对应关系,所以似乎应该有一个简单的解决方案,但我无法弄清楚。我是否忽略了一些非常容易的事情?

drm*_*iod 5

一般来说,这是一个非常有问题的事情,正如这里提到的,还有其他几个主题。

但幸运的是,在你的情况下这是可能的,因为你使用了scales='free_x'.

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

df$id <- 1:nrow(df)
Run Code Online (Sandbox Code Playgroud)

然后您可以使用带有正确标签的列覆盖这些索引。

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))
Run Code Online (Sandbox Code Playgroud)

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

此外,标签似乎消失了,因为 x 轴是数字而不是离散的。因此使用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))
Run Code Online (Sandbox Code Playgroud)

并产生以下输出:

结果