Skip to contents

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

Usage

g6tr_scatter_text_plot(
  df,
  x,
  y,
  label = NULL,
  fill = NULL,
  plot_type = c("label", "text"),
  scale = "none",
  accuracy = 1,
  size = 5,
  include_point = FALSE,
  x_intercept = NULL,
  y_intercept = NULL,
  text_fontface = c("bold", "plain", "italic", "bold.italic"),
  highlight = NULL,
  highlight_condition = NULL,
  highlight_color = g6tr_pal("core")(1),
  highlight_color_other = "grey",
  color_scale = NULL,
  legend_title = "Legend",
  legend_bar_width = 7.5,
  wrap_length = 40,
  ...
)

Arguments

df

Data frame that holds x, y, label and/or fill columns.

x

Unquoted X variable. A continuous variable on the x-axis.

y

Unquoted Y variable. A continuous variable on the y-axis.

label

Unquoted variable. A value associated with each point/label on the plot

fill

Unquoted variable. Variable to fill individual point/label on the plot by.

plot_type

Character. How the plot is displayed. One of label(default) or text.

scale

Character. How should the x/y axis and fill be formatted. One of percent, dollar, none or a function e.g scales::comma. It follows the format c(x, y, fill). If you want to apply the same scale to all three parameters, pass in a single value.

accuracy

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

size

Numeric. Size of the label/text.

include_point

Logical. Should points be included in plot?.

x_intercept, y_intercept

Numeric. Where intercept lines on x and y axis should be.

text_fontface

Character. One of bold(default), "plain", "italic" or "bold.italic".

highlight

Character. Item(s) from label to be highlighted in the plot. When a value is passed to label, by default, it uses values present in label for highlighting. Alternatively, conditional statement (character) can be used for highlighting by passing them in highlight and setting highlight_condition to TRUE. See examples.

highlight_condition

Character. (Default is NULL) A filter expression to highlight items by.

highlight_color, highlight_color_other

Character. Valid color (6 digit hex-code or must consist in grDevices::colors()) for the highlights of the plot. By default, when highlight isn't specified, all points are g6tr core blue and when highlight is specified, highlighted points are displayed in g6tr core blue while the other points are in grey. If arguments are passed to highlight, highlight_color handles the color of the highlighted fields and highlight_color_other handles the colors of the fields not highlighted.

color_scale

Character. Valid color (6 digit hex-code or must consist in grDevices::colors()) to be used when fill is present. When fill is of continuous type, scale_fill_gradientn() is used and when fill is of character/factor type, scale_fill_manual() is used.

legend_title

Character. Title of the legend bar.

legend_bar_width

Numeric. Length of the legend bar with (default is 7.5 cm).

wrap_length

Numeric. When plot_type = label, passed to stringr::str_wrap() to edit width of text labels.

...

Additional arguments to be passed to ggrepel::geom_label_repel() or ggrepel::geom_text_repel().

Value

A ggplot object that can be manipulated further.

Examples

library(dplyr)
library(ggplot2)
library(tidyr)

set.seed(21324)
sample_data <-
  tibble(
    consider = sample(1:100, 20) / 100,
    purchase = sample(1:100, 20) / 100,
    label = paste("Reason", sample(LETTERS, 20)),
    performance = sample(-100:100, 20) / 100,
    group = rep(c("Asia", "Africa", "Europe", "N. America", "S.America"), 4)
  )

## Creating a basic scatter plot
g6tr_scatter_text_plot(
  df = sample_data,
  x = consider,
  y = purchase,
  label = label,
  color_scale = g6tr_pal("core")(1),
  scale = "percent"
)


## Using a conditional statement as highlighting parameter
g6tr_scatter_text_plot(
  df = sample_data,
  x = consider,
  y = purchase,
  label = label,
  highlight_condition = "consider > 0.5 & purchase > 0.5 & group == 'Asia'",
  include_point = TRUE,
  x_intercept = 0.5,
  y_intercept = 0.5,
  scale = "percent"
)
#> Warning: Non-numeric fill argument detected. No color scale provided. Defaulting to g6tr
#> colors


## Highlighting based on a value specified in `label`
g6tr_scatter_text_plot(
  df = sample_data,
  x = consider,
  y = purchase,
  label = label,
  highlight = c("Reason G", "Reason A"),
  plot_type = "label",
  x_intercept = 0.5,
  y_intercept = 0.5,
  scale = "percent"
)
#> Warning: Non-numeric fill argument detected. No color scale provided. Defaulting to g6tr
#> colors


## Fill points by a variable (numeric)
g6tr_scatter_text_plot(
  df = sample_data,
  x = consider,
  y = purchase,
  label = label,
  fill = performance,
  plot_type = "label",
  x_intercept = 0.5,
  y_intercept = 0.5,
  scale = c("percent", "percent", "none"),
  legend_title = "Performance"
)
#> Warning: Numeric fill argument detected. No color scale provided. Defaulting to g6tr
#> Green -> Red diverging (5) colors


## Making extra customization to plot
g6tr_scatter_text_plot(
  df = sample_data,
  x = consider,
  y = purchase,
  label = label,
  fill = performance,
  x_intercept = 0.5,
  y_intercept = 0.5,
  scale = c("percent", "percent", "none"),
  legend_title = "Performance"
) +
  scale_size_continuous()
#> Warning: Numeric fill argument detected. No color scale provided. Defaulting to g6tr
#> Green -> Red diverging (5) colors
#> Scale for size is already present.
#> Adding another scale for size, which will replace the existing scale.


## Fill points by a variable (character/factor)
g6tr_scatter_text_plot(
  df = sample_data,
  x = consider,
  y = purchase,
  label = label,
  fill = group,
  plot_type = "text",
  x_intercept = 0.5,
  y_intercept = 0.5,
  scale = c("percent", "percent", "none"),
  legend_title = "Continent"
)
#> Warning: Non-numeric fill argument detected. No color scale provided. Defaulting to g6tr
#> colors