0

I have a df and I have plotted two different line charts. Unfortunately, there are a lot of dates so everything is squeezed up in the x-axis. I would like to remove the dates or leave just 4 . Can anyone help?

dput(df)
structure(list(ID = c("F1", "F1", "F1", "F1", "F1", "F1", 
"F1", "F2", "F2", "F2", "F2", "F2", "F2", "F2", "F2", "F3", "F3", 
"F3", "F3", "F3", "F3", "F3", "F3", "F3", "F4", "F4", "F4", "F4", 
"F4", "F4", "F4", "F4"), Date = c("22/6/2021", "23/6/2021", "24/6/2021", 
"25/6/2021", "26/6/2021", "27/6/2021", "28/6/2021", "22/6/2021", 
"23/6/2021", "24/6/2021", "25/6/2021", "26/6/2021", "27/6/2021", 
"28/6/2021", "29/6/2021", "22/6/2021", "23/6/2021", "24/6/2021", 
"25/6/2021", "26/6/2021", "27/6/2021", "28/6/2021", "29/6/2021", 
"30/6/2021", "22/6/2021", "23/6/2021", "24/6/2021", "25/6/2021", 
"26/6/2021", "27/6/2021", "28/6/2021", "29/6/2021"), Values = c(9.6, 
9.8, 10.2, 9.8, 9.9, 9.9, 9.9, 1.2, 1.2, 1.8, 1.5, 1.5, 1.6, 
1.4, 1.1, 3266, 3256, 7044, 6868, 6556, 3405, 3410, 3980, 5567, 
59.4, 56, 52.8, 52.4, 55.5, 54, 61, 53.6), Bin = c(0L, 0L, 1L, 
0L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 
1L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L)), class = "data.frame", row.names = c(NA, 
-32L))
ggplot(data = df, aes(x = factor(Date), y = Values)) +       
  geom_line(aes(group = ID)) + geom_point() + 
  facet_wrap(~ID, scales = 'free')
ggplot(aes(x = Date, y = Values)) +       
             geom_line(aes(group = ID)) + geom_point(aes(color = factor(Bin) )) + 
              scale_x_discrete(labels="")+
             facet_wrap(~ID, scales = 'free')) 
pipts
  • 87
  • 7
  • You can drop x-axis labels by adding `+ theme(axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank())` to you plot. See https://stackoverflow.com/questions/35090883/remove-all-of-x-axis-labels-in-ggplot – Ronak Shah Aug 09 '21 at 14:57
  • 2
    I would suggest to convert your Date to date format using e.g. `df$Date <- as.Date(df$Date, "%d/%m/%Y")`. Doing so will lead ggplot2 to automatically choose more pretty breaks. And in case the defaults are not fine you could adjust them via e.g. `scale_x_date(date_breaks = "4 days")`. Another option would be to put the labels in two rows using e.g. `scale_x_date(guide = guide_axis(n.dodge = 2))` – stefan Aug 09 '21 at 15:20
  • Yep, convert to date object is the way to go – Cameron Sep 30 '22 at 10:49

1 Answers1

0

You have two options

1) Turn Date into a date object and only plot x many ticks

As mentioned by Stefan

df %>% 
  mutate(Date = as.Date(Date)) %>% 
  ggplot(aes(x = Date, y = Values)) +       
  geom_line(aes(group = ID)) + 
  geom_point() + 
  facet_wrap(~ID, scales = 'free') +
  scale_x_date(
    breaks = "12 months",
    labels = scales::label_date_short(
      format = c("%y", "%b"),
      sep = "\n"))

enter image description here

2) Remove all axis labels

df %>% 
  mutate(Date = as.Date(Date)) %>% 
  ggplot(aes(x = Date, y = Values)) +       
  geom_line(aes(group = ID)) + 
  geom_point() + 
  facet_wrap(~ID, scales = 'free') +
  theme(axis.text.x = element_blank())

enter image description here

Cameron
  • 226
  • 1
  • 12