Lubridate and ggplot

1 Load packages

Show the code
library(tidyverse)
library(nycflights13)
library(ggh4x)
library(ggforce)
1
A collection of packages including the lubridate and ggplot2 packagse.
2
A package that includes the flights and airlines data used in this tutorial.

The nycflights13 package comes with a number of data sets including flights and airlines that will be used in this exercise.

2 Wrangle data

3 Data visualization

4 The lubridate package

The lubridate package in R simplifies working with date-times by providing intuitive and user-friendly functions. It is part of the tidyverse ecosystem and makes it easy to parse, manipulate, extract, and perform arithmetic operations on date-time data.

4.1 Lubridate primarily helps you:

  • Parse character or numeric date-time data into R date-time formats.

  • Manipulate dates and times through arithmetic and transformations.

  • Extract components like years, months, days, hours, minutes, and seconds.

  • Handle time zones effectively.

  • Perform interval and duration calculations seamlessly.

    4.2 Key Functions in Lubridate:

4.3 Parsing Functions:

  • ymd(), mdy(), dmy(), ydm(), myd(), dym() Parse character or numeric vectors into date objects, based on the order of year, month, and day.
  • ymd_hms(), ymd_hm(), mdy_hms(), dmy_hms(), etc. Parse date-time strings with specified order, returning POSIXct date-time objects.

4.4 Date-time Component Extraction:

  • year() Extracts the year from a date or date-time.
  • month() Extracts the month (numeric by default) from a date or date-time.
  • day() Extracts the day of the month.
  • wday() Extracts the weekday (numeric or label).
  • qday() Extracts the day of the quarter.
  • yday() Extracts the day of the year.
  • hour(), minute(), second() Extract hour, minute, or second from a date-time object.

4.5 Date-time Modification:

  • update() Changes individual components (year, month, day, hour, etc.) of a date-time object.
  • floor_date(), ceiling_date(), round_date() Round a date-time to the nearest specified unit (e.g., day, month, year).

4.6 Time Span Classes:

  • duration() Creates duration objects (exact number of seconds).
  • period() Creates period objects (human-readable units such as months and years).
  • interval() Defines intervals between two date-time points.

4.7 Arithmetic and Interval Operations:

  • %–% Creates an interval between two dates or times (e.g., start_date %–% end_date).
  • int_length() Computes the length of an interval in seconds.
  • int_start(), int_end() Extract start or end dates from intervals.
  • int_shift() Shifts an interval by a specified duration or period.
  • int_overlaps() Checks if intervals overlap.

4.8 Time Zones:

  • with_tz() Converts the time to a different time zone without changing clock time.
  • force_tz() Forces the current time to a new time zone, keeping the clock time fixed.
  • tz() Retrieves or assigns the time zone attribute of a date-time object.

4.9 Logical Checks and Helpers:

  • is.Date(), is.POSIXct(), is.POSIXlt() Check if an object is of the respective date-time class.
  • leap_year() Tests whether a given year is a leap year.
  • days_in_month() Returns the number of days in a given month.

4.10 Convenience Functions:

  • today() Returns the current date.
  • now() Returns the current date and time.
  • make_date(), make_datetime() Construct date or date-time objects directly from components.
  • stamp() Creates custom date-time formatting functions based on an example format.
  • pretty_dates() Generates visually appealing sequences of dates for plotting axes.