scandal_scatter_plot: Create a scatter plot

Description Usage Arguments Value Author(s) Examples

View source: R/plotting.R

Description

This function creates a scatter plot i.e. a two-dimensional plot that uses dots to visualize the values of two different variables x and y.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
scandal_scatter_plot(
  x,
  y,
  labels = NULL,
  color_legend_name = NULL,
  title = NULL,
  xlab = NULL,
  ylab = NULL,
  plot_ordered = FALSE,
  order_by_axis = "y",
  title_text_size = 20
)

Arguments

x

a numeric vector representing the variable to plot in the x axis. If set to NULL then a vector 1:length(y) will be used instead.

y

a numeric vector representing the variable to plot in the y axis. If set to NULL then a vector 1:length(x) will be used instead.

labels

an optional vector of character labels for each (x, y) point. Used for color coding each point. Default is NULL.

color_legend_name

the name of the legend, valid only if the labels vector is provided. Default is NULL (can be set to NULL as well if labels vector is provided).

title

an optional string for the title of the plot. Default is NULL (no title).

xlab

a label for the x axis. Default is NULL (no label).

ylab

a label for the y axis. Default is NULL (no label).

plot_ordered

a logical indicating of the provided data should be plotted ordered. Valid only if either x or y are provided but not both of them. Default is FALSE.

order_by_axis

the axis by which to order the data (either "x" or "y"). Default is "y".

title_text_size

text size of the title. Default is 20.

Value

A ggplot2 object representing the scatter plot.

Author(s)

Avishay Spitzer

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Example #1 - One variable of interest

# Generate randomly 1000 data points
y <- rnorm(1000)

# See the difference between the ordered and unordered scatter plots
scandal_scatter_plot(x = NULL, y = y, plot_ordered = TRUE, order_by_axis = "y", title = "Ordered plot")
scandal_scatter_plot(x = NULL, y = y, plot_ordered = FALSE, title = "Unordered plot")

# Example #2 - Two variables with linear correlation

library(MASS)
library(ggplot2)

samples <- 1000
r <- 0.8

# Generate 2 variables with a linear correlation between them (1000 data points with pearsnon's r 0.8)
data <- mvrnorm(n = samples, mu = c(0, 0), Sigma = matrix(c(1, r, r, 1), nrow = 2), empirical = TRUE)

# Plot the two variables. As scandal_scatter_plot returns a ggplot object we can add to it a
# linear regression line showing the positive correlation
scandal_scatter_plot(x = data[, 1], y = data[, 2], plot_ordered = FALSE) +
    geom_smooth(method = "glm")

dravishays/scandal documentation built on Jan. 8, 2020, 1:30 p.m.