Description Usage Arguments Details Examples
Wrapper for ggplot() + geom_point() visualisations
1 2 3 |
x |
The x axis variable |
y |
The y axis variable |
dataframe |
An optional dataframe. If dataframe is used, all variables (x, y, color and size) must be of the dataframe. |
color |
A vector of the same length of x and y. An arbitrary color (e.g. "blue") cannot be used. If vector color is not provided, the default color is blue. |
size |
A vector of the same length of x and y. An arbitrary size (e.g. 8) cannot be used. |
alpha |
The transparency of the data points. |
title |
The title of the plot |
x_label |
The x axis label. If no label is given, the name of the x variable is used. |
y_label |
The y axis label. If no label is given, the name of the y variable is used. |
color_title |
The title of the color legend. If there is no color vector, the color_title is ignored. |
size_title |
The title of the size legend. If there is no size vector, the size_title is ignored. |
facet_formula |
The formula to create facet grids on ggplot. |
This function allows the creation of scatter plots and bubble charts in one line. It handles color and size automatically using predefined settings.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | # Load packages
library(gridExtra)
# Set a seed to always have the same results
set.seed(0010)
# Create vectors
var_x = sort(rnorm(100))
var_y = var_x * 2 + rnorm(100)
var_z = c(rep('A', 25), rep('B', 25), rep('C', 25), rep('D', 25))
var_s = rnorm(100)
var_f1 = sample(x = c('Alpha', 'Bravo'), size = 100, replace = T)
var_f2 = sample(x = c('Sierra', 'Tango'), size = 100, replace = T)
# Create dataframe
df = data.frame(x = var_x, y = var_y, z = var_z, s = var_s,
a = var_f1, b = var_f2)
# Create scatter plots
p1 = scatter_plot(x = var_x, y = var_y,
title = 'Simple')
p2 = scatter_plot(x = var_x, y = var_y, color = var_z,
title = 'With categorical color')
p3 = scatter_plot(x = var_x, y = var_y, size = var_s,
title = 'With size')
p4 = scatter_plot(x = var_x, y = var_y, color = var_x, size = var_s,
title = 'With continuous color and size')
# Plot them
grid.arrange(p1, p2, p3, p4, ncol = 2)
# Wait for key stroke
key_stroke = readline(prompt = 'Press Enter to continue...')
# Plot facets
scatter_plot(x = x,
y = y,
dataframe = df,
color = z,
size = s,
title = 'All plus df, labels and facets',
x_label = 'My X Var',
y_label = 'My Y Var',
color_title = 'Color',
size_title = 'Size',
facet_formula = a ~ b)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.