options(width = 150) knitr::opts_chunk$set( message = TRUE, fig.width = 11 ) library(tidyverse) library(gt) # tables library(here) # relative file paths library(patchwork) # combining ggplots library(glue) # combining strings and data # Set ggplot2 theme and defaults theme_set(cowplot::theme_cowplot() + cowplot::background_grid(major = "xy")) ggp <- function(...) ggplot(...) + scale_color_brewer(palette = "Set1") + scale_fill_brewer(palette = "Set1")
# Alternatively, use DGI Clinical themes and fonts library(dgitheme) # This command must be run once to register all fonts installed on Windows # extrafont::font_import() # This command must be run once in each R session to register fonts extrafont::loadfonts(device = "win") # DGI fonts theme_update(text = element_text(family = "Lato"), plot.title = element_text(family = "Bebas Neue", size = 25)) # DGI colors ggp <- function(...) ggplot(...) + scale_color_dgi() + scale_fill_dgi()
Below is a comparison of
(A) the default ggplot2
qualitative color palette,
(B) the "Set1" palette from scale_fill_brewer()
,
(C) the "main" palette from dgitheme::scale_fill_dgi()
, and
(D) the "triadic" palette from dgitheme::scale_fill_dgi()
.
Also demonstrates how to use the patchwork
package to arrange plots.
p1 <- ggplot(iris, aes(Sepal.Length, fill = Species)) + geom_density() + scale_y_continuous(NULL, expand = c(0, 0)) + labs(title = "Default ggplot2 palette") + theme(legend.position = c(0.7, 0.7)) p2 <- p1 + scale_fill_brewer(palette = "Set1") + labs(title = "Brewer Set1 palette") p3 <- p1 + scale_fill_dgi() + labs(title = "DGI Clinical main palette") p4 <- p1 + scale_fill_dgi("triadic") + labs(title = "DGI Clinical triadic palette") p1 + p2 + p3 + p4 + plot_layout(nrow = 2) + plot_annotation(tag_levels = "A")
When plotting ordered factors, some options include:
(A) the ggplot2
default virdis palette,
(B) the default sequential color palette ("Blues") from scale_fill_brewer()
,
(C) the "main" palette (reversed) from dgitheme::scale_fill_dgi()
, and
(D) the "teal grey" palette (reversed) from dgitheme::scale_fill_dgi()
.
p1 <- diamonds %>% ggplot(aes(clarity, fill = cut)) + geom_bar() + scale_y_continuous(expand = c(0, 0)) + labs(title = "Default ggplot2 palette") + theme(legend.position = c(0.7, 0.7)) p2 <- p1 + scale_fill_brewer() + labs(title = "Brewer Blues palette") p3 <- p1 + scale_fill_dgi(reverse = TRUE) + labs(title = "DGI Clinical main palette") p4 <- p1 + scale_fill_dgi("teal grey", reverse = TRUE) + labs(title = "DGI Clinical teal grey palette") p1 + p2 + p3 + p4 + plot_layout(nrow = 2) + plot_annotation(tag_levels = "A")
From the options(width = 150)
argument at setup, code output should fill the width of the page.
glimpse(mpg)
Also, printing of dataframes will default to a paged format to allow full exploration:
mpg
Text can be **bold** or *italic*.
Mathematical expressions can be rendered using LaTeX syntax: $\sigma^2 = \sum \frac{(x_i - \mu)^2}{N}$, or on a separate line as:
$$\chi^2 = \frac{(n-1) s^2}{\sigma^2}$$
Tables are best rendered using the gt()
package, which has extensive documentation and examples.
Here is an example making use of the gt::data_color()
function to add dgitheme
colors corresponding to data:
pizzaplace %>% filter( type %in% c("chicken", "supreme")) %>% group_by(type, size) %>% summarize( sold = n(), income = sum(price) ) %>% gt(rowname_col = "size") %>% data_color( columns = vars(sold, income), colors = scales::col_numeric( palette = dgitheme::dgi_pal("teal white", reverse = T)(2), domain = NULL) )
countrypops %>% filter(country_name == "Mongolia") %>% select(-contains("code")) %>% tail(10) %>% gt() %>% data_color( columns = vars(population), colors = scales::col_numeric( palette = dgitheme::dgi_pal("complementary")(2), domain = c(2.6E6, 3.1E6)) )
devtools::session_info()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.