How to Use scale_x_date to Format Dates in R


Often you may want to create a plot in R that uses dates along the x-axis.

The most cumbersome part of creating these types of plots is usually formatting the dates along the x-axis.

Fortunately, you can use the scale_x_date() function from the ggplot2 package to easily format dates in a variety of different ways.

You can add this function to the end of a ggplot2 plot in the following manner:

p + scale_x_date(date_labels = "%b %Y")

In this particular example, p represents a plot created in ggplot2 and we use the operators %b and %y to specify that the dates along the x-axis should be formatted using an abbreviated month name and 4-digit year.

Note that the scale_x_date function can take any of the following arguments:

  • %d: Day as a number between 0 and 31
  • %a: Abbreviated weekday (e.g. “Tue”)
  • %A: Unabbreviated weekday (e.g. “Tuesday”)
  • %m: Month between 0 and 12
  • %b: Abbreviated month (e.g. “Jan”)
  • %B: Unabbreviated month (e.g. “January”)
  • %y: 2-digit year (e.g. “21”)
  • %Y: 4-digit year (e.g. “2024”)
  • %W: Week of the year between 0 and 52

The following examples show how to use this function in practice in different scenarios with the following dataset in R that contains information about sales made on various dates at some store:

#make this example reproducible
set.seed(12)

#create dataset
df <- data.frame(date = as.Date("2024-01-01") - 0:99,
                 sales = runif(100, 10, 500) + seq(50, 149)^2)

#view head of dataset
head(df)

        date    sales
1 2024-01-01 2543.987
2 2023-12-31 3011.710
3 2023-12-30 3175.885
4 2023-12-29 2950.997
5 2023-12-28 3008.981
6 2023-12-27 3051.609

Example: How to Use scale_x_date to Format Dates in R

Suppose we use the following syntax to create a plot in ggplot2 that displays the date on the x-axis and sales on the y-axis:

library(ggplot2)

#create plot with date on x-axis and sales on y-axis
p <- ggplot(df, aes(x=date, y=sales)) +
       geom_line()

#display plot
p

By default, the names of the abbreviated months are shown along the x-axis.

To customize the dates shown on the x-axis, we can use the scale_x_date() function.

For example, we can use the following syntax to display the dates with abbreviated month names and 4-digit years:

p + scale_x_date(date_labels = "%b %Y")

This syntax produces the following plot:

scale_x_date in ggplot2 to format dates on x-axis in R

Notice the x-axis displays the dates with abbreviated month names and 4-digit years.

Or we could use the following syntax to display the dates with a typical day, month and year format:

p + scale_x_date(date_labels = "%d/%m/%Y")

This syntax produces the following plot:

The dates on the x-axis are now shown with a typical day, month and year format.

Note that you can also use the date_breaks argument to specify the frequency of breaks on the x-axis that should be shown.

For example, we could display the dates for every two weeks along the x-axis:

p + scale_x_date(date_breaks = "2 week")

This syntax produces the following plot:

scale_x_date ggplot2 with 2-week breaks along x-axis

Notice that the x-axis now displays the dates in two-week frequency intervals, just as we specified.

Note: You can find the complete documentation for the scale_x_date() function in ggplot2 here.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

A Complete Guide to the Best ggplot2 Themes
The Complete Guide to ggplot2 Titles
How to Create Side-by-Side Plots in ggplot2

Leave a Reply

Your email address will not be published. Required fields are marked *