Set up

knitr::opts_chunk$set(echo = TRUE)

# this is for rendering custom fonts in markdown
# but it doesn;t appear to be working ...

ragg_png_wrapper = function(..., res = 150) {
  ragg::agg_png(..., res = res, units = "in")
}

knitr::opts_chunk$set(dev = ragg_png_wrapper)
library(tidyverse)

library(ggtext)     # improved text rendering for ggplot2
library(ggforce)    # extra functionality for ggplot2 inc. improved annotation
library(patchwork)  # composing layouts with multiple plots
library(ggdist)     # visualising uncertainty and distributions
library(readxl)     # for reading Excel files
library(lubridate)  # for dates
library(readxl)     # for reading excel files

# packages for using custom fonts
# library(systemfonts)
# library(textshaping)
library(ragg)

library(cjmr)       # my own package including default styling for plots

# set default theme for exploratory plots
theme_set(theme_light())

Source of info on common ggplot2 tasks

Useful code snippets

Changing the size of a legend - Cédric Scherer

guides(color = guide_colorbar(title.position = "top", 
                                title.hjust = .5, 
                                barwidth = unit(20, "lines"), 
                                barheight = unit(.5, "lines")))

Changing margins - Cédric Scherer

theme(plot.margin = margin(t = 25, r = 25, b = 10, l = 25)) # top, right, bottom, left

Adjusting axis

scale_y_continuous(breaks = seq(0,350, 50), # labels
                   limits = c(0, 350)) # extent of axis

Saving using custom fonts with ragg

ggplot(mtcars, aes(hp, mpg)) +
  geom_point() +
  labs(title = "22222222222222222") +
  theme(text = element_text(family = "Roboto"),
        plot.title = element_text(size = 50))

ggsave("filename.png", device = ragg::agg_png())

Colour palettes

Remember it is an iterative process

Import data


Clean data


Explore the data


Explore the context of the data

Think about the audience

| To think about | Notes | |-----------------------------------------------------------------------------------|-------| | Can you think of a group of people who might view the visualisations? | | | Can you think of a specific person who might view the visualizations? | | | What do want the audience to know or be able to after viewing the visualizations? | | | What expectations might the audience have about visualisations of this data? | |

Ask some questions to be answered by the visualizations

  1. ...

  2. ...

Summarize the story that the visualizations can help tell

Based on the questions above, the story might go something like this:

Create the visualisation/s

Visualization 1 (question n)

| Question | Answer (i.e. title) | Notes to consider in design, chart types etc. | |----------|---------------------|-----------------------------------------------| | | | |


Visualization x (question n)

| Question | Answer (i.e. title) | Notes to consider in design, chart types etc. | |----------|---------------------|-----------------------------------------------| | | | |


Visualization x (question n)

| Question | Answer (i.e. title) | Notes to consider in design, chart types etc. | |----------|---------------------|-----------------------------------------------| | | | |


Export the visualizations for editing

Some useful image sizes (mm unless stated)

| Purpose | Width | Height | |-------------------------|-------|--------| | Single plot for Twitter | | | | | | | | | | |

file_folder <- ""
file_name <- "holding_name"

ggsave(str_c(file_folder, file_name, ".svg"))

# if saving as a pdf use device = cairo_pdf in ggsave 

Import back in the vizualisation as an image

Consider how well the overall design works

Based on Flowing Data.

Think about each issue (in bold below) as a continuum rather than a check-box.

+-----------------------------------------------------+--------------------------------------------------------------+-------+ | Question to consider | Tips | Notes | +=====================================================+==============================================================+=======+ | Are the encodings explained? | Air on the side of too much explanation ... | | +-----------------------------------------------------+--------------------------------------------------------------+-------+ | Is the context explained? | Titles, subtitles | | | | | | | | Annotate | | | | | | | | Use colour to highlight | | +-----------------------------------------------------+--------------------------------------------------------------+-------+ | Does it have readability like text? | Think visual hierarchy (as when laying out text) | | +-----------------------------------------------------+--------------------------------------------------------------+-------+ | Do the aesthetics align with the topic/message? | Serious topic -> dark colours, straight-forward chart types | | +-----------------------------------------------------+--------------------------------------------------------------+-------+

Remember it is an iterative process



tbk03/cjmr documentation built on Dec. 23, 2021, 8:40 a.m.