Want data with date and time to be plotted and formatted in a particular way

Load basic data with ymd_hm format for datetime and simple number for the value.

library(ggplot2)
library(lubridate)

dat <- read.csv("datetime_data.csv")

Look at the data and see the datetime is not considered a date format but is a factor.

str(dat)
## 'data.frame':    10 obs. of  2 variables:
##  $ datetime: Factor w/ 10 levels "2016-01-01 12:00",..: 1 2 3 4 5 6 7 8 9 10
##  $ value   : int  2 3 4 5 6 7 8 9 10 11

Convert the factors to characters and use ymd_hm to convert to time and look at how they are stored.

dat$DateTime <- ymd_hm(as.character(dat$datetime))
str(dat)
## 'data.frame':    10 obs. of  3 variables:
##  $ datetime: Factor w/ 10 levels "2016-01-01 12:00",..: 1 2 3 4 5 6 7 8 9 10
##  $ value   : int  2 3 4 5 6 7 8 9 10 11
##  $ DateTime: POSIXct, format: "2016-01-01 12:00:00" "2016-01-02 04:00:00" ...

Now plot the data

ggplot(dat, aes(x = DateTime, y = value)) + geom_point()

Now adjust the formatting of the datetime axis – note we have to use scale_*_datetime since lubridate converts the dates into POSIXct format. See: http://docs.ggplot2.org/current/scale_date.html.

ggplot(dat, aes(x = DateTime, y = value)) + geom_point() +
  scale_x_datetime(date_labels = "%B %d %H:%M")

Or a simpler format:

ggplot(dat, aes(x = DateTime, y = value)) + geom_point() +
  scale_x_datetime(date_labels = "%H:%M")

The code for the different datetime variables can be found in the strptime function: https://stat.ethz.ch/R-manual/R-devel/library/base/html/strptime.html.