+ - 0:00:00
Notes for current slide
Notes for next slide

More that You Can Do With Scales

Dates, log, labels, breaks, color palettes

Peter Higgins

2021-10-30

1 / 3

Customizing Plot Scales

  • Date scales can be tricky
  • you can use scale_(x|y)_date()
  • then you can set date_breaks to an interval
  • and format the dates with date_labels. These have specific codes
2 / 3

Demonstration 1: Select Variables, then Violin Plot

mdeaths
# A tibble: 72 × 5
deaths month year day date
<dbl> <int> <dbl> <dbl> <date>
1 2134 1 1974 1 1974-01-01
2 1863 2 1974 1 1974-02-01
3 1877 3 1974 1 1974-03-01
4 1877 4 1974 1 1974-04-01
5 1492 5 1974 1 1974-05-01
6 1249 6 1974 1 1974-06-01
7 1280 7 1974 1 1974-07-01
8 1131 8 1974 1 1974-08-01
9 1209 9 1974 1 1974-09-01
10 1492 10 1974 1 1974-10-01
# … with 62 more rows
2 / 3

Demonstration 1: Select Variables, then Violin Plot

mdeaths %>%
ggplot(aes(x = date, y = deaths))

2 / 3

Demonstration 1: Select Variables, then Violin Plot

mdeaths %>%
ggplot(aes(x = date, y = deaths)) +
geom_line()

2 / 3

Demonstration 1: Select Variables, then Violin Plot

mdeaths %>%
ggplot(aes(x = date, y = deaths)) +
geom_line() +
theme_linedraw(base_size = 14)

2 / 3

Demonstration 1: Select Variables, then Violin Plot

mdeaths %>%
ggplot(aes(x = date, y = deaths)) +
geom_line() +
theme_linedraw(base_size = 14) +
scale_x_date(date_breaks = "6 months")

2 / 3

Demonstration 1: Select Variables, then Violin Plot

mdeaths %>%
ggplot(aes(x = date, y = deaths)) +
geom_line() +
theme_linedraw(base_size = 14) +
scale_x_date(date_breaks = "6 months") +
scale_x_date(date_labels = '%b\n%Y')

2 / 3

Date Formatting Codes

Format Example Unit Code
Mon abbreviated day text %a
Monday full day text %A
09 digit day of month %d
3 (0-6) digit day of week (Sunday = 0) %w
24 (0-53) digit week of year (0-53) - Sunday start %U
44 (0-53) digit week of year (0-53) - Monday start %W
Oct Abbreviated Month %b
October Full Month %B
10 Digit Month %m
12 2-Digit Year %y
2012 4-Digit Year %Y
3 / 3

Customizing Plot Scales

  • Date scales can be tricky
  • you can use scale_(x|y)_date()
  • then you can set date_breaks to an interval
  • and format the dates with date_labels. These have specific codes
2 / 3
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow