f_month: Format Months to One Letter Abbreviation

Description Usage Arguments Value Examples

View source: R/f_month.R

Description

Format long month 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_month(x, ...)

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

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

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

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

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

ff_month(...)

f_month_name(x, ...)

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

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

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

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

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

ff_month_name(...)

f_month_abbreviation(x, ...)

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

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

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

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

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

ff_month_abbreviation(...)

Arguments

x

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

...

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
f_month(month.name)

f_month(1:12)

dates <- seq(as.Date("2000/1/1"), by = "month", length.out = 12)
f_month(dates)
## 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(
    month = sample(month.name, 1000, TRUE),
    area =  sample(LETTERS[1:5], 1000, TRUE)
) %>%
    count(month, area) %>%
    ungroup() %>%
    mutate(month = factor(month, levels = constant_months))

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

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

## End(Not run)

Example output

 [1] "J" "F" "M" "A" "M" "J" "J" "A" "S" "O" "N" "D"
 [1] "J" "F" "M" "A" "M" "J" "J" "A" "S" "O" "N" "D"
 [1] "J" "F" "M" "A" "M" "J" "J" "A" "S" "O" "N" "D"
Loading required package: pacman
# A tibble: 12 x 11
   date       year_4 year_2 quarter month_name month_abbreviat~ month_short
   <date>     <chr>  <chr>  <chr>   <fct>      <fct>            <chr>      
 1 2005-03-07 05     2005   Q1      March      Mar              M          
 2 1990-01-11 90     1990   Q1      January    Jan              J          
 3 2017-12-16 17     2017   Q4      December   Dec              D          
 4 1990-10-08 90     1990   Q4      October    Oct              O          
 5 1993-07-17 93     1993   Q3      July       Jul              J          
 6 2042-04-10 42     2042   Q2      April      Apr              A          
 7 1994-09-26 94     1994   Q3      September  Sep              S          
 8 2005-11-15 05     2005   Q4      November   Nov              N          
 9 2038-03-16 38     2038   Q1      March      Mar              M          
10 1996-09-29 96     1996   Q3      September  Sep              S          
11 1999-08-02 99     1999   Q3      August     Aug              A          
12 2014-02-14 14     2014   Q1      February   Feb              F          
# ... with 4 more variables: weekday_name <chr>, weekday_abbreviation <chr>,
#   weekday_short <chr>, weekday_short_distinct <chr>
Warning message:
`data_frame()` is deprecated, use `tibble()`.
This warning is displayed once per session. 

numform documentation built on Oct. 10, 2021, 1:10 a.m.