f_weekday: Format Weekdays to One Letter Abbreviation

Description Usage Arguments Value Examples

Description

Format long weekday name, integer, or date formats to a single capital letter. Useful for plot scales as a way to save space.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
f_weekday(x, distinct = FALSE, ...)

## Default S3 method:
f_weekday(x, distinct = FALSE, ...)

## S3 method for class 'numeric'
f_weekday(x, distinct = FALSE, ...)

## S3 method for class 'Date'
f_weekday(x, distinct = FALSE, ...)

## S3 method for class 'POSIXt'
f_weekday(x, distinct = FALSE, ...)

## S3 method for class 'hms'
f_weekday(x, distinct = FALSE, ...)

ff_weekday(distinct = FALSE, ...)

f_weekday_name(x, ...)

## Default S3 method:
f_weekday_name(x, ...)

## S3 method for class 'numeric'
f_weekday_name(x, ...)

## S3 method for class 'Date'
f_weekday_name(x, ...)

## S3 method for class 'POSIXt'
f_weekday_name(x, ...)

## S3 method for class 'hms'
f_weekday_name(x, ...)

ff_weekday_name(...)

f_weekday_abbreviation(x, ...)

## Default S3 method:
f_weekday_abbreviation(x, ...)

## S3 method for class 'numeric'
f_weekday_abbreviation(x, ...)

## S3 method for class 'Date'
f_weekday_abbreviation(x, ...)

## S3 method for class 'POSIXt'
f_weekday_abbreviation(x, ...)

## S3 method for class 'hms'
f_weekday_abbreviation(x, ...)

ff_weekday_abbreviation(...)

Arguments

x

A vector of weekday names, integers 1-12, or dates.

distinct

logical. If TRUE Sunday will be presented as Su and Thursday as Th.

...

ignored.

Value

Returns a single letter month abbreviation atomic vector.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
f_weekday(weekdays(x=as.Date(seq(7), origin="1950-01-07")))
f_weekday(weekdays(x=as.Date(seq(7), origin="1950-01-07")), TRUE)

f_weekday(1:7)
f_weekday(1:7, TRUE)

days <- seq(as.Date("2000/1/2"), by = "day", length.out = 7)
f_weekday(days)
f_weekday(days, TRUE)

## Not run: 
if (!require("pacman")) install.packages("pacman")
pacman::p_load(tidyverse)

set.seed(11)
data_frame(
    date = sample(seq(as.Date("1990/1/1"), by = "day", length.out = 2e4), 12)
) %>%
    mutate(
        year_4 = f_year(date, 2),
        year_2 = f_year(date, 4),
        quarter = f_quarter(date),
        month_name = f_month_name(date) %>%
            as_factor(),
        month_abbreviation = f_month_abbreviation(date) %>%
            as_factor(),
        month_short = f_month(date),
        weekday_name = f_weekday_name(date),
        weekday_abbreviation = f_weekday_abbreviation(date),
       weekday_short = f_weekday(date),
        weekday_short_distinct = f_weekday(date, distinct = TRUE)
    )


set.seed(10)
dat <- data_frame(
    day = sample(weekdays(days), 10000, TRUE),
    area =  sample(LETTERS[1:15], 10000, TRUE)
) %>%
    count(day, area) %>%
    ungroup() %>%
    mutate(
        day = factor(day, levels = weekdays(days))
    )

## without date formatting
ggplot(dat, aes(day, n)) +
    geom_bar(stat = 'identity') +
    facet_wrap(~area)

## with date formatting
ggplot(dat, aes(day, n)) +
    geom_bar(stat = 'identity') +
    facet_wrap(~area) +
    scale_x_discrete(labels = f_weekday)

## with date formatting
ggplot(dat, aes(day, n)) +
    geom_bar(stat = 'identity') +
    facet_wrap(~area) +
    scale_x_discrete(labels = ff_weekday(distinct = TRUE))

## End(Not run)

numform documentation built on May 2, 2019, 6:31 a.m.