4

I've been stuck on this all morning, but cannot figure it out.

Each row of my data comprises: timestamps, measurement, user.

If I plot my data as a whole, it works fine. But if I try to plot the data by date using facet_wrap, it keeps failing with the error: Error: scale_id must not be NA .

The answer might be completely obvious, but I am lacking the knowledge and keywords to find the answer on my own.

I've distilled down the simplest example that gives the error below.

Any input/advice greatly appreciated.

Reprex:

library(tidyverse)

cols <- cols(
  timestamp = col_datetime(format = ""),
  tps = col_double(),
  user = col_character()
)

df <- read_csv("
timestamp,              tps,    user  
2021-01-06 07:05:44,    10,    CatA
2021-01-06 09:05:44,    15,    CatA
2021-01-06 10:15:44,    10,    CatA
2021-01-06 14:05:44,    15,    CatA
2021-01-06 07:03:44,    11,    CatB
2021-01-06 09:01:44,    13,    CatB
2021-01-06 10:12:44,    2,     CatB
2021-01-06 16:05:44,    6,     CatB
2021-01-07 07:05:44,    2,     CatA
2021-01-07 09:05:44,    3,     CatA
2021-01-07 10:15:44,    6,     CatA
2021-01-07 14:05:44,    7,     CatA
2021-01-07 07:03:44,    9,     CatB
2021-01-07 09:01:44,    6,     CatB
2021-01-07 10:12:44,    4,     CatB
2021-01-07 16:05:44,    1,     CatB
", col_types = cols)

str(df)


#  tibble [16 x 4] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
#   $ timestamp: POSIXct[1:16], format: "2021-01-06 07:05:44" "2021-01-06 09:05:44" "2021-01-06 10:15:44" "2021-01-06 14:05:44" ...
#   $ tps      : num [1:16] 10 15 10 15 11 13 2 6 2 3 ...
#   $ user     : chr [1:16] "CatA" "CatA" "CatA" "CatA" ...
#   - attr(*, "spec")=
#    .. cols(
#    ..   timestamp = col_datetime(format = ""),
#    ..   tps = col_double(),
#    ..   user = col_character()
#    .. ) 


#I then create a date variable:

df <- df %>%
  mutate(date = floor_date(
    timestamp, 
    unit = "days", 
    week_start = getOption("lubridate.week.start", 7)) 
  )


# This is where it fails: 
ggplot(data=df, aes(x=timestamp, y=tps, color=user)) +
  geom_line() +
  scale_x_datetime() +
  facet_wrap( ~ date)


##########
# RESULT #
##########

#> ggplot(data=df, aes(x=timestamp, y=tps, color=user)) +
#+   geom_line() +
#+   scale_x_datetime() +
#+   facet_wrap( ~ date)
#Error: `scale_id` must not be `NA`

sparco1500
  • 187
  • 3
  • 15
  • 1
    This error can also occur when the a function instead of variable was used as faceting variables. – jan-glx Feb 14 '23 at 12:22

1 Answers1

8

Change the date column to Date class.

library(ggplot2)

ggplot(data=df, aes(x=timestamp, y=tps, color=user)) +
  geom_line() +
  scale_x_datetime() +
  facet_wrap( ~ as.Date(date), scales = 'free')

enter image description here

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • thankyou!! Can you clarify why `facet_wrap( ~ date)` is insufficient? What is it about POSIXct class that caused facet_wrap to fail unless it is converted to date ? – sparco1500 Feb 25 '21 at 03:21
  • 2
    Facets don't work as expected when the variable is of Posixct type. There is an issue reported here regarding that https://github.com/tidyverse/ggplot2/issues/4175 – Ronak Shah Feb 25 '21 at 03:29