Prepartions for introduction:

# Install and load packages

# install.packages(c("ggptt", "dplyr", "tidyr", "scales"))

library(ggptt)
library(dplyr)


# Clear possible settings
unset_ptt()

# data for examples
pdat <- economics %>% 
  select(date, psavert, uempmed) %>% 
  tidyr::gather(vars, values, -date)

ggptt will also load ggplot2.

PTT colours

PTT colours: ptt_pal()

scales::show_col(ptt_pal(6))

VNK: vnk_pal()

scales::show_col(vnk_pal(4))

PTT scales

scale_colour_ptt() and scale_fill_ptt()

p <- ggplot(pdat, aes(x = date, y = values, colour = vars)) + 
  geom_line()

p

p + scale_colour_ptt()

PTT theme

p + theme_ptt()

set_ptt() sets PTT theme and colours. unset_ptt() could be used to undo ggptt settings.

set_ptt()
p

Also theme_map() for maps.

"the"-theme elemets

p + the_legend_bot()

p + the_title_blank()

p + the_title_blank(c("x", "l"))

p + the_x45()

scale formaters

p + scale_x_date(labels = no_century)

# Percents
pdat2 <- pdat %>% mutate(values = values/100)
p %+% pdat2 + scale_y_continuous(labels = percent_comma)

# comma
p %+% pdat2 + scale_y_continuous(labels = deci_comma)

Extra geoms

Horizontal 0-line

p + geom_h0()

Share a legend

grid_arrange_shared_legend(p, p + geom_h0(), ncol = 2)

Add extra breaks to a continuous scale

p + scale_y_continuous(breaks = extra_breaks(17))

Additional functions

Saving shorcuts

Translation

p <- ggplot(mtcars, aes(x = wt, y=mpg)) + geom_point() + labs(title = "Test plot")
translation <- c("Test plot" = "Testikuvio", wt = "Paino", mpg = "Mailia per gallona")
p2 <- translate_plot(p, translation)

p
p2


pttry/ggptt documentation built on May 4, 2023, 2:48 p.m.