Plotting data against time/date on the x-axis in ggplot2

Im trying to plot a value column against a time column. So far my code looks like this:

ggplot(echo120_edit, aes(y= PRC_ABC, x =Time_M)) + geom_smooth(method = loess, se = TRUE, fullrange= TRUE) +
scale_x_continuous(breaks = seq(0, 23, 1), labels = paste(seq(0, 23, 1), "00", sep = ":")) +
geom_point() + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_rect(fill = "white", colour = NA))

The time column has the time the variable was collected (its area backscattering coefficient data btw). I can plot on against the other, but the x-axis looks like a mess and I cant get the geom_smooth lines to appear when I do that. Essentially, I'm stuck trying to make the x-axis (time) have a repeating 24 hour clock with date marks with the loess line and varibale data plotted against it.

What kind of format is the time data in? If it's a time or a datetime, it should automatically use a reasonable time labeling convention.

Are you trying to plot multiple days worth of data (and smoothed data) on the same graph? Or is it one 24-hour section?

When this happens to me, I can usually debug it with one or both of two fixes:

  1. Converting the column with the date/time information to the right class (e.g., from string to date).

  2. Using scale_x_date() or scale_x_datetime() in my call to ggplot().

The nice thing with the latter is that it also affords you finer control over the appearance of that axis, e.g. different breaks or nicer labels.

the date and time were in 2 columns but I combined them. It was 24 hour periods over ~2 days.

I ultimately managed to convert them to POSIXct and used a cbind function to put it into the data. So far it seems to have worked but needs tweeking.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.