4

I am attempting to make a faceted t-chart using ggplot2, where the x-axis is represented a sequence of events and the y-axis represents the number of days between those events. The x-axis should be labelled with the event date, but it is not a time series since the distance between x-axis ticks should be uniform regardless of the real time between events.

Adding a faceting layer has been confusing me. Here's some sample data:

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))

Ignoring the date of the event, I can produce this:

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

I have tried to use the labels parameter to scale_x_discrete without success:

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

g + scale_x_discrete(labels = xaxis.ticks)

But that's wrong in a way I can't describe, because it cuts off my tick labels altogether.

Because there is a 1-1 correspondence between EventNBR and EventDT by Group for this dataset, it seems like there should be an easy solution, but I can't figure it out. Am I overlooking something painfully easy?

f_lost
  • 67
  • 1
  • 4

1 Answers1

5

In general, this is a very problematic thing as mentioned here and there are several other topics on this.

But luckily in your case it is possible since you use scales='free_x'.

What you need to do is adding an unique index column like

df$id <- 1:nrow(df)

and afterwards you can overwrite these indexes with you column with correct labels.

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))

There might be easier solutions but this is working in your example.

Also, the labels seem to be gone since the x axis is numeric and not discrete. So using scale_x_continuous produces the correct labels.

EDIT:

So a full example looks like this

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))

and produces the following output:

result

drmariod
  • 11,106
  • 16
  • 64
  • 110
  • Ah, I spoke to soon on the solution to this. The EventDT column is not facetted, but just reproduced verbatim across the three facets, i.e., both Groups B and C are labelled with Group A's EventDTs. – f_lost Aug 08 '17 at 19:30
  • @f_lost Not in my case. I will add a picture and a full example. – drmariod Aug 09 '17 at 06:14
  • Thank you, this works for me now. I must have inadvertently changed something yesterday to get me the strange behavior. – f_lost Aug 09 '17 at 18:06