-1

So I have this graph with the x-axis showing Julian dates across a single year. I would like to condense the x-axis a bit more and make it for legible, but I am unsure how. Maybe increasing the Julian date by increments of 2 or 3 days? I would like to prevent losing as much information from the graph as possible. So I converted a date and time column with the format `YYYY-MM-DD HH:MM:SS' to a POSIXct then I changed the format of the column to Julian Dates.

ind_steps$t2 <- format(as.POSIXct(ind_steps$t2),"%y%j") 

I had tried to turn the x-axis label by 90 degrees to see if that would make it more legible, but it didn't help very much.

    plot_list[[i]]  <- ggplot(ind_steps, aes(x = t2, y = NSD)) + 
      geom_line() + theme_bw() +
      ggtitle(random_tables[i]) +
      theme(axis.text.x = element_text(angle = 90)) +
  }

Thank you for your time.

enter image description here

John Huang
  • 845
  • 4
  • 15
  • 2
    Check if `t2` is of class date. Seems to be characters. Also share data and code to make your problem reproducible. – markus Apr 17 '21 at 22:12
  • 2
    You want to convert t2 to POSIXct, but do not format it. Then see Tim's answer below, where you can substitute in the desired format ("%y%j"). You do not want to format the t2 variable prior to plotting it. – Dave2e Apr 17 '21 at 23:14
  • 'Error: Invalid input: date_trans works with objects of class Date only' I get this error when I run the code. What might the issue be? – John Huang Apr 17 '21 at 23:40
  • 1
    You are trying to pass a datetime object to a function expecting a date. Use `scale_x_datetime` instead. – Dave2e Apr 18 '21 at 01:11

1 Answers1

2

I agree with @markus comment; please create a reproducible example (https://stackoverflow.com/help/minimal-reproducible-example).

However, the line of code below might help you. It assumes your date field is year-month-day; and labels the x-axis with a two year interval.

  scale_x_date(date_breaks = "2 year", date_labels = "%Y")
Tim Assal
  • 677
  • 6
  • 15