Skip to contents

Generate a simple Heatmap with all of Gradient's best practices applied. Almost instantly ready for publication.

Usage

g6tr_heatmap_plot(
  df,
  x,
  y,
  fill,
  custom_label = NULL,
  order_x_by = NULL,
  order_y_by = NULL,
  color_low = "#93DB95",
  color_high = "#399091",
  text_color = "#ffffff",
  tile_color = "#ffffff",
  text_size = 14,
  axis_text_size = 14,
  scale = c("percent", "dollar", "custom"),
  accuracy = 0.1,
  x_wrap = 30,
  y_wrap = 45,
  legend = "none"
)

Arguments

df

Data frame that holds x and y columns.

x

Unquoted (factor or character) X variable.

y

Unquoted (factor or character) Y variable.

fill

Unquoted (continuous) variable to be used to form the basis of the heatmap.

custom_label

Unquoted variable.(optional) To be used to display text on heatmap. Use this argument when you already have a custom label you'd like to display. Set scale = "custom" when an argument is passed.

order_x_by

Character. What value in y should x be ordered by?. Use apply_desc(character) if you want it ordered in descending order

order_y_by

Character. What value in x should y be ordered by?. Use apply_desc(character) if you want it ordered in descending order.

color_low

Lower gradient color.

color_high

Higher gradient color.

text_color

Tile text color.

tile_color

Color of tile border.

text_size

Tile text size. Default 14.

axis_text_size

Numeric axis y and x text size. Default 14.

scale

Character. How should the labels and x axis be formatted. One of percent, dollar, custom or a function e.g scales::comma. Whenever there's a custom label, set scale to custom.

accuracy

Numeric. Passed to the accuracy argument of the selected scale function to set numeric accuracy. Default is 1.

x_wrap

Numeric x wrap length. Default 30.

y_wrap

Numeric y wrap length. Default 45.

legend

String to be passed to the legend.position argument of theme(). Default "none".

Value

A ggplot object that can be manipulated further.

Examples


## Preparing test data set.
library(dplyr)
library(tidyr)
library(scales)

set.seed(4224)
sales_record <-
  expand_grid(
    month = factor(month.abb, levels = month.abb),
    year = factor(2016:2022)
  ) %>%
  mutate(
    value = sample(1:100, nrow(.), replace = TRUE) / 100,
    dollar_value = value * 124,
    dollar_label = dollar(value * 124, accuracy = 0.1)
  )

## Plot a heatmap without passing a custom label.
sales_record %>%
  g6tr_heatmap_plot(
    x = year,
    y = month,
    fill = value,
    scale = "percent",
    text_size = 5
  )


## Plot a heatmap with custom labels.
sales_record %>%
  g6tr_heatmap_plot(
    x = year,
    y = month,
    fill = dollar_value,
    custom_label = dollar_label,
    scale = "custom",
    text_size = 5
  )


## Plot a heatmap with custom scale
sales_record %>%
  g6tr_heatmap_plot(
    x = year,
    y = month,
    fill = dollar_value,
    scale = scales::dollar,
    accuracy = 1,
    text_size = 5
  )


## Order a heatmap

## Here we are trying to order the y axis (month)
## by a particular level (2016) on the x axis (year)
sales_record %>%
  g6tr_heatmap_plot(
    x = year,
    y = month,
    fill = dollar_value,
    order_y_by = "2016",
    scale = scales::dollar,
    accuracy = 1,
    text_size = 5
  )


## Here we are trying to order the x axis (year)
## by a particular level (Dec) on the y axis (month) in descending order
sales_record %>%
  g6tr_heatmap_plot(
    x = year,
    y = month,
    fill = dollar_value,
    order_x_by = apply_desc("Dec"),
    scale = scales::dollar,
    accuracy = 1,
    text_size = 5
  )

  
## Here we are trying to order both the x axis and y axis (year and month)
## 1. for year: by a particular level (Dec) on the y axis (month) in descending order
## 2. for month: by a particular level (2016) on the x axis (year)
sales_record %>%
  g6tr_heatmap_plot(
    x = year,
    y = month,
    fill = dollar_value,
    order_x_by = apply_desc("Dec"),
    order_y_by = "2016",
    scale = scales::dollar,
    accuracy = 1,
    text_size = 5
  )