Search code examples
ggplot2axis-labelsfacet-wrap

Using 2nd variable to label axis ticks in ggplot2 facets


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?


Solution

  • 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