R ggplot2 scale_x_datetime() – Time series graph x-axis control

A package called, scales, is very useful for controlling the x-axis on a time-series ggplot.We will mainly use date_breaks() and date_format() functions in “scales” package to control the time-axis.

Re: the input of date_breaks(), you can use one of the following interval specifications in place of “month”: “sec”, “min”, “hour”, “day”, “week”, “month”, “year”. Also, you can use any integer in front of the specification, like date_breaks(width = “5 month”) . Note: always have a space between an integer and a specification .

With respect to the input of date_format (), see the section on Details at http://stat.ethz.ch/R-manual/R-patched/library/base/html/strptime.html.   A detailed instruction of how to use these functions for a ggplot can be found at http://docs.ggplot2.org/0.9.3.1/scale_datetime.html.



# Sample Data
date_today <- Sys.Date( )
date_yesterday <- today - 1
timeInSecondsSinceEpoch_today <- as.numeric(as.POSIXct(date_today, TZ="gmt", origin="1970-01-01"))
timeInSecondsSinceEpoch_yesterday <- as.numeric(as.POSIXct(date_yesterday, TZ="gmt", origin="1970-01-01"))
gmt <- as.POSIXct(timeInSecondsSinceEpoch_yesterday:timeInSecondsSinceEpoch_today, tz="gmt", origin="1970-01-01")
data <- data.frame(gmt = gmt, value = 3)

# x limits for 3 different x-axis
time1_p1 <- strptime(paste(date_yesterday, "00:00:00"), "%Y-%m-%d %H:%M:%S")
time2_p1 <- strptime(paste(date_yesterday, "06:00:00"), "%Y-%m-%d %H:%M:%S")
xlim_p1 <- as.POSIXct(c(time1_p1, time2_p1), origin="1970-01-01", tz="GMT")

time1_p2 <- strptime(paste(date_yesterday, "00:00:00"), "%Y-%m-%d %H:%M:%S")
time2_p2 <- strptime(paste(date_yesterday, "12:00:00"), "%Y-%m-%d %H:%M:%S")
xlim_p2 <- as.POSIXct(c(time1_p2, time2_p2), origin="1970-01-01", tz="GMT")

time1_p3 <- strptime(paste(date_yesterday, "00:00:00"), "%Y-%m-%d %H:%M:%S")
time2_p3 <- strptime(paste(date_today, "00:00:00"), "%Y-%m-%d %H:%M:%S")
xlim_p3 <- as.POSIXct(c(time1_p3, time2_p3), origin="1970-01-01", tz="GMT")

# plotting
library(ggplot2)
library(scales)

label_x <- paste(date_yesterday, "to", date_today, "(gmt)")
p <- qplot(x=gmt, y=value, data=data) + xlab(label_x)

# 30-min break on the axis
p1 <- p + scale_x_datetime(breaks = date_breaks("30 min"), minor_breaks=date_breaks("15 min"), labels=date_format("%H:%M:%S"), limits=xlim_p1)

# 1-hour break on the axis
p2 <- p + scale_x_datetime(breaks = date_breaks("1 hour"), minor_breaks=date_breaks("30 min"), labels=date_format("%H:%M:%S"), limits=xlim_p2)

# 2-hour break on the axis</div>
p3 <- p + scale_x_datetime(breaks = date_breaks("2 hour"), minor_breaks=date_breaks("1 hour"), labels=date_format("%H:%M:%S"), limits=xlim_p3)

p1 (30-min breaks)

p1 (30-min breaks)

p2 (1-hour breaks)

p2 (1-hour breaks)

p3 (2-hour breaks)

p3 (2-hour breaks)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s