Generate a scatter plot with all of Gradient's best practices applied. Almost instantly ready for publication.
Usage
g6tr_scatter_point_plot(
df,
x,
y,
label = NULL,
fill = NULL,
scale = "none",
accuracy = 1,
size = 5,
x_intercept = NULL,
y_intercept = NULL,
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
)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.
- 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
accuracyargument of the selectedscalefunction to set numeric accuracy. Default is1.- size
Numeric. Size of point.
- x_intercept, y_intercept
Numeric. Where intercept lines on x and y axis should be.
- highlight
Character. Filter expression. Item(s) from
labelto be highlighted in the plot. When a value is passed tolabeluse values present inlabelfor highlighting.- 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, whenhighlightisn't specified, all points are g6tr core blue and whenhighlightis specified, highlighted points are displayed in g6tr core blue while the other points are in grey. If arguments are passed tohighlight,highlight_colorhandles the color of the highlighted fields andhighlight_color_otherhandles 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 whenfillis 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).
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_point_plot(
df = sample_data,
x = consider,
y = purchase,
color_scale = g6tr_pal("core")(1),
scale = "percent"
)
## Using a conditional statement as highlighting parameter
g6tr_scatter_point_plot(
df = sample_data,
x = consider,
y = purchase,
highlight_condition = "consider > 0.5 & purchase > 0.5 & group == 'Asia'",
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_point_plot(
df = sample_data,
x = consider,
y = purchase,
label = label,
highlight = c("Reason G", "Reason A"),
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_point_plot(
df = sample_data,
x = consider,
y = purchase,
fill = performance,
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_point_plot(
df = sample_data,
x = consider,
y = purchase,
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_point_plot(
df = sample_data,
x = consider,
y = purchase,
fill = group,
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
## Making some extra customization on plot
## Here, we would like to convert from displaying
## colors for each continent to displaying shapes.
#
g6tr_scatter_point_plot(
df = sample_data,
x = consider,
y = purchase,
fill = group,
x_intercept = 0.5,
y_intercept = 0.5,
scale = c("percent", "percent", "none"),
legend_title = "Continent"
) +
## Apply custom shape formatting
scale_shape_discrete() +
scale_color_manual(values = rep("black", 5)) +
## Set shape argument in `guides()` to make legends visible.
## Set default color argument to `waiver()` to prevent it from showing in plot.
guides(
shape = guide_legend(
title = "Continent",
title.theme = element_text(face = "bold"),
title.position = "top",
title.hjust = 0
),
color = waiver()
)
#> Warning: Non-numeric fill argument detected. No color scale provided. Defaulting to g6tr
#> colors
#> Scale for shape is already present.
#> Adding another scale for shape, which will replace the existing scale.
#> Scale for colour is already present.
#> Adding another scale for colour, which will replace the existing scale.