knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README_",
  dpi = 300,
  out.width = "100%"
)

kfbmisc

kfbmisc is a package full of utility function I find useful

Installation

You can install the development version of kfbmisc from GitHub with:

# install.packages("devtools")
devtools::install_github("kylebutts/kfbmisc")
library(kfbmisc)

ggplot2 theme

This is a relatively simple theme that makes the font larger for figures and adds more margins around things.

#| fig.width: 8
#| fig.height: 4
library(tidyverse)
library(palmerpenguins)
penguins = penguins |> drop_na(body_mass_g, bill_length_mm, sex)
ggplot(penguins) +
  geom_point(aes(x = body_mass_g, y = bill_length_mm), shape = 21) +
  labs(
    x = "Body mass", y = "Bill length"
  ) +
  theme_kyle()
#| fig.width: 8
#| fig.height: 4
ggplot(penguins) +
  geom_point(aes(x = body_mass_g, y = bill_length_mm, color = species), shape = 21) +
  scale_color_manual(
    values = kfbmisc::kyle_color("magenta", "blue", "green")
  ) + 
  labs(
    x = "Body mass", y = "Bill length", color = NULL
  ) +
  theme_kyle(legend = "top")
#| fig.width: 8
#| fig.height: 4
ggplot(
  penguins |> mutate(sex = str_to_title(sex))
) +
  geom_point(aes(x = body_mass_g, y = bill_length_mm, color = species), shape = 21) +
  facet_wrap(~ sex) +
  scale_color_manual(
    values = kfbmisc::kyle_color("magenta", "blue", "green")
  ) + 
  labs(
    x = "Body mass", y = "Bill length", color = NULL
  ) +
  theme_kyle(legend = "top")

tikzsave

mod = lm(bill_length_mm ~ body_mass_g, penguins)
title_str = paste0(
  "$\\hat{y} = ", round(coef(mod)[1], 1), " + ", round(coef(mod)[2], 3), "x$"
)
p = ggplot(penguins) +
  geom_point(aes(x = body_mass_g, y = bill_length_mm), shape = 21) +
  geom_smooth(
    aes(x = body_mass_g, y = bill_length_mm), 
    color = kfbmisc::kyle_color("blue"),
    fill = colorspace::lighten(kfbmisc::kyle_color("blue"), 0.8),
    method = "lm",
    formula = y ~ x,
    linewidth = 1.5,
  ) +
  labs(
    x = "Body mass", y = "Bill length",
    title = sprintf("$\\hat{y} = %0.1f + %0.3f x$", coef(mod)[1], coef(mod)[2])
  ) +
  theme_kyle() 
# `plot` -> `tikzpicture` -> `pdf`
tikzsave(
  "man/figures/README_penguins.pdf",
  p, width = 8, height = 4,
  # uses `imagemagick` to convert to png 
  create_png = TRUE
)
#| echo: false
knitr::include_graphics("man/figures/README_penguins.png")

Maps

#| fig.width: 8
#| fig.height: 5
library(sf)
nc <- st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE)

(map <- ggplot(nc) +
  geom_sf(aes(fill = BIR74)) +
  scale_fill_fermenter(
    direction = 1, palette = 3,
    guide = guide_colorbar(direction = "horizontal")
  ) + 
  labs(fill = "1974 Births") +
  theme_kyle(map = TRUE) +
  theme(
    legend.position = "inside",
    legend.position.inside = c(0.1, 0.07),
    legend.justification = c(0, 0),
    legend.title.position = "top",
    legend.key = element_rect(fill ="red"),
    legend.key.size = unit(5, "lines"),
    legend.key.height = unit(16, "pt")
  ))

Additional Examples

Most of these examples are taken from pilot. The source code can be found in gallery/:

Line Chart

r knitr::include_graphics("gallery/figures/line_chart.png")

[Scatter Chart](https://github.com/kylebutts/kfbmisc/blob/master/gallery/

scatter_chart_basic.R) r knitr::include_graphics("gallery/figures/scatter_chart.png")

[Scatter Chart Facets](https://github.com/kylebutts/kfbmisc/blob/master/gallery/

scatter_chart_facets.R) r knitr::include_graphics("gallery/figures/scatter_chart_facets.png")

[Stacked Column Chart](https://github.com/kylebutts/kfbmisc/blob/master/gallery/

stacked_column_chart.R) r knitr::include_graphics("gallery/figures/stacked_column_chart.png")

Custom Labels

r knitr::include_graphics("gallery/figures/reg_line_with_label.png")



kylebutts/kfbmisc documentation built on April 17, 2025, 5:20 p.m.